From cf1ea6db16d2188ddef5e6c1fb4d000cab67f99c Mon Sep 17 00:00:00 2001 From: Gvn Lazar Suntop Date: Sat, 17 May 2014 18:28:11 -0700 Subject: [PATCH] Allowing either flat or nested .jsbeautifyrc schemas --- .jsbeautifierrc => .jsbeautifyrc | 0 lib/atom-beautify.js | 49 +++++++++++++++++++++++--------- package.json | 4 +-- 3 files changed, 38 insertions(+), 15 deletions(-) rename .jsbeautifierrc => .jsbeautifyrc (100%) diff --git a/.jsbeautifierrc b/.jsbeautifyrc similarity index 100% rename from .jsbeautifierrc rename to .jsbeautifyrc diff --git a/lib/atom-beautify.js b/lib/atom-beautify.js index 155d5f2..c94ab59 100644 --- a/lib/atom-beautify.js +++ b/lib/atom-beautify.js @@ -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(); diff --git a/package.json b/package.json index ee42c61..880b4cf 100644 --- a/package.json +++ b/package.json @@ -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" } }