Skip to content

Commit

Permalink
Add --echo flag for dry run preview
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Aug 27, 2023
1 parent 18c60f5 commit cb391a9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ You can also install **recursive-exec** globally (`--global`) and then run it an

### 3. CLI flags
Command-line flags:
| Flag | Description | Value |
| ------------ | ----------------------------------------------------- | ---------- |
| Flag | Description | Value |
| ------------ | --------------------------------------------------------- | ---------- |
| `--echo` | Show dry run preview of each command without executig it. | N/A |
| `--ext` | Filter files by file extension, such as `.js`.<br>Use a comma to specify multiple extensions. | **string** |
| `--note` | Place to add a comment only for humans. | **string** |
| `--quiet` | Suppress informational messages. | N/A |
| `--note` | Place to add a comment only for humans. | **string** |
| `--quiet` | Suppress informational messages. | N/A |

Examples:
- `recursive-exec src/web --ext=.less 'lessc src/web/{{filename}} build/web/{{basename}}.css'`<br>
Expand Down
3 changes: 2 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { cliArgvUtil } from 'cli-argv-util';
import { recursiveExec } from '../dist/recursive-exec.js';

// Parameters and flags
const validFlags = ['ext', 'note', 'quiet'];
const validFlags = ['echo', 'ext', 'note', 'quiet'];
const cli = cliArgvUtil.parse(validFlags);
const folder = cli.params[0];
const command = cli.params[1];
Expand All @@ -39,6 +39,7 @@ const error =
if (error)
throw Error('[recursive-exec] ' + error);
const options = {
echo: cli.flagOn.echo,
quiet: cli.flagOn.quiet,
extensions: cli.flagMap.ext?.split(',') ?? null,
};
Expand Down
6 changes: 5 additions & 1 deletion recursive-exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import slash from 'slash';

// Types
export type Settings = {
echo: boolean, //show dry run preview of each command without executig it
extensions: string[] | null, //filter files by file extensions, example: ['.js', '.css']
quiet: boolean, //suppress informational messages
};
Expand Down Expand Up @@ -72,14 +73,17 @@ const recursiveExec = {
};
};
const results = files.map(slash).map(calcResult);
const previewCommand = (result: Result) => {
log(logName, chalk.blue.bold('preview:'), chalk.yellow(result.command));
};
const execCommand = (result: Result) => {
if (!settings.quiet)
log(logName, chalk.blue.bold('command:'), chalk.cyanBright(result.command));
const task = spawnSync(result.command, { shell: true, stdio: 'inherit' });
if (task.status !== 0)
throw Error(`[recursive-exec] Status: ${task.status}\nCommand: ${result.command}`);
};
results.forEach(execCommand);
results.forEach(settings.echo ? previewCommand : execCommand);
const summary = `(files: ${results.length}, ${Date.now() - startTime}ms)`;
if (!settings.quiet)
log(logName, chalk.green('done'), chalk.white(summary));
Expand Down
4 changes: 3 additions & 1 deletion spec/mocha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ describe('Executing the CLI', () => {
});

it('to rename copies of files with camel case names preserves the source folder structure', () => {
run('recursive-exec spec/fixtures/source --ext=.html "copy-file {{file}} spec/fixtures/target/html/{{path}}/{{name}}.{{nameCamelCase}}.html"');
const template = 'copy-file {{file}} spec/fixtures/target/html/{{path}}/{{name}}.{{nameCamelCase}}.html';
run(`recursive-exec spec/fixtures/source --ext=.html --echo "${template}"`);
run(`recursive-exec spec/fixtures/source --ext=.html "${template}"`);
const actual = cliArgvUtil.readFolder('spec/fixtures/target/html');
const expected = [
'mock-file1.mockFile1.html',
Expand Down

0 comments on commit cb391a9

Please sign in to comment.