Use season module internally

This commit is contained in:
Kevin Sawicki
2013-05-14 11:58:34 -07:00
parent 81f7354fb5
commit 2f54cb4c22
9 changed files with 28 additions and 238 deletions

View File

@@ -3,8 +3,7 @@ Package = require 'package'
fsUtils = require 'fs-utils'
_ = require 'underscore'
$ = require 'jquery'
CSON = require 'cson'
CSON = require 'season'
### Internal: Loads and resolves packages. ###
@@ -63,11 +62,11 @@ class AtomPackage extends Package
loadMetadata: ->
if metadataPath = fsUtils.resolveExtension(fsUtils.join(@path, 'package'), ['json', 'cson'])
@metadata = CSON.readObject(metadataPath)
@metadata = CSON.readFileSync(metadataPath)
@metadata ?= {}
loadKeymaps: ->
@keymaps = @getKeymapPaths().map (path) -> [path, CSON.readObject(path)]
@keymaps = @getKeymapPaths().map (path) -> [path, CSON.readFileSync(path)]
getKeymapPaths: ->
keymapsDirPath = fsUtils.join(@path, 'keymaps')

View File

@@ -1,7 +1,7 @@
fsUtils = require 'fs-utils'
_ = require 'underscore'
EventEmitter = require 'event-emitter'
CSON = require 'cson'
CSON = require 'season'
fs = require 'fs'
async = require 'async'
pathWatcher = require 'pathwatcher'
@@ -70,7 +70,7 @@ class Config
loadUserConfig: ->
if fsUtils.exists(@configFilePath)
try
userConfig = CSON.readObject(@configFilePath)
userConfig = CSON.readFileSync(@configFilePath)
_.extend(@settings, userConfig)
@configFileHasErrors = false
@trigger 'updated'
@@ -173,6 +173,6 @@ class Config
@trigger 'updated'
save: ->
CSON.writeObject(@configFilePath, @settings)
CSON.writeFileSync(@configFilePath, @settings)
_.extend Config.prototype, EventEmitter

View File

@@ -1,8 +1,7 @@
$ = require 'jquery'
_ = require 'underscore'
fsUtils = require 'fs-utils'
CSON = require 'cson'
CSON = require 'season'
BindingSet = require 'binding-set'
# Internal: Associates keymaps with actions.
@@ -46,7 +45,7 @@ class Keymap
@load(filePath) for filePath in fsUtils.list(directoryPath, ['.cson', '.json'])
load: (path) ->
@add(path, CSON.readObject(path))
@add(path, CSON.readFileSync(path))
add: (args...) ->
name = args.shift() if args.length > 1

View File

@@ -5,7 +5,7 @@ _ = require 'underscore'
SnippetExpansion = require './snippet-expansion'
Snippet = require './snippet'
TextMatePackage = require 'text-mate-package'
CSON = require 'cson'
CSON = require 'season'
async = require 'async'
module.exports =
@@ -41,7 +41,7 @@ module.exports =
loadSnippetFile = (filename, done) =>
return done() if filename.indexOf('.') is 0
filepath = fsUtils.join(snippetsDirPath, filename)
CSON.readObjectAsync filepath, (err, object) =>
CSON.readFile filepath, (err, object) =>
if err
console.warn "Error reading snippets file '#{filepath}': #{err.stack}"
else
@@ -66,7 +66,7 @@ module.exports =
try
readObject =
if CSON.isObjectPath(filepath)
CSON.readObjectAsync.bind(CSON)
CSON.readFile.bind(CSON)
else
fsUtils.readPlistAsync.bind(fsUtils)

View File

@@ -1,117 +0,0 @@
require 'underscore-extensions'
_ = require 'underscore'
fs = require 'fs'
fsUtils = require 'fs-utils'
module.exports =
isObjectPath: (path) ->
extension = fsUtils.extension(path)
extension is '.cson' or extension is '.json'
readObject: (path) ->
@parseObject(path, fsUtils.read(path))
readObjectAsync: (path, done) ->
fs.readFile path, 'utf8', (err, contents) =>
return done(err) if err?
try
done(null, @parseObject(path, contents))
catch err
done(err)
parseObject: (path, contents) ->
if fsUtils.extension(path) is '.cson'
CoffeeScript = require 'coffee-script'
CoffeeScript.eval(contents, bare: true)
else
JSON.parse(contents)
writeObject: (path, object) ->
if fsUtils.extension(path) is '.cson'
content = @stringify(object)
else
content = JSON.stringify(object, undefined, 2)
fsUtils.write(path, "#{content}\n")
stringifyIndent: (level=0) -> _.multiplyString(' ', Math.max(level, 0))
stringifyString: (string) ->
string = JSON.stringify(string)
string = string[1...-1] # Remove surrounding double quotes
string = string.replace(/\\"/g, '"') # Unescape escaped double quotes
string = string.replace(/'/g, '\\\'') # Escape single quotes
"'#{string}'" # Wrap in single quotes
stringifyBoolean: (boolean) -> "#{boolean}"
stringifyNumber: (number) -> "#{number}"
stringifyNull: -> 'null'
stringifyArray: (array, indentLevel=0) ->
return '[]' if array.length is 0
cson = '[\n'
for value in array
indent = @stringifyIndent(indentLevel + 2)
cson += indent
if _.isString(value)
cson += @stringifyString(value)
else if _.isBoolean(value)
cson += @stringifyBoolean(value)
else if _.isNumber(value)
cson += @stringifyNumber(value)
else if _.isNull(value) or value is undefined
cson += @stringifyNull(value)
else if _.isArray(value)
cson += @stringifyArray(value, indentLevel + 2)
else if _.isObject(value)
cson += "{\n#{@stringifyObject(value, indentLevel + 4)}\n#{indent}}"
else
throw new Error("Unrecognized type for array value: #{value}")
cson += '\n'
"#{cson}#{@stringifyIndent(indentLevel)}]"
stringifyObject: (object, indentLevel=0) ->
return '{}' if _.isEmpty(object)
cson = ''
prefix = ''
for key, value of object
continue if value is undefined
if _.isFunction(value)
throw new Error("Function specified as value to key: #{key}")
cson += "#{prefix}#{@stringifyIndent(indentLevel)}'#{key}':"
if _.isString(value)
cson += " #{@stringifyString(value)}"
else if _.isBoolean(value)
cson += " #{@stringifyBoolean(value)}"
else if _.isNumber(value)
cson += " #{@stringifyNumber(value)}"
else if _.isNull(value)
cson += " #{@stringifyNull(value)}"
else if _.isArray(value)
cson += " #{@stringifyArray(value, indentLevel)}"
else if _.isObject(value)
if _.isEmpty(value)
cson += ' {}'
else
cson += "\n#{@stringifyObject(value, indentLevel + 2)}"
else
throw new Error("Unrecognized value type for key: #{key} with value: #{value}")
prefix = '\n'
cson
stringify: (object) ->
throw new Error("Cannot stringify undefined object") if object is undefined
throw new Error("Cannot stringify function: #{object}") if _.isFunction(object)
return @stringifyString(object) if _.isString(object)
return @stringifyBoolean(object) if _.isBoolean(object)
return @stringifyNumber(object) if _.isNumber(object)
return @stringifyNull(object) if _.isNull(object)
return @stringifyArray(object) if _.isArray(object)
return @stringifyObject(object) if _.isObject(object)
throw new Error("Unrecognized type to stringify: #{object}")

View File

@@ -334,15 +334,15 @@ module.exports =
done(err)
readObject: (path) ->
cson = require 'cson'
if cson.isObjectPath(path)
cson.readObject(path)
CSON = require 'season'
if CSON.isObjectPath(path)
CSON.readFileSync(path)
else
@readPlist(path)
readObjectAsync: (path, done) ->
cson = require 'cson'
if cson.isObjectPath(path)
cson.readObjectAsync(path, done)
CSON = require 'season'
if CSON.isObjectPath(path)
CSON.readFile(path, done)
else
@readPlistAsync(path, done)