From b8986b60fee5e2748aed81370d7a4dec9cb7c79c Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Sat, 17 Oct 2015 10:43:37 +0700 Subject: [PATCH] Allow JSON files and node modules as config files on CLI --- cli.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/cli.js b/cli.js index 0ee883e..79b5adc 100755 --- a/cli.js +++ b/cli.js @@ -195,17 +195,30 @@ cli.main(function(args, options) { } if (options['config-file']) { + var fileOptions; + var fileOptionsPath = path.resolve(options['config-file']); try { - var fileOptions = JSON.parse(fs.readFileSync(path.resolve(options['config-file']), 'utf8')); - if ((fileOptions !== null) && (typeof fileOptions === 'object')) { - minifyOptions = fileOptions; - } + fs.accessSync(fileOptionsPath, fs.R_OK); } catch (e) { - cli.fatal('Cannot read the specified config file'); + cli.fatal('The specified config file doesn’t exist or is unreadable:\n' + fileOptionsPath); + } + try { + fileOptions = JSON.parse(fs.readFileSync(fileOptionsPath), 'utf8'); + } + catch (je) { + try { + fileOptions = require(fileOptionsPath); + } + catch (ne) { + cli.fatal('Cannot read the specified config file. \nAs JSON: ' + je.message + '\nAs module: ' + ne.message); + } } - } + if (fileOptions && typeof fileOptions === 'object') { + minifyOptions = fileOptions; + } + } mainOptionKeys.forEach(function(key) { var paramKey = changeCase.paramCase(key); var value = options[paramKey]; -- 2.34.1