Skip to content

Commit

Permalink
New --exclude flag to skip files
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Aug 3, 2023
1 parent 4de0f26 commit 94594bc
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Command-line flags:
| `--cd` | Change working directory before starting search. | **string** |
| `--concat` | Merge all files into one file in the target folder. | **string** |
| `--content` | String to be used instead of the input file contents. | **string** |
| `--exclude` | Skip files containing the string in their path. | **string** |
| `--ext` | Filter files by file extension, such as `.js`.<br>Use a comma to specify multiple extensions. | **string** |
| `--find` | Text to search for in the source input files. | **string** |
| `--header` | Prepend a line of text to each file. | **string** |
Expand Down
3 changes: 2 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import log from 'fancy-log';
import path from 'path';

// Parameters and flags
const validFlags = ['cd', 'concat', 'content', 'ext', 'find', 'header', 'no-source-map',
const validFlags = ['cd', 'concat', 'content', 'exclude', 'ext', 'find', 'header', 'no-source-map',
'note', 'pkg', 'quiet', 'regex', 'rename', 'replacement', 'summary'];
const cli = cliArgvUtil.parse(validFlags);
const source = cli.params[0]; //origin file or folder
Expand Down Expand Up @@ -88,6 +88,7 @@ const options = {
cd: cli.flagMap.cd ?? null,
concat: cli.flagMap.concat ?? null,
content: escape(cli.flagMap.content),
exclude: cli.flagMap.exclude ?? null,
extensions: cli.flagMap.ext?.split(',') ?? [],
filename: isFile ? path.basename(source) : null,
find: escape(cli.flagMap.find),
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,17 @@
"@types/fancy-log": "~2.0",
"@types/glob": "~8.1",
"@types/node": "~20.4",
"@typescript-eslint/eslint-plugin": "~6.0",
"@typescript-eslint/parser": "~6.0",
"@typescript-eslint/eslint-plugin": "~6.2",
"@typescript-eslint/parser": "~6.2",
"add-dist-header": "~1.1",
"assert-deep-strict-equal": "~1.1",
"copy-file-util": "~1.1",
"eslint": "~8.44",
"eslint": "~8.46",
"fetch-json": "~3.2",
"highlight.js": "~11.8",
"jshint": "~2.13",
"mocha": "~10.2",
"pretty-print-json": "~2.0",
"rev-web-assets": "~1.2",
"rimraf": "~5.0",
"run-scripts-util": "~1.2",
"typescript": "~5.1",
Expand Down
16 changes: 11 additions & 5 deletions replacer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ export type Settings = {
cd: string | null, //change working directory before starting search
concat: string | null, //merge all files into one file in the target folder
content: string | null, //string to be used instead of the input file contents
exclude: string | null, //skip files containing the string in their path
extensions: string[], //filter files by file extensions, example: ['.js', '.css']
filename: string | null, //single file in the source folder to be processed
find: string | null, //text to search for in the source input files
header: string | null, //prepend a line of text to each file
noSourceMap: boolean, //remove all "sourceMappingURL" comments directives.
pkg: boolean, //load package.json and make it available as "pkg"
regex: RegExp | null, //pattern to search for in the source input files
rename: string | null, //new output filename if there's only one source file.
replacement: string | null, //text to insert into the target output files
pkg: boolean, //load package.json and make it available as "pkg"
};
export type Options = Partial<Settings>;
export type Results = {
Expand Down Expand Up @@ -61,12 +62,13 @@ const replacer = {
const defaults = {
cd: null,
concat: null,
exclude: null,
extensions: [],
find: null,
noSourceMap: false,
pkg: false,
regex: null,
replacement: null,
pkg: false,
};
const settings = { ...defaults, ...options };
const startTime = Date.now();
Expand All @@ -89,14 +91,18 @@ const replacer = {
null;
if (errorMessage)
throw Error('[replacer-util] ' + errorMessage);
const resultsFile = (file: string) =>({
const globFiles = () =>
exts.map(ext => globSync(source + '/**/*' + ext)).flat().sort();
const keep = (filename: string) =>
!settings.exclude || !filename.includes(settings.exclude);
const resultsFile = (file: string) => ({
origin: file,
dest: concatFile ?? renameFile ?? target + '/' + file.substring(source.length + 1),
});
const exts = settings.extensions.length ? settings.extensions : [''];
const globFiles = () => exts.map(ext => globSync(source + '/**/*' + ext)).flat().sort();
const filesRaw = settings.filename ? [source + '/' + settings.filename] : globFiles();
const files = filesRaw.filter(task.isTextFile).map(file => slash(file)).map(resultsFile);
const filtered = filesRaw.filter(task.isTextFile).filter(keep);
const files = filtered.map(file => slash(file)).map(resultsFile);
const pkg = settings.pkg ? task.readPackageJson() : null;
const engine = new Liquid({ globals: { pkg } });
const versionFormatter = (numIds: number) =>
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/source/mock1.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ <h2>{{pkg.description}}</h2>
<li>Minor: v{{pkg.version | minor-version}}</li>
<li>Major: v{{pkg.version | major-version}}</li>
</ul>
<pre><code class=language-js>{% render 'spec/fixtures/source/subfolder/mock2.js' %}</code></pre>
<pre><code class=language-js>{% render 'spec/fixtures/source/subfolder-a/mock2.js' %}</code></pre>
</body>
</html>
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions spec/fixtures/source/subfolder-b/mock3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>🔍🔍🔍 {{pkg.name}} 🔍🔍🔍</title>
<link rel=stylesheet href=https://cdn.jsdelivr.net/npm/pretty-print-json@{{pkg.devDependencies.pretty-print-json|version}}/dist/css/pretty-print-json.css>
<script src=https://cdn.jsdelivr.net/npm/pretty-print-json@{{pkg.devDependencies.pretty-print-json|version}}/dist/pretty-print-json.min.js></script>
<script src=https://cdn.jsdelivr.net/npm/fetch-json@{{pkg.devDependencies.fetch-json|version}}/dist/fetch-json.min.js></script>
</head>
<body>
<h1>🔍🔍🔍 {{pkg.name}} 🔍🔍🔍</h1>
<h2>{{pkg.description}}</h2>
<p>I, for one, welcome our new insect overlords.</p>
<ul>
<li>Release: v{{pkg.version | version}}</li>
<li>Minor: v{{pkg.version | minor-version}}</li>
<li>Major: v{{pkg.version | major-version}}</li>
</ul>
</body>
</html>
File renamed without changes.
File renamed without changes.
19 changes: 10 additions & 9 deletions spec/mocha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// Imports
import { assertDeepStrictEqual } from 'assert-deep-strict-equal';
import { cliArgvUtil } from 'cli-argv-util';
import { revWebAssets } from 'rev-web-assets';
import assert from 'assert';
import fs from 'fs';

Expand Down Expand Up @@ -49,20 +48,22 @@ describe('Calling replacer.transform()', () => {
it('creates the correct text files in the target folder', () => {
const options = {
cd: 'spec/fixtures',
pkg: true,
exclude: 'subfolder-b',
find: 'insect',
noSourceMap: true,
pkg: true,
replacement: 'A.I. {{pkg.type}}', //'A.I. module'
};
replacer.transform('source', 'target', options);
const actual = revWebAssets.readFolderRecursive('spec/fixtures/target');
const actual = fs.readdirSync('spec/fixtures/target', { recursive: true }).sort();
const expected = [
'spec/fixtures/target/mock1.html',
'spec/fixtures/target/mock1.js',
'spec/fixtures/target/mock1.min.css',
'spec/fixtures/target/subfolder/mock2.html',
'spec/fixtures/target/subfolder/mock2.js',
'spec/fixtures/target/subfolder/mock2.min.css',
'mock1.html',
'mock1.js',
'mock1.min.css',
'subfolder-a',
'subfolder-a/mock2.html',
'subfolder-a/mock2.js',
'subfolder-a/mock2.min.css',
];
assertDeepStrictEqual(actual, expected);
});
Expand Down

0 comments on commit 94594bc

Please sign in to comment.