From 115b960ce7c60f587a2e2c9d320c54fa8216004d Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 11 Mar 2013 22:39:57 -0700 Subject: [PATCH] Handle reading and writing in cson.coffee Moves helpers from fs.coffee to cson.coffee --- src/app/atom-package.coffee | 3 ++- src/app/atom-theme.coffee | 3 ++- src/app/config.coffee | 5 +++-- src/app/keymap.coffee | 3 ++- src/app/text-mate-grammar.coffee | 5 +++-- src/app/text-mate-package.coffee | 5 +++-- .../snippets/lib/load-snippets-handler.coffee | 5 +++-- src/packages/snippets/lib/snippets.coffee | 3 ++- src/stdlib/cson.coffee | 20 +++++++++++++++++++ src/stdlib/fs.coffee | 20 ------------------- 10 files changed, 40 insertions(+), 32 deletions(-) diff --git a/src/app/atom-package.coffee b/src/app/atom-package.coffee index 8bd99562e..eb4cbfdfc 100644 --- a/src/app/atom-package.coffee +++ b/src/app/atom-package.coffee @@ -2,6 +2,7 @@ Package = require 'package' fs = require 'fs' _ = nodeRequire 'underscore' $ = require 'jquery' +CSON = require 'cson' module.exports = class AtomPackage extends Package @@ -69,7 +70,7 @@ class AtomPackage extends Package loadMetadata: -> if metadataPath = fs.resolveExtension(fs.join(@path, 'package'), ['cson', 'json']) - @metadata = fs.readObject(metadataPath) + @metadata = CSON.readObject(metadataPath) @metadata ?= {} loadKeymaps: -> diff --git a/src/app/atom-theme.coffee b/src/app/atom-theme.coffee index 6a3e1e379..697e44697 100644 --- a/src/app/atom-theme.coffee +++ b/src/app/atom-theme.coffee @@ -1,5 +1,6 @@ fs = require 'fs' Theme = require 'theme' +CSON = require 'cson' module.exports = class AtomTheme extends Theme @@ -13,7 +14,7 @@ class AtomTheme extends Theme else metadataPath = fs.resolveExtension(fs.join(@path, 'package'), ['cson', 'json']) if fs.isFile(metadataPath) - stylesheetNames = fs.readObject(metadataPath)?.stylesheets + stylesheetNames = CSON.readObject(metadataPath)?.stylesheets if stylesheetNames @loadStylesheet(fs.join(@path, name)) for name in stylesheetNames else diff --git a/src/app/config.coffee b/src/app/config.coffee index d01180a3f..126202609 100644 --- a/src/app/config.coffee +++ b/src/app/config.coffee @@ -1,6 +1,7 @@ fs = require 'fs' _ = nodeRequire 'underscore' EventEmitter = require 'event-emitter' +CSON = require 'cson' configDirPath = fs.absolute("~/.atom") bundledPackagesDirPath = fs.join(resourcePath, "src/packages") @@ -55,7 +56,7 @@ class Config loadUserConfig: -> if fs.exists(@configFilePath) - userConfig = fs.readObject(@configFilePath) + userConfig = CSON.readObject(@configFilePath) _.extend(@settings, userConfig) get: (keyPath) -> @@ -96,6 +97,6 @@ class Config @trigger 'updated' save: -> - fs.writeObject(@configFilePath, @settings) + CSON.writeObject(@configFilePath, @settings) _.extend Config.prototype, EventEmitter diff --git a/src/app/keymap.coffee b/src/app/keymap.coffee index b3d45872d..9846fd58f 100644 --- a/src/app/keymap.coffee +++ b/src/app/keymap.coffee @@ -1,6 +1,7 @@ $ = require 'jquery' _ = nodeRequire 'underscore' fs = require 'fs' +CSON = require 'cson' BindingSet = require 'binding-set' @@ -39,7 +40,7 @@ class Keymap @load(filePath) for filePath in fs.list(directoryPath, ['.cson', '.json']) load: (path) -> - @add(fs.readObject(path)) + @add(CSON.readObject(path)) add: (keymap) -> for selector, bindings of keymap diff --git a/src/app/text-mate-grammar.coffee b/src/app/text-mate-grammar.coffee index 712b3e2ad..2ceb59bfb 100644 --- a/src/app/text-mate-grammar.coffee +++ b/src/app/text-mate-grammar.coffee @@ -2,14 +2,15 @@ _ = nodeRequire 'underscore' fs = require 'fs' plist = require 'plist' Token = require 'token' +CSON = require 'cson' {OnigRegExp, OnigScanner} = nodeRequire 'oniguruma' module.exports = class TextMateGrammar @readFromPath: (path) -> grammarContent = null - if fs.isObjectPath(path) - grammarContent = fs.readObject(path) + if CSON.isObjectPath(path) + grammarContent = CSON.readObject(path) else plist.parseString fs.read(path), (e, data) -> throw new Error(e) if e diff --git a/src/app/text-mate-package.coffee b/src/app/text-mate-package.coffee index 2c430de99..1e727860b 100644 --- a/src/app/text-mate-package.coffee +++ b/src/app/text-mate-package.coffee @@ -3,6 +3,7 @@ fs = require 'fs' plist = require 'plist' _ = nodeRequire 'underscore' TextMateGrammar = require 'text-mate-grammar' +CSON = require 'cson' module.exports = class TextMatePackage extends Package @@ -76,8 +77,8 @@ class TextMatePackage extends Package readObjectFromPath: (path, callback) -> object = null error = null - if fs.isObjectPath(path) - object = fs.readObject(path) + if CSON.isObjectPath(path) + object = CSON.readObject(path) else plist.parseString fs.read(path), (e, data) -> error = e diff --git a/src/packages/snippets/lib/load-snippets-handler.coffee b/src/packages/snippets/lib/load-snippets-handler.coffee index c897330dc..331013adb 100644 --- a/src/packages/snippets/lib/load-snippets-handler.coffee +++ b/src/packages/snippets/lib/load-snippets-handler.coffee @@ -1,6 +1,7 @@ fs = require 'fs' TextMatePackage = require 'text-mate-package' SnippetBodyParser = require './snippet-body-parser' +CSON = require 'cson' module.exports = snippetsLoaded: (snippets) -> @@ -20,7 +21,7 @@ module.exports = continue if fs.base(snippetsPath).indexOf('.') is 0 try - if fs.isObjectPath(snippetsPath) and object = fs.readObject(snippetsPath) + if CSON.isObjectPath(snippetsPath) and object = CSON.readObject(snippetsPath) snippets.push(object) else if object = fs.readPlist(snippetsPath) snippets.push(object) @@ -37,7 +38,7 @@ module.exports = for snippetsPath in fs.list(snippetsDirPath) continue if fs.base(snippetsPath).indexOf('.') is 0 try - snippets.push(fs.readObject(snippetsPath)) + snippets.push(CSON.readObject(snippetsPath)) catch e console.warn "Error reading snippets file '#{snippetsPath}'" @snippetsLoaded(snippets) diff --git a/src/packages/snippets/lib/snippets.coffee b/src/packages/snippets/lib/snippets.coffee index 74e02cae9..ee89f587b 100644 --- a/src/packages/snippets/lib/snippets.coffee +++ b/src/packages/snippets/lib/snippets.coffee @@ -4,6 +4,7 @@ _ = nodeRequire 'underscore' SnippetExpansion = require './snippet-expansion' Snippet = require './snippet' LoadSnippetsTask = require './load-snippets-task' +CSON = require 'cson' module.exports = snippetsByExtension: {} @@ -28,7 +29,7 @@ module.exports = loadFile: (snippetsPath) -> try - snippets = fs.readObject(snippetsPath) + snippets = CSON.readObject(snippetsPath) catch e console.warn "Error reading snippets file '#{snippetsPath}'" @add(snippets) diff --git a/src/stdlib/cson.coffee b/src/stdlib/cson.coffee index 30402be5a..c723a558b 100644 --- a/src/stdlib/cson.coffee +++ b/src/stdlib/cson.coffee @@ -1,6 +1,26 @@ _ = nodeRequire 'underscore' +fs = require 'fs' module.exports = + isObjectPath: (path) -> + extension = fs.extension(path) + extension is '.cson' or extension is '.json' + + readObject: (path) -> + contents = fs.read(path) + if fs.extension(path) is '.cson' + CoffeeScript = nodeRequire 'coffee-script' + CoffeeScript.eval(contents, bare: true) + else + JSON.parse(contents) + + writeObject: (path, object) -> + if fs.extension(path) is '.cson' + content = @stringify(object) + else + content = JSON.stringify(object, undefined, 2) + fs.write(path, "#{content}\n") + stringifyIndent: (level=0) -> _.multiplyString(' ', Math.max(level, 0)) stringifyString: (string) -> diff --git a/src/stdlib/fs.coffee b/src/stdlib/fs.coffee index aef59bdf6..bebe835fe 100644 --- a/src/stdlib/fs.coffee +++ b/src/stdlib/fs.coffee @@ -212,26 +212,6 @@ module.exports = base = @base(path, extension).toLowerCase() base is 'readme' and (extension is '' or @isMarkdownExtension(extension)) - isObjectPath: (path) -> - extension = @extension(path) - extension is '.cson' or extension is '.json' - - readObject: (path) -> - contents = @read(path) - if @extension(path) is '.cson' - CoffeeScript = nodeRequire 'coffee-script' - CoffeeScript.eval(contents, bare: true) - else - JSON.parse(contents) - - writeObject: (path, object) -> - if @extension(path) is '.cson' - CSON = require 'cson' - content = CSON.stringify(object) - else - content = JSON.stringify(object, undefined, 2) - @write(path, "#{content}\n") - readPlist: (path) -> plist = require 'plist' object = null