Merge pull request #9948 from dirk-thomas/order_config_cson

save config.cson in alphabetic order
This commit is contained in:
Max Brunsfeld 2015-12-07 11:19:56 -08:00
commit 5825fb51b5
2 changed files with 28 additions and 0 deletions

View File

@ -679,6 +679,26 @@ describe "Config", ->
writtenConfig = CSON.writeFileSync.argsForCall[0][1]
expect(writtenConfig).toEqual '*': atom.config.settings
it 'writes properties in alphabetical order', ->
atom.config.set('foo', 1)
atom.config.set('bar', 2)
atom.config.set('baz.foo', 3)
atom.config.set('baz.bar', 4)
CSON.writeFileSync.reset()
atom.config.save()
expect(CSON.writeFileSync.argsForCall[0][0]).toBe atom.config.configFilePath
writtenConfig = CSON.writeFileSync.argsForCall[0][1]
expect(writtenConfig).toEqual '*': atom.config.settings
expectedKeys = ['bar', 'baz', 'foo']
foundKeys = (key for key of writtenConfig['*'] when key in expectedKeys)
expect(foundKeys).toEqual expectedKeys
expectedKeys = ['bar', 'foo']
foundKeys = (key for key of writtenConfig['*']['baz'] when key in expectedKeys)
expect(foundKeys).toEqual expectedKeys
describe "when ~/.atom/config.json doesn't exist", ->
it "writes any non-default properties to ~/.atom/config.cson", ->
atom.config.set("a.b.c", 1)

View File

@ -827,6 +827,7 @@ class Config
allSettings = {'*': @settings}
allSettings = _.extend allSettings, @scopedSettingsStore.propertiesForSource(@getUserConfigPath())
allSettings = sortObject(allSettings)
try
CSON.writeFileSync(@configFilePath, allSettings)
catch error
@ -1190,6 +1191,13 @@ Config.addSchemaEnforcers
isPlainObject = (value) ->
_.isObject(value) and not _.isArray(value) and not _.isFunction(value) and not _.isString(value) and not (value instanceof Color)
sortObject = (value) ->
return value unless isPlainObject(value)
result = {}
for key in Object.keys(value).sort()
result[key] = sortObject(value[key])
result
withoutEmptyObjects = (object) ->
resultObject = undefined
if isPlainObject(object)