-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
cli.js
executable file
·67 lines (60 loc) · 1.61 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
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env node
import process from 'node:process';
import meow from 'meow';
import replaceInFiles from './api.js';
const cli = meow(`
Usage
$ replace-in-files <files…>
Options
--regex Regex pattern to find (Can be set multiple times)
--string String to find (Can be set multiple times)
--replacement Replacement string (Required)
--ignore-case Search case-insensitively
--no-glob Disable globbing
Examples
$ replace-in-files --string='horse' --regex='unicorn|rainbow' --replacement='🦄' foo.md
$ replace-in-files --regex='v\\d+\\.\\d+\\.\\d+' --replacement=v$npm_package_version foo.css
$ replace-in-files --string='blob' --replacement='blog' 'some/**/[gb]lob/*' '!some/glob/foo'
You can use the same replacement patterns as with \`String#replace()\`, like \`$&\`.
`, {
importMeta: import.meta,
flags: {
regex: {
type: 'string',
isMultiple: true,
},
string: {
type: 'string',
isMultiple: true,
},
replacement: {
type: 'string',
isRequired: true,
},
ignoreCase: {
type: 'boolean',
default: false,
},
glob: {
type: 'boolean',
default: true,
},
},
});
if (cli.input.length === 0) {
console.error('Specify one or more file paths');
process.exit(1);
}
if (!cli.flags.regex && !cli.flags.string) {
console.error('Specify at least `--regex` or `--string`');
process.exit(1);
}
await replaceInFiles(cli.input, {
find: [
...cli.flags.string,
...cli.flags.regex.map(regexString => new RegExp(regexString, 'g')),
],
replacement: cli.flags.replacement,
ignoreCase: cli.flags.ignoreCase,
glob: cli.flags.glob,
});