-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
36 lines (31 loc) · 1005 Bytes
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env node
const packageJson = require('./package.json');
const regexReplace = require('./index');
const argv = process.argv;
const [ cmd, cmdFile, searchString, replaceString, path, ...options ] = argv;
const isVersionFlag = (argv[2] === '-v');
const customOptions = options.reduce((acc, value, key) => {
switch (value) {
case '--filename':
case '--filenames': {
acc.filenamesOnly = true;
break;
}
case '--filecontent':
case '--filecontents': {
acc.fileContentsOnly = true;
break;
}
default: {
break;
}
}
return acc;
}, {});
if((searchString && replaceString && path) && !isVersionFlag) {
regexReplace(searchString, replaceString, path, customOptions);
} else if (!isVersionFlag){
console.error('missing required arguments: (<searchString>, <replaceString>, <path>, [options])');
} else if(isVersionFlag) {
console.log(packageJson.version);
}