-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
182 lines (141 loc) · 3.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*!
* delete-empty <https://github.com/jonschlinkert/delete-empty>
* Copyright (c) 2015-present, Jon Schlinkert
* Released under the MIT License.
*/
'use strict';
const fs = require('fs');
const util = require('util');
const path = require('path');
const rimraf = require('rimraf');
const startsWith = require('path-starts-with');
const colors = require('ansi-colors');
const readdir = util.promisify(fs.readdir);
/**
* Helpers
*/
const GARBAGE_REGEX = /(?:Thumbs\.db|\.DS_Store)$/i;
const isGarbageFile = (file, regex = GARBAGE_REGEX) => regex.test(file);
const filterGarbage = (file, regex) => !isGarbageFile(file, regex);
const isValidDir = (cwd, dir, empty) => {
return !empty.includes(dir) && startsWith(dir, cwd) && isDirectory(dir);
};
const deleteDir = async (dirname, options = {}) => {
if (options.dryRun !== true) {
return new Promise((resolve, reject) => {
rimraf(dirname, { ...options, glob: false }, err => {
if (err) {
reject(err);
} else {
resolve();
}
});
})
}
};
const deleteDirSync = (dirname, options = {}) => {
if (options.dryRun !== true) {
return rimraf.sync(dirname, { ...options, glob: false });
}
};
const deleteEmpty = (cwd, options, cb) => {
if (typeof cwd !== 'string') {
return Promise.reject(new TypeError('expected the first argument to be a string'));
}
if (typeof options === 'function') {
cb = options;
options = null;
}
if (typeof cb === 'function') {
return deleteEmpty(cwd, options)
.then(res => cb(null, res))
.catch(cb);
}
const opts = options || {};
const dirname = path.resolve(cwd);
const onDirectory = opts.onDirectory || (() => {});
const empty = [];
const remove = async filepath => {
let dir = path.resolve(filepath);
if (!isValidDir(cwd, dir, empty)) return;
onDirectory(dir);
let files = await readdir(dir);
if (isEmpty(dir, files, empty, opts)) {
empty.push(dir);
await deleteDir(dir, opts);
if (opts.verbose === true) {
console.log(colors.red('Deleted:'), path.relative(cwd, dir));
}
if (typeof opts.onDelete === 'function') {
await opts.onDelete(dir);
}
return remove(path.dirname(dir));
}
for (const file of files) {
await remove(path.join(dir, file));
}
return empty;
};
return remove(dirname);
};
deleteEmpty.sync = (cwd, options) => {
if (typeof cwd !== 'string') {
throw new TypeError('expected the first argument to be a string');
}
const opts = options || {};
const dirname = path.resolve(cwd);
const deleted = [];
const empty = [];
const remove = filepath => {
let dir = path.resolve(filepath);
if (!isValidDir(cwd, dir, empty)) {
return empty;
}
let files = fs.readdirSync(dir);
if (isEmpty(dir, files, empty, opts)) {
empty.push(dir);
deleteDirSync(dir, opts);
if (opts.verbose === true) {
console.log(colors.red('Deleted:'), path.relative(cwd, dir));
}
if (typeof opts.onDelete === 'function') {
opts.onDelete(dir);
}
return remove(path.dirname(dir));
}
for (let filepath of files) {
remove(path.join(dir, filepath));
}
return empty;
};
remove(dirname);
return empty;
};
/**
* Return true if the given `files` array has zero length or only
* includes unwanted files.
*/
const isEmpty = (dir, files, empty, options = {}) => {
let filter = options.filter || filterGarbage;
let regex = options.junkRegex;
for (let basename of files) {
let filepath = path.join(dir, basename);
if (!(options.dryRun && empty.includes(filepath)) && filter(filepath, regex) === true) {
return false;
}
}
return true;
};
/**
* Returns true if the given filepath exists and is a directory
*/
const isDirectory = dir => {
try {
return fs.statSync(dir).isDirectory();
} catch (err) { /* do nothing */ }
return false;
};
/**
* Expose deleteEmpty
*/
module.exports = deleteEmpty;