-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
AutomaticVendorFederation.js
69 lines (66 loc) · 2.22 KB
/
AutomaticVendorFederation.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
const finder = require("find-package-json");
const path = require("path");
const fs = require("fs");
const AutomaticVendorFederation = ({
exclude,
ignoreVersion,
packageJson,
ignorePatchVersion = true,
shareFrom = ["dependencies"],
}) => {
let combinedDependencies;
if (!packageJson) {
throw new Error(
"AutomaticVendorFederation: You must pass the package.json file of your app"
);
}
if (shareFrom) {
if (!Array.isArray(shareFrom)) {
throw new Error("AutomaticVendorFederation: shareFrom must be an array");
}
combinedDependencies = shareFrom.reduce((acc, jsonKey) => {
Object.assign(acc, packageJson[jsonKey]);
return acc;
}, {});
}
const shareableDependencies = Object.keys(combinedDependencies).filter(
(dependency) => {
if (exclude.some((dep) => dependency.includes(dep))) return false;
return dependency;
}
);
return shareableDependencies.reduce((shared, pkg) => {
let packageVersion;
try {
const packageExists = fs.existsSync(
path.join(path.dirname(require.resolve(pkg)), "/package.json")
);
if (packageExists) {
console.log('package exists')
const resolvedPackage = path.join(
path.dirname(require.resolve(pkg)),
"/package.json"
);
packageVersion = require(resolvedPackage).version.split(".");
} else {
console.log("searching for package");
const f = finder(path.dirname(require.resolve(pkg)));
const jsonValue = f.next().value;
packageVersion = require(f.next().filename).version.split(".");
}
console.log(packageVersion);
if (ignorePatchVersion) {
packageVersion.pop();
}
if (ignoreVersion && ignoreVersion.includes(pkg)) {
Object.assign(shared, {[pkg]: pkg});
} else {
Object.assign(shared, {[`${pkg}-${packageVersion.join(".")}`]: pkg});
}
} catch (e) {
return shared
}
return shared;
}, {});
};
module.exports = AutomaticVendorFederation;