Skip to content

Commit

Permalink
Fix: multiple files with dynamic replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasrw committed Aug 13, 2018
1 parent 6d407ec commit de017e9
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 2,782 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ node_js:
- "7"
- "6"


install: npm install

script:
- npm run test-format # is formatting OK
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Flag | Effect
`-m` | **`--output-match`** Output each match on a new line. Will not replace any content but you still need to provide a dummy value (like `_`) as replacement parameter. If search pattern does not contain matching groups the full match will be outputted. If search pattern does contain matching groups only matching groups will be outputted (same line with no delimiter). [boolean]
`-T` | **`--trim-pipe`** Trim piped data before processing. If piped data only consists of chars that can be trimmed (new line, space, tabs...) it will become an empty string. [boolean]
`-R` | **`--replacement-pipe`** Replacement will be piped in. You still need to provide a dummy value (like `_`) as replacement parameter. [boolean]
`-j` | **`--replacement-js`** Treat replacement as javascript source code. The statement from the last expression will become the replacement string. Purposefully implemented the most insecure way possible to remove _any_ incentive to consider running code from an untrusted person - that be anyone that is not yourself. The full match will be available as a javascript variable named $0 while each captured group will be available as $1, $2, $3, ... and so on. At some point, the $ char _will_ give you a headache when used from the command line, so use €0, €1, €2 €3 ... instead. If the javascript source code references to the full match or a captured group the code will run once per match. Otherwise, it will run once per file. The code has access to the following variables: `_fs` from node, `_globs` from npm, `_cwd` current working dir, `_pipe` is the piped data into the command (null if no piped data), `_find` is the final pattern searched for. `_text` is the full text being searched (= file contents or piped data). The following values are also available if working on a file (if data is being piped they are all set to an empty string): `_file` is the full path of the active file being searched (including full filename), `_path` is the full path without filename of the active file being searched, `_filename` is the full filename of the active file being searched, `_name` is the filename of the active file being searched with no extension, `_ext` is the extension of the filename including leading dot. [boolean]
`-j` | **`--replacement-js`** Treat replacement as javascript source code. The statement from the last expression will become the replacement string. Purposefully implemented the most insecure way possible to remove _any_ incentive to consider running code from an untrusted person - that be anyone that is not yourself. The full match will be available as a javascript variable named $0 while each captured group will be available as $1, $2, $3, ... and so on. At some point, the $ char _will_ give you a headache when used from the command line, so use €0, €1, €2 €3 ... instead. If the javascript source code references to the full match or a captured group the code will run once per match. Otherwise, it will run once per file. The code has access to the following variables: `require` from node, `fs` from node, `globs` from npm, `_cwd` current working dir, `_pipe` is the data piped into the command (null if no piped data), `_find` is the pattern searched for (the needle). `_text` is the full text being searched i.e. file content or piped data (the haystack). The following values are also available if working on a file (if data is being piped they are all set to an empty string): `_file` is the full path of the active file being searched (including full filename), `_path` is the full path without filename of the active file being searched, `_filename` is the full filename of the active file being searched, `_name` is the filename of the active file being searched with no extension, `_ext` is the extension of the filename including leading dot. [boolean]
`-h` | **`--help`** Display help. [boolean]
## Good to know

Expand Down
9 changes: 5 additions & 4 deletions bin/ES6/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,13 @@ const yargs = require('yargs')
`At some point, the $ char _will_ give you a headache when used from the command line, so use €0, €1, €2 €3 ... instead. ` +
`If the javascript source code references to the full match or a captured group the code will run once per match. Otherwise, it will run once per file. ` +
`\nThe code has access to the following variables: ` +
`\n'_fs' from node, ` +
`\n'_globs' from npm, ` +
`\n'require' from node, ` +
`\n'fs' from node, ` +
`\n'globs' from npm, ` +
`\n'_cwd' current working dir, ` +
`\n'_pipe' is the data piped into the command (null if no piped data), ` +
`\n'_find' is the final pattern searched for. ` +
`\n'_text' is the full text being searched (= file contents or piped data). ` +
`\n'_find' is the pattern searched for (the needle). ` +
`\n'_text' is the full text being searched i.e. file content or piped data (the haystack). ` +
`\nThe following values are also available if working on a file (if data is being piped they are all set to an empty string): ` +
`\n'_file' is the full path of the active file being searched (including full filename), ` +
`\n'_path' is the full path without filename of the active file being searched, ` +
Expand Down
15 changes: 11 additions & 4 deletions bin/ES6/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function engine(config) {
step(config);
config.pattern = getFinalPattern(config) || '';
config.replacement = getFinalReplacement(config) || '';
config.replacementOri = config.replacement;
config.regex = getFinalRegex(config) || '';
step(config);
if (handlePipedData(config)) {
Expand Down Expand Up @@ -51,9 +52,11 @@ export function engine(config) {
const _pipe = _config_rr.pipedData;
const _text = _data_rr;
const _find = _config_rr.pattern;
const code_rr = _config_rr.replacement;
const code_rr = _config_rr.replacementOri;
const _cwd = process.cwd();
let _file = '', _path = '', _filename = '', _name = '', _ext = '', dynamicContent = new Function('_fs', '_globs', '_pipe', '_text', '_find', '_file', '_path', '_filename', '_name', '_ext', '_cwd', 'code_rr', 'return eval(code_rr)');
let _file = '', _path = '', _filename = '', _name = '', _ext = '', dynamicContent = new Function(
//'require',
'fs', 'globs', '_pipe', '_text', '_find', '_file', '_path', '_filename', '_name', '_ext', '_cwd', 'code_rr', 'return eval(code_rr)');
if (!_config_rr.dataIsPiped) {
_file = path.normalize(path.join(process.cwd(), _file_rr));
let pathInfo = path.parse(_file);
Expand All @@ -64,7 +67,9 @@ export function engine(config) {
}
// Run only once if no captured groups (replacement cant change)
if (!/\$\d/.test(_config_rr.replacement)) {
_config_rr.replacement = dynamicContent(fs, globs, _pipe, _text, _find, _file, _path, _filename, _name, _ext, _cwd, code_rr);
_config_rr.replacement = dynamicContent(
//require,
fs, globs, _pipe, _text, _find, _file, _path, _filename, _name, _ext, _cwd, code_rr);
}
else {
// Captures groups present, so need to run once per match
Expand All @@ -75,7 +80,9 @@ export function engine(config) {
for (var i = 0; i < arguments.length - 2; i++) {
capturedGroups += 'var $' + i + '=' + JSON.stringify(arguments[i]) + '; ';
}
return dynamicContent(fs, globs, __pipe, __text, __find, __file, __path, __filename, __name, __ext, __cwd, capturedGroups + __code_rr);
return dynamicContent(
//require,
fs, globs, __pipe, __text, __find, __file, __path, __filename, __name, __ext, __cwd, capturedGroups + __code_rr);
};
}
}
Expand Down
22 changes: 0 additions & 22 deletions bin/rexreplace.js

This file was deleted.

Loading

0 comments on commit de017e9

Please sign in to comment.