forked from nrwl/nx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
108 lines (93 loc) · 2.89 KB
/
index.ts
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
import { readFileSync, readdirSync } from 'fs';
import { join } from 'path';
import * as chalk from 'chalk';
import getDiscrepancies from './discrepancies';
import getMissingDependencies from './missing';
const argv = require('yargs')
.usage('Check projects for dependency discrepancies.')
.option('projects', {
alias: 'p',
type: 'array',
description: 'Projects to check',
})
.option('missing', {
alias: 'm',
type: 'boolean',
default: true,
description: 'Check for missing dependencies',
})
.option('discrepancies', {
alias: 'd',
type: 'boolean',
default: true,
description: 'Check for discrepancies between package and dev dependencies',
})
.option('verbose', {
alias: 'v',
type: 'boolean',
description: 'Run with verbose logging',
}).argv;
(async () => {
const { devDependencies } = JSON.parse(
readFileSync(`./package.json`).toString()
);
const packagesDirectory = join(__dirname, '../..', 'packages');
const projects =
argv.projects ||
readdirSync(packagesDirectory, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
const results = await Promise.all(
projects
.sort()
.map((name) => ({ name }))
.map(async (project) => {
const projectPath = join(packagesDirectory, project.name);
const { dependencies, peerDependencies } = JSON.parse(
readFileSync(`${projectPath}/package.json`).toString()
);
const missing = argv.missing
? await getMissingDependencies(
project.name,
projectPath,
{ ...dependencies, ...(peerDependencies || {}) },
argv.verbose
)
: [];
const discrepancies = argv.discrepancies
? getDiscrepancies(project.name, dependencies, devDependencies)
: [];
return { ...project, missing, discrepancies };
})
);
const total = { missing: 0, discrepancies: 0 };
results.forEach(({ name, missing, discrepancies }) => {
if (!missing.length && !discrepancies.length) {
return;
}
console.log(`${chalk.inverse.bold.cyan(` ${name.toUpperCase()} `)}`);
if (missing.length > 0) {
total.missing += missing.length;
console.log(
`⚠️ ${chalk.bold.inverse(` Missing `)}\n${missing
.sort()
.map(
(p) => ` ${devDependencies[p] ? `${p}@${devDependencies[p]}` : p}`
)
.join(`\n`)}\n`
);
}
if (discrepancies.length > 0) {
total.discrepancies += discrepancies.length;
console.log(
`⛔ ${chalk.bold.inverse(` Discrepancies `)}\n${discrepancies
.map((d) => ` ${d}`)
.join(`\n`)}\n`
);
}
});
if (total.discrepancies > 0 || total.missing > 0) {
process.exit(1);
}
process.exit(0);
})().catch((err) => console.log(err));