-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for passing Regex into CLI #14
Changes from 1 commit
fbcdc79
97ddf18
2339f3d
e4952af
299767e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,9 +14,18 @@ if (argv._.length < 3) { | |
process.exit(1); | ||
} | ||
|
||
// Parse from/to as either a Regex or a string | ||
const isRegexMatch = /.*\/([gimy]*)$/; | ||
const getRegexOrString = value => { | ||
if(!isRegexMatch.test(value)) return value; | ||
const flags = value.replace(/.*\/([gimy]*)$/, '$1'); | ||
const pattern = value.replace(new RegExp('^/(.*?)/'+flags+'$'), '$1'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code block will not pass the linter, could you run ESLint on the code to fix style inconsistencies please? |
||
return new RegExp(pattern, flags); | ||
}; | ||
|
||
//Collect main arguments | ||
const from = argv._.shift(); | ||
const to = argv._.shift(); | ||
const from = getRegexOrString(argv._.shift()); | ||
const to = getRegexOrString(argv._.shift()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
//Single star globs already get expanded in the command line | ||
const files = argv._.reduce((files, file) => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slightly worried that this pattern might incorrectly identify for example paths as a regex. E.g.
/some-path/g
would match, but it could be a path string rather than a regex. Perhaps it'd be better to include a flag if you are passing in a regular expression as thefrom
parameter, to be explicit and prevent accidental regex matches.I'd propose the flag
isRegex
for this purpose.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good point, thanks for pointing this out. So should we use:
replace-in-file [-regex] from to some/file.js,some/**/glob.js
replace-in-file from [-isRegex] to some/file.js,some/**/glob.js
I'd hoped I could add array support onto all the parameters, but might be a bit too complicated for CLI options.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say:
Where
from
then is the regular expression string.Yes, agreed, the CLI and arrays is a bit hard. I figured at least to use the comma separated list for the files would be most useful.