forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_dependencies.js
151 lines (128 loc) · 4.59 KB
/
validate_dependencies.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
#!/usr/bin/env node
'use strict';
/* eslint-disable no-console */
const chalk = require('chalk');
const fs = require('fs');
const glob = require('glob');
const packages = require('../../lib/packages');
const path = require('path');
const IMPORT_RE = /(^|\n)\s*import\b(?:.|\n)*?\'[^\']*\'/g;
const REQUIRE_RE = /\brequire\('[^)]+?'\)/g;
const NODE_PACKAGES = [
'child_process',
'fs',
'https',
'os',
'path',
'process',
'url',
'vm',
'zlib'
];
const ANGULAR_PACKAGES = [
'@angular/compiler',
'@angular/compiler-cli',
'@angular/core'
];
function listImportedModules(source) {
const imports = source.match(IMPORT_RE);
return (imports || [])
.map(match => {
const m = match.match(/'(.*)'/);
return m && m[1];
})
.filter(x => !!x)
.filter(modulePath => modulePath[0] != '.')
.map(fullImportPath => {
if (fullImportPath[0] == '@') {
// Need to get the scope as well.
return fullImportPath.split('/').slice(0, 2).join('/');
} else {
return fullImportPath.split('/')[0];
}
});
}
function listRequiredModules(source) {
const requires = source.match(REQUIRE_RE);
return (requires || [])
.map(match => {
const m = match.match(/'([^']*)'/);
return m && m[1];
})
.filter(x => !!x)
.filter(modulePath => modulePath[0] != '.')
.map(fullImportPath => {
if (fullImportPath[0] == '@') {
// Need to get the scope as well.
return fullImportPath.split('/').slice(0, 2).join('/');
} else {
return fullImportPath.split('/')[0];
}
});
}
function reportMissingDependencies(missingDeps) {
if (missingDeps.length == 0) {
console.log(chalk.green(' no dependency missing from package.json.'));
} else {
console.log(chalk.yellow(` ${missingDeps.length} missing from package.json:`));
missingDeps.forEach(md => console.log(` ${md}`));
exitCode = 1;
}
}
function reportExcessiveDependencies(overDeps) {
if (overDeps.length == 0) {
console.log(chalk.green(' no excessive dependencies in package.json.'));
} else {
console.log(chalk.yellow(` ${overDeps.length} excessive dependencies in package.json:`));
overDeps.forEach(md => console.log(` ${md}`));
exitCode = 1;
}
}
let exitCode = 0;
const overallDeps = [];
for (const packageName of Object.keys(packages)) {
console.log(chalk.green(`Reading dependencies of "${packageName}".`));
const allSources = glob.sync(path.join(__dirname, '../../packages/', packageName, '**/*'))
.filter(p => p.match(/\.(js|ts)$/))
.filter(p => !p.match(/\.spec\./))
.filter(p => !p.match(/\/blueprints\//));
const importMap = {};
allSources.forEach(function(filePath) {
const source = fs.readFileSync(filePath, 'utf8');
listImportedModules(source)
.forEach(modulePath => importMap[modulePath] = true);
listRequiredModules(source)
.forEach(modulePath => importMap[modulePath] = true);
});
const dependencies = Object.keys(importMap)
// Filter out the node packages that should not be depended on.
.filter(x => NODE_PACKAGES.indexOf(x) == -1);
overallDeps.push(...dependencies);
console.log(chalk.green(` found ${dependencies.length} dependencies...`));
const packageJson = JSON.parse(fs.readFileSync(packages[packageName].packageJson, 'utf8'));
const allDeps = []
.concat(Object.keys(packageJson['dependencies'] || {}))
.concat(Object.keys(packageJson['devDependencies'] || {}))
.concat(Object.keys(packageJson['peerDependencies'] || {}));
const missingDeps = dependencies.filter(d => allDeps.indexOf(d) == -1);
reportMissingDependencies(missingDeps);
const overDeps = allDeps.filter(d => dependencies.indexOf(d) == -1)
.filter(x => ANGULAR_PACKAGES.indexOf(x) == -1);
reportExcessiveDependencies(overDeps);
console.log('');
}
console.log(chalk.green('Validating root package. [devDependencies ignored]'));
const rootPackagePath = path.join(__dirname, '../../package.json');
const rootPackageJson = JSON.parse(fs.readFileSync(rootPackagePath, 'utf8'));
// devDependencies are ignored
const allRootDeps = []
.concat(Object.keys(rootPackageJson['dependencies'] || {}))
.concat(Object.keys(rootPackageJson['peerDependencies'] || {}));
const internalPackages = Object.keys(packages);
const missingRootDeps = overallDeps.filter(d => allRootDeps.indexOf(d) == -1)
.filter(d => internalPackages.indexOf(d) == -1);
reportMissingDependencies(missingRootDeps);
const overRootDeps = allRootDeps.filter(d => overallDeps.indexOf(d) == -1)
.filter(x => ANGULAR_PACKAGES.indexOf(x) == -1);
reportExcessiveDependencies(overRootDeps);
process.exit(exitCode);