Skip to content

Commit

Permalink
Allowing either flat or nested .jsbeautifyrc schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
gvn committed May 18, 2014
1 parent 50de664 commit cf1ea6d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
File renamed without changes.
47 changes: 35 additions & 12 deletions lib/atom-beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 externalOptions;

if (rcInRecursiveCwd) {
externalOptions = JSON.parse(fs.readFileSync(rcInRecursiveCwd, {
encoding: 'utf8'
}));
} else if (rcInHomePath) {
externalOptions = JSON.parse(fs.readFileSync(rcInHomePath, {
encoding: 'utf8'
}));
} else {
externalOptions = {};
}

var containsNested = false;
var collectedConfig = {};
for (var idx = cfg.length - 1; idx >= 0; idx--) {
collectedConfig = extend(cfg[idx], 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]);
}
}
// Override the indenting options from the editor

beautifyOptions = extend(collectedConfig, beautifyOptions);
beautifyOptions = cleanOptions(beautifyOptions, knownOpts);

if (isSelection) {
text = editor.getSelectedText();
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit cf1ea6d

Please sign in to comment.