Skip to content

Commit f71aaa6

Browse files
committed
Fix --dry-run behavior and clarify help text
1 parent f6e4605 commit f71aaa6

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

cli.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const cli = meow(`
1717
1818
Options
1919
--force, -f Allow deleting the current working directory and outside
20-
--dry-run, -d List what would be deleted instead of deleting
20+
--dry-run, -d List what would be deleted instead of deleting (silent if no matches)
2121
--verbose, -v Display the absolute path of files and directories as they are deleted
2222
2323
Examples
@@ -45,13 +45,15 @@ if (cli.input.length === 0) {
4545
console.error('Specify at least one path');
4646
process.exitCode = 1;
4747
} else {
48-
const {verbose, ...flags} = cli.flags;
48+
const {verbose, dryRun, ...flags} = cli.flags;
4949

50-
const onProgress = verbose ? logEvent : noop;
50+
// Only use onProgress for verbose mode when not in dry-run
51+
// In dry-run mode, we print the files at the end instead
52+
const onProgress = verbose && !dryRun ? logEvent : noop;
5153

52-
const files = await deleteAsync(cli.input, {onProgress, ...flags});
54+
const files = await deleteAsync(cli.input, {onProgress, dryRun, ...flags});
5355

54-
if (cli.flags.dryRun) {
56+
if (dryRun && files.length > 0) {
5557
console.log(files.join('\n'));
5658
}
5759
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $ del --help
2222
2323
Options
2424
--force, -f Allow deleting the current working directory and outside
25-
--dry-run, -d List what would be deleted instead of deleting
25+
--dry-run, -d List what would be deleted instead of deleting (silent if no matches)
2626
--verbose, -v Display the absolute path of files and directories as they are deleted
2727
2828
Examples

test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,34 @@ test('verbose file does not exist', async t => {
1919
const {stdout} = await execa('./cli.js', ['--verbose', 'does-not-exist.txt']);
2020
t.is(stdout, '');
2121
});
22+
23+
test('dry-run with files to delete', async t => {
24+
const filename = tempWrite.sync('foo');
25+
const {stdout} = await execa('./cli.js', ['--dry-run', '--force', filename]);
26+
t.is(stdout, filename);
27+
// File should still exist after dry run
28+
t.true(fs.existsSync(filename));
29+
});
30+
31+
test('dry-run with no files to delete', async t => {
32+
const {stdout} = await execa('./cli.js', ['--dry-run', 'does-not-exist-*.txt']);
33+
t.is(stdout, '');
34+
});
35+
36+
test('verbose + dry-run does not duplicate output', async t => {
37+
const filename = tempWrite.sync('foo');
38+
const {stdout} = await execa('./cli.js', ['--verbose', '--dry-run', '--force', filename]);
39+
// Should only print the filename once, not twice
40+
t.is(stdout, filename);
41+
});
42+
43+
test('verbose + dry-run with multiple files', async t => {
44+
const file1 = tempWrite.sync('foo');
45+
const file2 = tempWrite.sync('bar');
46+
const {stdout} = await execa('./cli.js', ['--verbose', '--dry-run', '--force', file1, file2]);
47+
const lines = stdout.split('\n');
48+
// Should have exactly 2 lines (one per file)
49+
t.is(lines.length, 2);
50+
t.true(lines.includes(file1));
51+
t.true(lines.includes(file2));
52+
});

0 commit comments

Comments
 (0)