Allowing either flat or nested .jsbeautifyrc schemas

This commit is contained in:
Gvn Lazar Suntop 2014-05-17 18:28:11 -07:00
parent 50de6641bd
commit cf1ea6db16
3 changed files with 38 additions and 15 deletions

View File

@ -7,9 +7,9 @@ var beautifyHTML = require('js-beautify').html;
var beautifyCSS = require('js-beautify').css;
var fs = require('fs');
var path = require('path');
var cc = require('config-chain');
var nopt = require('nopt');
var extend = require('extend');
var _ = require('lodash');
// TODO: Copied from jsbeautify, please update it from time to time
var knownOpts = {
@ -121,20 +121,43 @@ function beautify() {
'', '.jsbeautifyrc'))) === editedFilePath) {
rcInHomePath = null;
}
var cfg = cc(
cleanOptions(cc.env('jsbeautify_'), knownOpts),
rcInRecursiveCwd,
rcInHomePath
).list;
// cc(...).snapshot SHOULD contain the same what I construct below,
// however I have not the faintest idea why it doesn't work here.
// It works at js-beautify cli, but not here. Weird.
var collectedConfig = {};
for (var idx = cfg.length - 1; idx >= 0; idx--) {
collectedConfig = extend(cfg[idx], collectedConfig);
var externalOptions;
if (rcInRecursiveCwd) {
externalOptions = JSON.parse(fs.readFileSync(rcInRecursiveCwd, {
encoding: 'utf8'
}));
} else if (rcInHomePath) {
externalOptions = JSON.parse(fs.readFileSync(rcInHomePath, {
encoding: 'utf8'
}));
} else {
externalOptions = {};
}
// Override the indenting options from the editor
var containsNested = false;
var collectedConfig = {};
var key;
// Check to see if config file uses nested object format to split up js/css/html options
for (key in externalOptions) {
if (typeof externalOptions[key] === 'object') {
containsNested = true;
}
}
// Create a flat object of config options if nested format was used
if (!containsNested) {
collectedConfig = externalOptions;
} else {
for (key in externalOptions) {
_.merge(collectedConfig, externalOptions[key]);
}
}
beautifyOptions = extend(collectedConfig, beautifyOptions);
beautifyOptions = cleanOptions(beautifyOptions, knownOpts);
if (isSelection) {
text = editor.getSelectedText();

View File

@ -15,10 +15,10 @@
"atom": ">0.50.0"
},
"dependencies": {
"config-chain": "^1.1.8",
"emissary": "^1.0.0",
"extend": "^1.2.1",
"js-beautify": "~1.4.2",
"nopt": "^2.2.1"
"nopt": "^2.2.1",
"lodash": "2.4.1"
}
}