Skip to content

Commit fbbb3ad

Browse files
committed
Release v1.1.1
1 parent dc124c5 commit fbbb3ad

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

dist/recursive-exec.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! recursive-exec v1.1.0 ~~ https://github.com/center-key/recursive-exec ~~ MIT License
1+
//! recursive-exec v1.1.1 ~~ https://github.com/center-key/recursive-exec ~~ MIT License
22

33
export type Settings = {
44
echo: boolean;
@@ -16,6 +16,8 @@ export type Result = {
1616
command: string;
1717
};
1818
declare const recursiveExec: {
19+
assert(ok: unknown, message: string | null): void;
20+
cli(): void;
1921
find(folder: string, command: string, options?: Partial<Settings>): Result[];
2022
};
2123
export { recursiveExec };

dist/recursive-exec.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
//! recursive-exec v1.1.0 ~~ https://github.com/center-key/recursive-exec ~~ MIT License
1+
//! recursive-exec v1.1.1 ~~ https://github.com/center-key/recursive-exec ~~ MIT License
22

3+
import { cliArgvUtil } from 'cli-argv-util';
34
import { globSync } from 'glob';
45
import { spawnSync } from 'node:child_process';
56
import chalk from 'chalk';
@@ -8,20 +9,47 @@ import log from 'fancy-log';
89
import path from 'path';
910
import slash from 'slash';
1011
const recursiveExec = {
12+
assert(ok, message) {
13+
if (!ok)
14+
throw new Error(`[recursive-exec] ${message}`);
15+
},
16+
cli() {
17+
const validFlags = ['echo', 'exclude', 'ext', 'note', 'quiet'];
18+
const cli = cliArgvUtil.parse(validFlags);
19+
const folder = cli.params[0];
20+
const command = cli.params[1];
21+
const macroName = command?.match(/^{{command:(.*)}}$/)?.[1];
22+
const readPkg = () => JSON.parse(fs.readFileSync('package.json', 'utf-8'));
23+
const macroValue = macroName && readPkg().recursiveExecConfig?.commands?.[macroName];
24+
const error = cli.invalidFlag ? cli.invalidFlagMsg :
25+
!folder ? 'Missing source folder.' :
26+
!command ? 'Missing command to execute.' :
27+
cli.paramCount > 2 ? 'Extraneous parameter: ' + cli.params[2] :
28+
macroName && !macroValue ? 'Command macro not defined: ' + macroName :
29+
null;
30+
recursiveExec.assert(!error, error);
31+
const options = {
32+
echo: cli.flagOn.echo,
33+
excludes: cli.flagMap.exclude?.split(',') ?? null,
34+
quiet: cli.flagOn.quiet,
35+
extensions: cli.flagMap.ext?.split(',') ?? null,
36+
};
37+
recursiveExec.find(folder, macroValue ?? command, options);
38+
},
1139
find(folder, command, options) {
1240
const defaults = {
41+
echo: false,
1342
excludes: null,
1443
extensions: null,
1544
quiet: false,
1645
};
1746
const settings = { ...defaults, ...options };
18-
const errorMessage = !folder ? 'Must specify the folder path.' :
47+
const error = !folder ? 'Must specify the folder path.' :
1948
!fs.existsSync(folder) ? 'Folder does not exist: ' + folder :
2049
!fs.statSync(folder).isDirectory() ? 'Folder is not a folder: ' + folder :
2150
!command ? 'Command template missing.' :
2251
null;
23-
if (errorMessage)
24-
throw new Error('[recursive-exec] ' + errorMessage);
52+
recursiveExec.assert(!error, error);
2553
const startTime = Date.now();
2654
const source = slash(path.normalize(folder)).replace(/\/$/, '');
2755
const logName = chalk.gray('recursive-exec');
@@ -64,8 +92,7 @@ const recursiveExec = {
6492
if (!settings.quiet)
6593
log(logName, chalk.blue.bold('command:'), chalk.cyanBright(result.command));
6694
const task = spawnSync(result.command, { shell: true, stdio: 'inherit' });
67-
if (task.status !== 0)
68-
throw new Error(`[recursive-exec] Status: ${task.status}\nCommand: ${result.command}`);
95+
recursiveExec.assert(task.status === 0, `Status: ${task.status}, Command: ${result.command}`);
6996
};
7097
results.forEach(settings.echo ? previewCommand : execCommand);
7198
const summary = `(files: ${results.length}, ${Date.now() - startTime}ms)`;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "recursive-exec",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "Run a command on each file in a folder and its subfolders (CLI tool designed for use in npm package.json scripts)",
55
"license": "MIT",
66
"type": "module",

0 commit comments

Comments
 (0)