UNPKG

1.47 kBJavaScriptView Raw
1#!/usr/bin/env node
2// Copyright IBM Corp. and LoopBack contributors 2017,2020. All Rights Reserved.
3// Node module: @loopback/build
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6
7/*
8========
9
10Usage:
11 node ./bin/run-clean file1 file2 ...
12
13The script will `rm -rf` all files/folders.
14
15Then the provided command is executed with the modified arguments.
16
17Example usage:
18
19 node ./bin/run-clean dist dist6
20
21========
22*/
23
24'use strict';
25const {rimrafSync} = require('rimraf');
26const path = require('path');
27
28function 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 // Keep it backward compatible as dryRun
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
56module.exports = run;
57if (require.main === module) run(process.argv);