mirror of
https://github.com/zhigang1992/replace-in-file.git
synced 2026-04-30 21:52:29 +08:00
Add config file support (#16)
* Add config file support * Allow passing files to config * Fixes #15
This commit is contained in:
committed by
Adam Reis
parent
acf5fc7815
commit
3a3ca2ce59
48
bin/cli.js
48
bin/cli.js
@@ -4,22 +4,60 @@
|
|||||||
/**
|
/**
|
||||||
* Dependencies
|
* Dependencies
|
||||||
*/
|
*/
|
||||||
|
const path = require('path');
|
||||||
const chalk = require('chalk');
|
const chalk = require('chalk');
|
||||||
const argv = require('yargs').argv;
|
const argv = require('yargs').argv;
|
||||||
const replace = require('../lib/replace-in-file');
|
const replace = require('../lib/replace-in-file');
|
||||||
|
|
||||||
//Verify arguments
|
//Verify arguments
|
||||||
if (argv._.length < 3) {
|
if (argv._.length < 3 && !argv.config) {
|
||||||
console.error(chalk.red('Replace in file needs at least 3 arguments'));
|
console.error(chalk.red('Replace in file needs at least 3 arguments'));
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Collect main arguments
|
let from, to, files;
|
||||||
let from = argv._.shift();
|
|
||||||
const to = argv._.shift();
|
// If --config is set, load config file
|
||||||
|
if (argv.config) {
|
||||||
|
//Read config file
|
||||||
|
let config;
|
||||||
|
try {
|
||||||
|
config = require(path.join(process.cwd(), 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
//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(','));
|
return files.concat(file.split(','));
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user