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 beautifyCSS = require('js-beautify').css;
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
var cc = require('config-chain');
var nopt = require('nopt'); var nopt = require('nopt');
var extend = require('extend'); var extend = require('extend');
var _ = require('lodash');
// TODO: Copied from jsbeautify, please update it from time to time // TODO: Copied from jsbeautify, please update it from time to time
var knownOpts = { var knownOpts = {
@ -121,20 +121,43 @@ function beautify() {
'', '.jsbeautifyrc'))) === editedFilePath) { '', '.jsbeautifyrc'))) === editedFilePath) {
rcInHomePath = null; rcInHomePath = null;
} }
var cfg = cc(
cleanOptions(cc.env('jsbeautify_'), knownOpts), var externalOptions;
rcInRecursiveCwd,
rcInHomePath if (rcInRecursiveCwd) {
).list; externalOptions = JSON.parse(fs.readFileSync(rcInRecursiveCwd, {
// cc(...).snapshot SHOULD contain the same what I construct below, encoding: 'utf8'
// however I have not the faintest idea why it doesn't work here. }));
// It works at js-beautify cli, but not here. Weird. } else if (rcInHomePath) {
var collectedConfig = {}; externalOptions = JSON.parse(fs.readFileSync(rcInHomePath, {
for (var idx = cfg.length - 1; idx >= 0; idx--) { encoding: 'utf8'
collectedConfig = extend(cfg[idx], collectedConfig); }));
} 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 = extend(collectedConfig, beautifyOptions);
beautifyOptions = cleanOptions(beautifyOptions, knownOpts);
if (isSelection) { if (isSelection) {
text = editor.getSelectedText(); text = editor.getSelectedText();

View File

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