-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (78 loc) · 2.63 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
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const rootDirectory = './';
checkDirectory(rootDirectory);
function checkDirectory(directory) {
fs.readdir(directory, (readDirError, files) => {
if (readDirError) {
console.log(chalk.red('Error found:', readDirError));
return;
}
// 1. Find package.json
let index = files.findIndex((value) => value === 'package.json');
if (index > -1) {
fs.readFile(
path.join(directory, files[index]),
'utf8',
(readFileError, packageJSONFile) => {
if (readFileError) {
console.log(chalk.red('Error found:', readFileError));
return;
}
// 2. Find dependencies in package.json
const dependencies = JSON.parse(packageJSONFile).dependencies;
console.log(
chalk.green.bold(
`${directory} - ${Object.keys(dependencies).length}`
)
);
Object.keys(dependencies).forEach((dependency) => {
fs.readdir(
path.join(directory, 'node_modules', dependency),
(depError, depFiles) => {
let depIndex = depFiles.findIndex(
(dep) => dep === 'package.json'
);
if (depIndex > -1) {
fs.readFile(
path.join(
directory,
'node_modules',
dependency,
depFiles[depIndex]
),
'utf-8',
(fileError, depPackageJSONFile) => {
if (fileError) {
console.log(chalk.red('Error found:', fileError));
return;
}
depPackageJSON = JSON.parse(depPackageJSONFile);
if ('dependencies' in depPackageJSON)
console.log(
chalk.green.bold(
`${dependency} - ${
Object.keys(depPackageJSON.dependencies).length
}`
)
);
}
);
}
}
);
});
// console.log(
// `${directory} has ${Object.keys(dependencies).length} dependencies`
// );
// console.log(Object.keys(dependencies));
}
);
} else {
console.log('not found');
}
// 3. Find package in node_modules
// 4. Read package.json of package
});
}