-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
67 lines (56 loc) · 1.75 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
const walk = require('walk-filtered');
const path = require('path');
const findRoot = require('find-root');
const fs = require('fs-extra');
const prep = (projectPath, options = {}) => {
const root = findRoot(projectPath);
let pruneJson = null;
let usingCustomPrune = false;
let prunePath = path.join(root, 'prune.json');
try {
pruneJson = fs.readJsonSync(prunePath);
usingCustomPrune = true;
} catch (e) {
prunePath = path.join(__dirname, 'default-prune.json');
pruneJson = fs.readJsonSync(prunePath);
}
let files = pruneJson.files || [];
const extensions = pruneJson.extensions || [];
const directories = pruneJson.directories || [];
if (options.pruneLicense) files = files.concat(pruneJson.licenses || []);
const shouldPruneFile = pth =>
files.includes(path.basename(pth).toLowerCase()) ||
extensions.includes(path.extname(pth).toLowerCase());
const shouldPruneDir = pth => directories.includes(path.basename(pth).toLowerCase());
const modulePath = path.join(root, 'node_modules');
let size = 0;
let fileCount = 0;
let dirCount = 0;
const filesToPrune = [];
const dirsToPrune = [];
const process = (pth, stats) => {
if (stats.isFile() && shouldPruneFile(pth)) {
size += stats.size;
fileCount += 1;
filesToPrune.push(path.join(modulePath, pth));
}
if (stats.isDirectory() && shouldPruneDir(pth)) {
dirCount += 1;
dirsToPrune.push(path.join(modulePath, pth));
}
};
const onFinish = () => ({
modulePath,
usingCustomPrune,
prunePath,
size,
files: filesToPrune,
fileCount,
dirs: dirsToPrune,
dirCount,
});
return new Promise(resolve => walk(modulePath, process, true, () => resolve(onFinish())));
};
module.exports = {
prep,
};