Skip to content

Commit

Permalink
Add --verbose flag (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
marchuffnagle committed Aug 31, 2023
1 parent dde9e11 commit f32b531
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
19 changes: 18 additions & 1 deletion cli.js
Expand Up @@ -3,13 +3,22 @@ import process from 'node:process';
import meow from 'meow';
import {deleteAsync} from 'del';

const logEvent = event => {
if (event.path !== undefined) {
console.log(event.path);
}
};

const noop = () => {};

const cli = meow(`
Usage
$ del <path|glob> …
Options
--force, -f Allow deleting the current working directory and outside
--dry-run, -d List what would be deleted instead of deleting
--verbose, -v Display the absolute path of files and directories as they are deleted
Examples
$ del unicorn.png rainbow.png
Expand All @@ -25,14 +34,22 @@ const cli = meow(`
type: 'boolean',
alias: 'd',
},
verbose: {
type: 'boolean',
alias: 'v',
},
},
});

if (cli.input.length === 0) {
console.error('Specify at least one path');
process.exitCode = 1;
} else {
const files = await deleteAsync(cli.input, cli.flags);
const {verbose, ...flags} = cli.flags;

const onProgress = verbose ? logEvent : noop;

const files = await deleteAsync(cli.input, {onProgress, ...flags});

if (cli.flags.dryRun) {
console.log(files.join('\n'));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -50,7 +50,7 @@
"cross-platform"
],
"dependencies": {
"del": "^7.0.0",
"del": "^7.1.0",
"meow": "^10.1.3"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions readme.md
Expand Up @@ -23,6 +23,7 @@ $ del --help
Options
--force, -f Allow deleting the current working directory and outside
--dry-run, -d List what would be deleted instead of deleting
--verbose, -v Display the absolute path of files and directories as they are deleted
Examples
$ del unicorn.png rainbow.png
Expand Down
11 changes: 11 additions & 0 deletions test.js
Expand Up @@ -8,3 +8,14 @@ test('main', async t => {
await execa('./cli.js', ['--force', filename]);
t.false(fs.existsSync(filename));
});

test('verbose file exists', async t => {
const filename = tempWrite.sync('foo');
const {stdout} = await execa('./cli.js', ['--force', '--verbose', filename]);
t.is(stdout, filename);
});

test('verbose file does not exist', async t => {
const {stdout} = await execa('./cli.js', ['--verbose', 'does-not-exist.txt']);
t.is(stdout, '');
});

0 comments on commit f32b531

Please sign in to comment.