-
Notifications
You must be signed in to change notification settings - Fork 9
/
checkdep.js
60 lines (52 loc) · 1.61 KB
/
checkdep.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
const { readdir, readFile } = require('fs').promises;
const { resolve } = require('path');
const thePackage = require('./package.json');
const imports = [];
const ignore = [
'zone.js',
'concurrently',
'jasmine-spec-reporter',
'ts-node',
'@popperjs/core'
];
async function getFiles(dir) {
const dirents = await readdir(dir, { withFileTypes: true });
for (let dirent of dirents) {
const res = resolve(dir, dirent.name);
if (dirent.isDirectory() && dirent.name !== 'node_modules' && dirent.name !== '.git') {
await getFiles(res);
} else if (res.toLowerCase().endsWith('.ts')) {
const content = (await readFile(res)).toString();
const regex = /(import.+? from ['"](.+?)['"])|(require\(['"](.+?)['"]\))/gm;
let match;
while ((match = regex.exec(content)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (match.index === regex.lastIndex) {
regex.lastIndex++;
}
if (match[2]) {
imports.push(match[2]);
} else if (match[4]) {
imports.push(match[4]);
}
}
}
}
}
function check(blockName, block) {
const dependencies = Object.keys(block);
for (let dep of dependencies) {
if (dep.startsWith('@types/') || ignore.indexOf(dep) >= 0) {
continue;
}
const found = imports.filter(next => next.indexOf(dep) === 0 || next.indexOf(dep + '/') === 0);
if (found.length <= 0) {
console.log(blockName + ': ' + dep);
}
}
}
getFiles('.')
.then(() => {
check('dep', thePackage.dependencies);
check('dev', thePackage.devDependencies);
});