-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·83 lines (75 loc) · 1.97 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env node
/* eslint-disable no-undef */
import meow from 'meow'
import process from 'node:process'
import { replaceRegex } from './dist/index.js'
const cli = meow(
`
Usage
$ replace-regex <files…>
Options
--from Regex pattern or string to find (Can be set multiple times)
--to Replacement string or function (Required)
--dry Dry run (do not actually replace, just show what would be replaced)
--no-glob Disable globbing
--ignore Ignore files matching this pattern (Can be set multiple times)
--ignore-case Search case-insensitively
Examples
$ replace-regex --from='fox' --to='🦊' foo.md
$ replace-regex --from='v\\d+\\.\\d+\\.\\d+' --to='v$npm_package_version' foo.css
$ replace-regex --from='blob' --to='blog' 'some/**/[gb]lob/*' '!some/glob/foo'
`,
{
importMeta: import.meta,
flags: {
from: {
type: 'string',
isMultiple: true,
},
to: {
type: 'string',
isRequired: true,
},
dry: {
type: 'boolean',
default: false,
},
glob: {
type: 'boolean',
default: true,
},
ignore: {
type: 'string',
isMultiple: true,
},
ignoreCase: {
type: 'boolean',
default: false,
},
},
},
)
if (cli.input.length === 0) {
console.error('Specify one or more file paths')
process.exit(1)
}
if (!cli.flags.from || cli.flags.from.length === 0) {
console.error('Specify at least `--from`')
process.exit(1)
}
const replaceOptions = {
files: cli.input,
from: cli.flags.from,
to: cli.flags.to,
dry: cli.flags.dry,
disableGlobs: !cli.flags.glob,
ignore: cli.flags.ignore,
ignoreCase: cli.flags.ignoreCase,
}
const results = await replaceRegex(replaceOptions)
results.forEach((result) => {
const { file, replaceCount, matchCount } = result
console.log(`file: ${file}
replace count: ${replaceCount}
match count: ${matchCount}`)
})