1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | 'use strict';
|
25 | const {rimrafSync} = require('rimraf');
|
26 | const path = require('path');
|
27 |
|
28 | function run(argv, options) {
|
29 | const globPatterns = argv.slice(2);
|
30 | const removed = [];
|
31 | if (!globPatterns.length) {
|
32 | console.error('Please specify file patterns to remove.');
|
33 | process.exit(1);
|
34 | }
|
35 |
|
36 | if (typeof options === 'boolean') options = {dryRun: options};
|
37 | options = options || {};
|
38 | globPatterns.forEach(pattern => {
|
39 | const relativePath = path.relative(process.cwd(), pattern);
|
40 | if (relativePath.indexOf('..') !== -1) {
|
41 | if (!options.dryRun) {
|
42 | console.error(
|
43 | 'Skipping ' +
|
44 | pattern +
|
45 | ' as it is not inside the project root directory.',
|
46 | );
|
47 | }
|
48 | } else {
|
49 | if (!options.dryRun) rimrafSync(pattern, {glob: true});
|
50 | removed.push(pattern);
|
51 | }
|
52 | });
|
53 | return 'rm -rf ' + removed.join(' ');
|
54 | }
|
55 |
|
56 | module.exports = run;
|
57 | if (require.main === module) run(process.argv);
|