From 2f92c2bc7883b5050303d141458c3957114a31d7 Mon Sep 17 00:00:00 2001 From: RyanZim Date: Sat, 25 Feb 2017 20:40:36 -0500 Subject: [PATCH 1/2] Add config file support Fixes #15 --- bin/cli.js | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index edc02d2..507aece 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -4,19 +4,42 @@ /** * Dependencies */ +const path = require('path'); const chalk = require('chalk'); const argv = require('yargs').argv; const replace = require('../lib/replace-in-file'); //Verify arguments -if (argv._.length < 3) { +if (argv._.length < 3 && !argv.config) { console.error(chalk.red('Replace in file needs at least 3 arguments')); process.exit(1); } -//Collect main arguments -let from = argv._.shift(); -const to = argv._.shift(); +let from; +let to; + +if (argv.config) { + if (!argv.length) { + console.error(chalk.red('Must pass a list of files')); + process.exit(1); + } + //Read config file + let config; + try { + config = require(path.join(process.cwd(), argv.config)); + } + catch (e) { + console.error(chalk.red('Cannot load config file')); + process.exit(1); + } + from = config.from; + to = config.to; +} +else { + //Collect main arguments + from = argv._.shift(); + to = argv._.shift(); +} //Single star globs already get expanded in the command line const files = argv._.reduce((files, file) => { From 19de14e0680c634270c357f1afd9b52bb21242c8 Mon Sep 17 00:00:00 2001 From: RyanZim Date: Thu, 2 Mar 2017 18:47:19 -0500 Subject: [PATCH 2/2] Allow passing files to config --- bin/cli.js | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index 507aece..56044fc 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -15,14 +15,10 @@ if (argv._.length < 3 && !argv.config) { process.exit(1); } -let from; -let to; +let from, to, files; +// If --config is set, load config file if (argv.config) { - if (!argv.length) { - console.error(chalk.red('Must pass a list of files')); - process.exit(1); - } //Read config file let config; try { @@ -30,19 +26,38 @@ if (argv.config) { } catch (e) { console.error(chalk.red('Cannot load config file')); + console.error(e); process.exit(1); } from = config.from; to = config.to; + if (typeof config.files === 'string') { + config.files = [config.files]; + } + files = config.files; } -else { - //Collect main arguments + +if (!from === undefined) { from = argv._.shift(); +} +if (!to === undefined) { to = argv._.shift(); } +if (!files) { + files = argv._; +} + +if (!from === undefined || !to === undefined) { + console.error(chalk.red('Must set from & to options')); + process.exit(1); +} +if (!files) { + console.error(chalk.red('Must pass a list of files')); + process.exit(1); +} //Single star globs already get expanded in the command line -const files = argv._.reduce((files, file) => { +files = files.reduce((files, file) => { return files.concat(file.split(',')); }, []);