1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.delSync = exports.del = void 0;
|
4 | const js_lib_1 = require("@naturalcycles/js-lib");
|
5 | const fs = require("fs-extra");
|
6 | const colors_1 = require("../colors");
|
7 | const index_1 = require("../index");
|
8 | const DEF_OPT = {
|
9 | patterns: [],
|
10 | concurrency: Number.POSITIVE_INFINITY,
|
11 | };
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | async function del(_opt) {
|
18 | const started = Date.now();
|
19 |
|
20 | if (typeof _opt === 'string') {
|
21 | _opt = {
|
22 | patterns: [_opt],
|
23 | };
|
24 | }
|
25 | const opt = {
|
26 | ...DEF_OPT,
|
27 | ..._opt,
|
28 | concurrency: _opt.concurrency || DEF_OPT.concurrency,
|
29 | };
|
30 | const { patterns, concurrency, verbose, silent, debug, dry } = opt;
|
31 | if (debug) {
|
32 | console.log(opt);
|
33 | }
|
34 |
|
35 | const filenames = await (0, index_1.globby)(patterns, {
|
36 | dot: true,
|
37 | expandDirectories: true,
|
38 | onlyFiles: true,
|
39 | });
|
40 | if (verbose || debug || dry) {
|
41 | console.log(`Will delete ${(0, colors_1.yellow)(filenames.length)} files:`, filenames);
|
42 | }
|
43 | if (dry)
|
44 | return;
|
45 | await (0, js_lib_1.pMap)(filenames, filepath => fs.remove(filepath), { concurrency });
|
46 |
|
47 | let dirnames = await (0, index_1.globby)(patterns, {
|
48 | dot: true,
|
49 | expandDirectories: true,
|
50 | onlyDirectories: true,
|
51 | });
|
52 |
|
53 | dirnames = dirnames.concat(await (0, js_lib_1.pFilter)(patterns, async (pattern) => {
|
54 | return (await fs.pathExists(pattern)) && (await fs.lstat(pattern)).isDirectory();
|
55 | }));
|
56 | const dirnamesSorted = dirnames.sort().reverse();
|
57 |
|
58 | const deletedDirs = [];
|
59 | for await (const dirpath of dirnamesSorted) {
|
60 | if (await isEmptyDir(dirpath)) {
|
61 |
|
62 | await fs.remove(dirpath);
|
63 | deletedDirs.push(dirpath);
|
64 | }
|
65 | }
|
66 | if (verbose || debug)
|
67 | console.log({ deletedDirs });
|
68 | if (!silent) {
|
69 | console.log(`del deleted ${(0, colors_1.yellow)(filenames.length)} files and ${(0, colors_1.yellow)(deletedDirs.length)} dirs ${(0, colors_1.dimGrey)((0, js_lib_1._since)(started))}`);
|
70 | }
|
71 | }
|
72 | exports.del = del;
|
73 | function delSync(_opt) {
|
74 | const started = Date.now();
|
75 |
|
76 | if (typeof _opt === 'string') {
|
77 | _opt = {
|
78 | patterns: [_opt],
|
79 | };
|
80 | }
|
81 | const opt = {
|
82 | ...DEF_OPT,
|
83 | ..._opt,
|
84 | };
|
85 | const { patterns, verbose, silent, debug, dry } = opt;
|
86 | if (debug) {
|
87 | console.log(opt);
|
88 | }
|
89 |
|
90 | const filenames = index_1.globby.sync(patterns, {
|
91 | dot: true,
|
92 | expandDirectories: true,
|
93 | onlyFiles: true,
|
94 | });
|
95 | if (verbose || debug || dry) {
|
96 | console.log(`Will delete ${(0, colors_1.yellow)(filenames.length)} files:`, filenames);
|
97 | }
|
98 | if (dry)
|
99 | return;
|
100 | filenames.forEach(filepath => fs.removeSync(filepath));
|
101 |
|
102 | let dirnames = index_1.globby.sync(patterns, {
|
103 | dot: true,
|
104 | expandDirectories: true,
|
105 | onlyDirectories: true,
|
106 | });
|
107 |
|
108 | dirnames = dirnames.concat(patterns.filter(p => fs.pathExistsSync(p) && fs.lstatSync(p).isDirectory()));
|
109 | const dirnamesSorted = dirnames.sort().reverse();
|
110 |
|
111 | const deletedDirs = [];
|
112 | for (const dirpath of dirnamesSorted) {
|
113 | if (isEmptyDirSync(dirpath)) {
|
114 |
|
115 | fs.removeSync(dirpath);
|
116 | deletedDirs.push(dirpath);
|
117 | }
|
118 | }
|
119 | if (verbose || debug)
|
120 | console.log({ deletedDirs });
|
121 | if (!silent) {
|
122 | console.log(`del deleted ${(0, colors_1.yellow)(filenames.length)} files and ${(0, colors_1.yellow)(deletedDirs.length)} dirs ${(0, colors_1.dimGrey)((0, js_lib_1._since)(started))}`);
|
123 | }
|
124 | }
|
125 | exports.delSync = delSync;
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | async function isEmptyDir(dir) {
|
131 | return (await fs.readdir(dir)).length === 0;
|
132 | }
|
133 | function isEmptyDirSync(dir) {
|
134 | return fs.readdirSync(dir).length === 0;
|
135 | }
|