-
Notifications
You must be signed in to change notification settings - Fork 66
/
cli.js
executable file
·55 lines (44 loc) · 1.51 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env node
import yargs from 'yargs'
import {hideBin} from 'yargs/helpers'
import {replaceInFileSync} from '../src/replace-in-file.js'
import {loadConfig, combineConfig} from '../src/helpers/config.js'
import {errorHandler, successHandler} from '../src/helpers/handlers.js'
/**
* Main routine
*/
async function main() {
//Extract parameters
const argv = yargs(hideBin(process.argv)).argv
const {configFile} = argv
//Verify arguments
if (argv._.length < 3 && !configFile) {
throw new Error('Replace in file needs at least 3 arguments')
}
//Load config and combine with passed arguments
const config = configFile ? await loadConfig(configFile) : {}
const options = combineConfig(config, argv)
//Extract settings
const {from, to, files, isRegex, verbose, quiet} = options
//Single star globs already get expanded in the command line
options.files = files.reduce((files, file) => {
return files.concat(file.split(','))
}, [])
//If the isRegex flag is passed, convert the from parameter to a RegExp object
if (isRegex && typeof from === 'string') {
const flags = from.replace(/.*\/([gimyus]*)$/, '$1')
const pattern = from.replace(new RegExp(`^/(.*?)/${flags}$`), '$1')
options.from = new RegExp(pattern, flags)
}
//Log
if (!quiet) {
console.log(`Replacing '${from}' with '${to}'`)
}
//Replace
const results = replaceInFileSync(options)
if (!quiet) {
successHandler(results, verbose)
}
}
//Call main routine
main().catch(error => errorHandler(error))