Skip to content

Commit

Permalink
Exit on regex error
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreisnz committed Feb 21, 2017
1 parent fbc7de4 commit faf1534
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ Via CLI:
replace-in-file from to some/file.js,some/**/glob.js [--isRegex]
```

The options `allowEmptyPaths` and `encoding` are supported in the CLI.
In addition, the CLI supports the `verbose` option to list the changed files.
The flags `allowEmptyPaths` and `encoding` are supported in the CLI.
In addition, the CLI supports the `verbose` flag to list the changed files.

Multiple files or globs can be replaced by providing a comma separated list.

A regular expression may be used for the `from` parameter by specifying the `--isRegex` option.
A regular expression may be used for the `from` parameter by specifying the `--isRegex` flag.

## License
(MIT License)
Expand Down
8 changes: 5 additions & 3 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ const files = argv._.reduce((files, file) => {
return files.concat(file.split(','));
}, []);

// If the --isRegex flag is passed, send the 'from' parameter
// to the lib as a RegExp object
//If the --isRegex flag is passed, send the 'from' parameter
//to the lib as a RegExp object
if (argv.isRegex) {
const flags = from.replace(/.*\/([gimy]*)$/, '$1');
const pattern = from.replace(new RegExp(`^/(.*?)/${flags}$`), '$1');
try {
from = new RegExp(pattern, flags);
}
catch (error) {
console.error('Could not create RegExp from \'from\' parameter', error);
console.error('Could not create RegExp from \'from\' parameter:');
console.error(error);
process.exit(1);
}
}

Expand Down

0 comments on commit faf1534

Please sign in to comment.