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

@ -19,11 +19,11 @@
"space-pen": "git://github.com/nathansobo/space-pen.git",
"less": "git://github.com/nathansobo/less.js.git",
"roaster": "0.0.3",
"jqueryui-browser": "1.10.2-1"
"jqueryui-browser": "1.10.2-1",
"season": "0.7.0"
},
"devDependencies": {
"biscotto": "0.0.11",
"season": "~0.7.0"
"biscotto": "0.0.11"
},
"private": true,
"scripts": {

View File

@ -1,3 +1,4 @@
fs = require 'fs'
fsUtils = require 'fs-utils'
describe "Config", ->
@ -46,7 +47,7 @@ describe "Config", ->
describe ".save()", ->
beforeEach ->
spyOn(fsUtils, 'write')
spyOn(fs, 'writeFileSync')
jasmine.unspy config, 'save'
describe "when ~/.atom/config.json exists", ->
@ -57,11 +58,11 @@ describe "Config", ->
config.set("x.y.z", 3)
config.setDefaults("a.b", e: 4, f: 5)
fsUtils.write.reset()
fs.writeFileSync.reset()
config.save()
expect(fsUtils.write.argsForCall[0][0]).toBe(fsUtils.join(config.configDirPath, "config.json"))
writtenConfig = JSON.parse(fsUtils.write.argsForCall[0][1])
expect(fs.writeFileSync.argsForCall[0][0]).toBe(fsUtils.join(config.configDirPath, "config.json"))
writtenConfig = JSON.parse(fs.writeFileSync.argsForCall[0][1])
expect(writtenConfig).toEqual config.settings
describe "when ~/.atom/config.json doesn't exist", ->
@ -72,12 +73,12 @@ describe "Config", ->
config.set("x.y.z", 3)
config.setDefaults("a.b", e: 4, f: 5)
fsUtils.write.reset()
fs.writeFileSync.reset()
config.save()
expect(fsUtils.write.argsForCall[0][0]).toBe(fsUtils.join(config.configDirPath, "config.cson"))
expect(fs.writeFileSync.argsForCall[0][0]).toBe(fsUtils.join(config.configDirPath, "config.cson"))
CoffeeScript = require 'coffee-script'
writtenConfig = CoffeeScript.eval(fsUtils.write.argsForCall[0][1], bare: true)
writtenConfig = CoffeeScript.eval(fs.writeFileSync.argsForCall[0][1], bare: true)
expect(writtenConfig).toEqual config.settings
describe ".setDefaults(keyPath, defaults)", ->

View File

@ -1,92 +0,0 @@
CSON = require 'cson'
describe "CSON", ->
describe "@stringify(object)", ->
describe "when the object is undefined", ->
it "throws an exception", ->
expect(-> CSON.stringify()).toThrow()
describe "when the object is a function", ->
it "throws an exception", ->
expect(-> CSON.stringify(-> 'function')).toThrow()
describe "when the object contains a function", ->
it "throws an exception", ->
expect(-> CSON.stringify(a: -> 'function')).toThrow()
describe "when formatting an undefined key", ->
it "does not include the key in the formatted CSON", ->
expect(CSON.stringify(b: 1, c: undefined)).toBe "'b': 1"
describe "when formatting a string", ->
it "returns formatted CSON", ->
expect(CSON.stringify(a: 'b')).toBe "'a': 'b'"
it "escapes single quotes", ->
expect(CSON.stringify(a: "'b'")).toBe "'a': '\\\'b\\\''"
it "doesn't escape double quotes", ->
expect(CSON.stringify(a: '"b"')).toBe "'a': '\"b\"'"
it "escapes newlines", ->
expect(CSON.stringify("a\nb")).toBe "'a\\nb'"
describe "when formatting a boolean", ->
it "returns formatted CSON", ->
expect(CSON.stringify(true)).toBe 'true'
expect(CSON.stringify(false)).toBe 'false'
expect(CSON.stringify(a: true)).toBe "'a': true"
expect(CSON.stringify(a: false)).toBe "'a': false"
describe "when formatting a number", ->
it "returns formatted CSON", ->
expect(CSON.stringify(54321.012345)).toBe '54321.012345'
expect(CSON.stringify(a: 14)).toBe "'a': 14"
expect(CSON.stringify(a: 1.23)).toBe "'a': 1.23"
describe "when formatting null", ->
it "returns formatted CSON", ->
expect(CSON.stringify(null)).toBe 'null'
expect(CSON.stringify(a: null)).toBe "'a': null"
describe "when formatting an array", ->
describe "when the array is empty", ->
it "puts the array on a single line", ->
expect(CSON.stringify([])).toBe "[]"
it "returns formatted CSON", ->
expect(CSON.stringify(a: ['b'])).toBe "'a': [\n 'b'\n]"
expect(CSON.stringify(a: ['b', 4])).toBe "'a': [\n 'b'\n 4\n]"
describe "when the array has an undefined value", ->
it "formats the undefined value as null", ->
expect(CSON.stringify(['a', undefined, 'b'])).toBe "[\n 'a'\n null\n 'b'\n]"
describe "when the array contains an object", ->
it "wraps the object in {}", ->
expect(CSON.stringify([{a:'b', a1: 'b1'}, {c: 'd'}])).toBe "[\n {\n 'a': 'b'\n 'a1': 'b1'\n }\n {\n 'c': 'd'\n }\n]"
describe "when formatting an object", ->
describe "when the object is empty", ->
it "returns {}", ->
expect(CSON.stringify({})).toBe "{}"
it "returns formatted CSON", ->
expect(CSON.stringify(a: {b: 'c'})).toBe "'a':\n 'b': 'c'"
expect(CSON.stringify(a:{})).toBe "'a': {}"
expect(CSON.stringify(a:[])).toBe "'a': []"
describe "when converting back to an object", ->
it "produces the original object", ->
object =
showInvisibles: true
fontSize: 20
core:
themes: ['a', 'b']
whitespace:
ensureSingleTrailingNewline: true
cson = CSON.stringify(object)
CoffeeScript = require 'coffee-script'
evaledObject = CoffeeScript.eval(cson, bare: true)
expect(evaledObject).toEqual object

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)