-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (73 loc) · 2.1 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
const {execSync} = require("child_process");
const path = require("path");
const fs = require("fs");
const YARN_WORKSPACES = "yarn-workspaces";
// TODO
const LERNA = "lerna";
const FIND = "find";
//
const SUPPORT = {
[YARN_WORKSPACES]: {
packagesLocations: target => {
const infoStr = execSync("yarn workspaces --silent info", {
cwd: target,
stdio: ["ignore", "pipe", "ignore"]
}).toString();
const rawInfo = JSON.parse(infoStr);
return [
target,
...Object.keys(rawInfo).map(pkgName => {
const relativePath = rawInfo[pkgName].location;
return path.join(target, relativePath);
})
];
}
}
};
const DEFAULT_TYPE = YARN_WORKSPACES;
const DEPENDENCIES_KEYS = [
"dependencies",
"devDependencies",
"peerDependencies"
];
module.exports = (target, {type = DEFAULT_TYPE, printAll = true} = {}) => {
if (!(type in SUPPORT)) throw new Error(`Unsupported type ${type}`);
const packagesLocations = SUPPORT[type]
.packagesLocations(target)
.sort(String.localeCompare);
const infos = packagesLocations.map(
pkgPath =>
JSON.parse(fs.readFileSync(path.join(pkgPath, "package.json"), "utf-8")),
{}
);
// [name]: version
const deps = {};
let error = false;
const onErr = printAll
? errStr => console.error(errStr)
: errStr => {
throw new Error(errStr);
};
infos.forEach(json => {
DEPENDENCIES_KEYS.forEach(depType => {
if (depType in json) {
const someDeps = json[depType];
Object.keys(someDeps).forEach(depName => {
const depVersion = someDeps[depName];
if (!(depName in deps)) {
deps[depName] = depVersion;
} else if (deps[depName] !== depVersion) {
error = true;
const errStr = `Dependency mismatch for ${depName}, expected ${
deps[depName]
} from previous packages but got ${depVersion} in ${
json.name
} ${depType}`;
onErr(errStr);
}
});
}
});
});
if (error) throw new Error("Dependencies mismatch");
};