Skip to content

Commit

Permalink
Exclude paths by starting glob with '^' or '!'
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasrw committed Jul 27, 2023
1 parent c9c87f3 commit befbcbe
Show file tree
Hide file tree
Showing 10 changed files with 1,232 additions and 703 deletions.
3 changes: 1 addition & 2 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ test/*
src/*
gfx/*
rollup.config.js
greenkeeper.json
tmp/*
package-lock.json
.*/
*.log
*.txt
*.file
*.lock
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ _.oO(What should "sed" have looked like by now?)_
### Future ideas
- Default to RegExmoji lax syntax
- Support RegExmoji strict syntax
- Add support to require hjson, jsonh, yaml, ini files directly
- Test-run with info outputted about what will happen (sets -t and does not change anything)
- Let search and replace be within the names of the files (ask for overwriting. -Y = no questions)
Expand Down
2 changes: 1 addition & 1 deletion bin/ES6/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function unescapeString(str = '') {
});
let pipeInUse = false;
let pipeData = '';
config.files = yargs.argv._;
config.globs = yargs.argv._;
config.pipedData = null;
config.showHelp = yargs.showHelp;
config.pattern = pattern;
Expand Down
26 changes: 22 additions & 4 deletions bin/ES6/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function engine(config = { engine: 'V8' }) {
if (handlePipedData(config)) {
return doReplacement('Piped data', config, config.pipedData);
}
config.files = globs.sync(config.files);
config.files = globs2paths(config.globs);
if (!config.files.length) {
return error(config.files.length + ' files found');
}
Expand Down Expand Up @@ -68,7 +68,7 @@ export function engine(config = { engine: 'V8' }) {
return;
}
// Release the memory while storing files
_data_rr = undefined;
_data_rr = '';
debug('Write new content to: ' + _file_rr);
// Write directly to the same file (if the process is killed all new and old data is lost)
if (_config_rr.voidBackup) {
Expand Down Expand Up @@ -121,7 +121,7 @@ export function engine(config = { engine: 'V8' }) {
}
function handlePipedData(config) {
step('Check Piped Data');
if (config.files.length) {
if (config.globs.length) {
if (!config.replacementJs) {
chat('Piped data never used.');
}
Expand Down Expand Up @@ -202,7 +202,7 @@ export function engine(config = { engine: 'V8' }) {
if (config.literal) {
pattern = pattern.replace(/[-\[\]{}()*+?.,\/\\^$|#\s]/g, '\\$&');
}
let regex = null;
let regex;
let flags = getFlags(config);
switch (config.engine) {
case 'V8':
Expand Down Expand Up @@ -339,3 +339,21 @@ function replacePlaceholders(str = '', conf) {
}
return str;
}
function globs2paths(_globs = []) {
const globsToInclude = [];
const globsToExclude = [];
_globs.filter(Boolean).forEach((glob) => {
if ('!' === glob[0] || '^' === glob[0]) {
globsToExclude.push(glob.slice(1));
}
else {
globsToInclude.push(glob);
}
});
let filesToInclude = globs.sync(globsToInclude);
if (globsToExclude.length) {
const filesToExclude = globs.sync(globsToExclude);
return filesToInclude.filter((el) => !filesToExclude.includes(el));
}
return filesToInclude;
}
32 changes: 26 additions & 6 deletions bin/rexreplace.cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
var path = require('path');
var globs = require('globs');
var now = new Date();
var version = '7.0.7';
var version = '7.1.1';
function engine(config) {
if ( config === void 0 ) config = { engine: 'V8' };

Expand All @@ -87,7 +87,7 @@
if (handlePipedData(config)) {
return doReplacement('Piped data', config, config.pipedData);
}
config.files = globs.sync(config.files);
config.files = globs2paths(config.globs);
if (!config.files.length) {
return error(config.files.length + ' files found');
}
Expand Down Expand Up @@ -139,7 +139,7 @@
return;
}
// Release the memory while storing files
_data_rr = undefined;
_data_rr = '';
debug('Write new content to: ' + _file_rr);
// Write directly to the same file (if the process is killed all new and old data is lost)
if (_config_rr.voidBackup) {
Expand Down Expand Up @@ -192,7 +192,7 @@
}
function handlePipedData(config) {
step('Check Piped Data');
if (config.files.length) {
if (config.globs.length) {
if (!config.replacementJs) {
chat('Piped data never used.');
}
Expand Down Expand Up @@ -275,7 +275,7 @@
if (config.literal) {
pattern = pattern.replace(/[-\[\]{}()*+?.,\/\\^$|#\s]/g, '\\$&');
}
var regex = null;
var regex;
var flags = getFlags(config);
switch (config.engine) {
case 'V8':
Expand Down Expand Up @@ -418,6 +418,26 @@
}
return str;
}
function globs2paths(_globs) {
if ( _globs === void 0 ) _globs = [];

var globsToInclude = [];
var globsToExclude = [];
_globs.filter(Boolean).forEach(function (glob) {
if ('!' === glob[0] || '^' === glob[0]) {
globsToExclude.push(glob.slice(1));
}
else {
globsToInclude.push(glob);
}
});
var filesToInclude = globs.sync(globsToInclude);
if (globsToExclude.length) {
var filesToExclude = globs.sync(globsToExclude);
return filesToInclude.filter(function (el) { return !filesToExclude.includes(el); });
}
return filesToInclude;
}

var assign;
var pattern, replacement;
Expand Down Expand Up @@ -625,7 +645,7 @@
});
var pipeInUse = false;
var pipeData = '';
config.files = yargs.argv._;
config.globs = yargs.argv._;
config.pipedData = null;
config.showHelp = yargs.showHelp;
config.pattern = pattern;
Expand Down
722 changes: 691 additions & 31 deletions bin/rexreplace.cli.min.js

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rexreplace",
"version": "7.0.7",
"version": "7.1.1",
"description": "Smoothly search & replace in files from CLI.",
"author": "Mathias Rangel Wulff",
"funding": {
Expand All @@ -23,14 +23,14 @@
"build": "yarn build-only",
"build-only": "tsc src/cli --outDir bin/ES6 -t ES6 && rollup -c",
"build-minify": "yarn build && yarn minify",
"minify": "echo '#!/usr/bin/env node' > bin/rexreplace.cli.min.js && google-closure-compiler --js=bin/rexreplace.cli.js --js_output_file=bin/rexreplace.cli.min.js",
"minify": "echo '#!/usr/bin/env node' > bin/rexreplace.cli.min.js && npx google-closure-compiler --js=bin/rexreplace.cli.js >> bin/rexreplace.cli.min.js",
"prebuild": "rm -fr bin && yarn format",
"test-js": "echo todo: async mocha",
"test-minify": "yarn build-minify && yarn test-cli && yarn test-js",
"test-cli": "npm uninstall -g rexreplace && npm -g install ./ && yarn test-cli-only",
"test-cli-only": "bash test/cli/run.sh",
"test-speed": "bash test/speed/run.sh",
"prepublishOnly": "yarn is-git-clean && git rebase origin && yarn test-minify && yarn load-options && yarn bump",
"prepublishOnly": "yarn is-git-clean && git fetch && git rebase origin && yarn test-minify && yarn load-options && yarn bump",
"postpublish": "git push --tag && git push && (open https://github.com/mathiasrw/rexreplace/releases || 1)",
"load-options": "rr -h | rr 'Options:(.+)Examples:' _ -ms | rr '\\n {26,}|\\n\\n *' ' ' | rr \"'\" '`' | rr '^ (-.+?), (--[^ ]+) *' '`€1` | **`€2`** ' | rr '(^---- . ----).+?(## Good to know)' '€1 + nl + pipe + nl + nl + €2' readme.md -jsT",
"test-format": "yarn prettier --list-different || (echo 'Please correct file formatting using `yarn format` and try again.' && exit 1)",
Expand All @@ -53,22 +53,21 @@
"@rollup/plugin-commonjs": "20.0.0",
"@rollup/plugin-node-resolve": "13.0.6",
"@rollup/plugin-replace": "3.0.0",
"@types/node": "16.11.4",
"@types/node": "20.4.5",
"assert": "^2.0.0",
"google-closure-compiler": "^20211006.0.0",
"magic-string": "^0.25.7",
"mocha": "9.1.3",
"prettier": "2.4.1",
"magic-string": "0.30.1",
"mocha": "10.2.0",
"prettier": "3.0.0",
"rollup": "2.58.0",
"rollup-plugin-closure-compiler-js": "^1.0.6",
"rollup-plugin-filesize": "9.1.1",
"rollup-plugin-hashbang": "2.2.2",
"rollup-plugin-preserve-shebang": "1.0.1",
"rollup-plugin-progress": "1.1.2",
"rollup-plugin-typescript3": "1.1.3",
"typescript": "4.4.4",
"typescript": "5.1.6",
"version-bump-prompt": "6.1.0",
"yarn": "1.22.17"
"yarn": "1.22.19"
},
"resolutions": {
"ansi-regex": "^5.0.1"
Expand All @@ -77,6 +76,7 @@
"test": "test"
},
"dependencies": {
"ansi-regex": "6.0.1",
"globs": "0.1.4",
"yargs": "16.2.0"
},
Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ function unescapeString(str = '') {

let pipeInUse = false;
let pipeData = '';
config.files = yargs.argv._;
config.globs = yargs.argv._;
config.pipedData = null;
config.showHelp = yargs.showHelp;
config.pattern = pattern;
Expand Down
30 changes: 26 additions & 4 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function engine(config: any = {engine: 'V8'}) {
return doReplacement('Piped data', config, config.pipedData);
}

config.files = globs.sync(config.files);
config.files = globs2paths(config.globs);

if (!config.files.length) {
return error(config.files.length + ' files found');
Expand Down Expand Up @@ -95,7 +95,7 @@ export function engine(config: any = {engine: 'V8'}) {
}

// Release the memory while storing files
_data_rr = undefined;
_data_rr = '';

debug('Write new content to: ' + _file_rr);

Expand Down Expand Up @@ -155,7 +155,7 @@ export function engine(config: any = {engine: 'V8'}) {
function handlePipedData(config) {
step('Check Piped Data');

if (config.files.length) {
if (config.globs.length) {
if (!config.replacementJs) {
chat('Piped data never used.');
}
Expand Down Expand Up @@ -260,7 +260,7 @@ export function engine(config: any = {engine: 'V8'}) {
pattern = pattern.replace(/[-\[\]{}()*+?.,\/\\^$|#\s]/g, '\\$&');
}

let regex = null;
let regex;

let flags = getFlags(config);

Expand Down Expand Up @@ -629,3 +629,25 @@ function replacePlaceholders(str = '', conf: any) {

return str;
}

function globs2paths(_globs: string[] = []) {
const globsToInclude: string[] = [];
const globsToExclude: string[] = [];

_globs.filter(Boolean).forEach((glob) => {
if ('!' === glob[0] || '^' === glob[0]) {
globsToExclude.push(glob.slice(1));
} else {
globsToInclude.push(glob);
}
});

let filesToInclude = globs.sync(globsToInclude);

if (globsToExclude.length) {
const filesToExclude = globs.sync(globsToExclude);
return filesToInclude.filter((el) => !filesToExclude.includes(el));
}

return filesToInclude;
}
Loading

0 comments on commit befbcbe

Please sign in to comment.