-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
60 lines (49 loc) · 1.72 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
const path = require('path');
const { execSync } = require('child_process');
function getAllDependenciesVersions({ ignorePackages = [], rootDir = path.resolve('./'), verbose = false }) {
rootDir = path.resolve(rootDir)
if (verbose) {
ignorePackages.length ? console.info(`ignoring packages: ${ignorePackages}`) : console.info(`not ignoring any package`)
rootDir && console.info(`using root dir: ${rootDir}`);
}
let yarnWorkspaceJsonString = execSync('yarn workspaces --silent info', {
cwd: rootDir,
encoding: 'utf8'
}).toString();
const allPackagesWorkspaces = JSON.parse(yarnWorkspaceJsonString.replace(`[2K[1G`, ''));
const rootWorkspace = {
"location": rootDir,
};
let allWorkspaces = { root: rootWorkspace, ...allPackagesWorkspaces };
if (verbose) {
console.info(`found tree:\n`, allWorkspaces);
}
const allDeps = {};
Object.values(allWorkspaces).forEach(workspace => {
const packageJson = require(path.resolve(rootDir, workspace.location, 'package.json'));
const allDependencies = [
...Object.entries(packageJson.dependencies || {}),
...Object.entries(packageJson.devDependencies || {}),
...Object.entries(packageJson.resolutions || {}).map(([name,v]) => [name.replace('**/', ''), v]),
];
allDependencies.forEach(([dep, version]) => {
if (!allDeps[dep]) {
allDeps[dep] = {
versions: [version],
dep,
};
return;
}
if (!allDeps[dep].versions.includes(version)) {
allDeps[dep].versions.push(version);
if (!ignorePackages.includes(dep)) {
allDeps[dep].haveConflicts = true;
}
}
});
});
return allDeps;
}
module.exports = {
getAllDependenciesVersions
}