Skip to content

Commit de017e9

Browse files
committed
Fix: multiple files with dynamic replacement
1 parent 6d407ec commit de017e9

File tree

15 files changed

+122
-2782
lines changed

15 files changed

+122
-2782
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ node_js:
66
- "7"
77
- "6"
88

9-
9+
install: npm install
1010

1111
script:
1212
- npm run test-format # is formatting OK

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Flag | Effect
125125
`-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]
126126
`-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]
127127
`-R` | **`--replacement-pipe`** Replacement will be piped in. You still need to provide a dummy value (like `_`) as replacement parameter. [boolean]
128-
`-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]
128+
`-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]
129129
`-h` | **`--help`** Display help. [boolean]
130130
## Good to know
131131

bin/ES6/cli.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,13 @@ const yargs = require('yargs')
9999
`At some point, the $ char _will_ give you a headache when used from the command line, so use €0, €1, €2 €3 ... instead. ` +
100100
`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. ` +
101101
`\nThe code has access to the following variables: ` +
102-
`\n'_fs' from node, ` +
103-
`\n'_globs' from npm, ` +
102+
`\n'require' from node, ` +
103+
`\n'fs' from node, ` +
104+
`\n'globs' from npm, ` +
104105
`\n'_cwd' current working dir, ` +
105106
`\n'_pipe' is the data piped into the command (null if no piped data), ` +
106-
`\n'_find' is the final pattern searched for. ` +
107-
`\n'_text' is the full text being searched (= file contents or piped data). ` +
107+
`\n'_find' is the pattern searched for (the needle). ` +
108+
`\n'_text' is the full text being searched i.e. file content or piped data (the haystack). ` +
108109
`\nThe following values are also available if working on a file (if data is being piped they are all set to an empty string): ` +
109110
`\n'_file' is the full path of the active file being searched (including full filename), ` +
110111
`\n'_path' is the full path without filename of the active file being searched, ` +

bin/ES6/engine.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export function engine(config) {
99
step(config);
1010
config.pattern = getFinalPattern(config) || '';
1111
config.replacement = getFinalReplacement(config) || '';
12+
config.replacementOri = config.replacement;
1213
config.regex = getFinalRegex(config) || '';
1314
step(config);
1415
if (handlePipedData(config)) {
@@ -51,9 +52,11 @@ export function engine(config) {
5152
const _pipe = _config_rr.pipedData;
5253
const _text = _data_rr;
5354
const _find = _config_rr.pattern;
54-
const code_rr = _config_rr.replacement;
55+
const code_rr = _config_rr.replacementOri;
5556
const _cwd = process.cwd();
56-
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)');
57+
let _file = '', _path = '', _filename = '', _name = '', _ext = '', dynamicContent = new Function(
58+
//'require',
59+
'fs', 'globs', '_pipe', '_text', '_find', '_file', '_path', '_filename', '_name', '_ext', '_cwd', 'code_rr', 'return eval(code_rr)');
5760
if (!_config_rr.dataIsPiped) {
5861
_file = path.normalize(path.join(process.cwd(), _file_rr));
5962
let pathInfo = path.parse(_file);
@@ -64,7 +67,9 @@ export function engine(config) {
6467
}
6568
// Run only once if no captured groups (replacement cant change)
6669
if (!/\$\d/.test(_config_rr.replacement)) {
67-
_config_rr.replacement = dynamicContent(fs, globs, _pipe, _text, _find, _file, _path, _filename, _name, _ext, _cwd, code_rr);
70+
_config_rr.replacement = dynamicContent(
71+
//require,
72+
fs, globs, _pipe, _text, _find, _file, _path, _filename, _name, _ext, _cwd, code_rr);
6873
}
6974
else {
7075
// Captures groups present, so need to run once per match
@@ -75,7 +80,9 @@ export function engine(config) {
7580
for (var i = 0; i < arguments.length - 2; i++) {
7681
capturedGroups += 'var $' + i + '=' + JSON.stringify(arguments[i]) + '; ';
7782
}
78-
return dynamicContent(fs, globs, __pipe, __text, __find, __file, __path, __filename, __name, __ext, __cwd, capturedGroups + __code_rr);
83+
return dynamicContent(
84+
//require,
85+
fs, globs, __pipe, __text, __find, __file, __path, __filename, __name, __ext, __cwd, capturedGroups + __code_rr);
7986
};
8087
}
8188
}

bin/rexreplace.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)