-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin.js
executable file
·116 lines (86 loc) · 3.48 KB
/
bin.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
#!/usr/bin/env node
const {existsSync, readFileSync, writeFileSync} = require(`fs`);
const {relative, resolve} = require(`path`);
const pnpSource = process.argv[2] || `pnpapi`;
const prodOnly = true;
let pnp;
try {
pnp = require(pnpSource);
} catch (error) {
if (pnpSource === `pnpapi`) {
console.error(`Couldn't load the PnP api from your environment - is Plug'n'Play enabled?`);
console.error(`Check the instruction at https://github.com/arcanis/build-pnm for more information.`);
} else {
console.error(`Couldn't load the specified file (${pnpSource}).`);
}
throw error;
}
const packageNameMaps = {path_prefix: `/`, packages: {}, scopes: {}};
const packageRefCount = new Map();
function getPackageJson(path) {
// Some packages have install errors that cause them to be removed from the disk
try {
return require(path);
} catch (error) {
return {};
}
}
function filterDevDependencies(packageInformation, dependencies) {
const pkgJson = getPackageJson(resolve(packageInformation.packageLocation, `package.json`));
if (!pkgJson.devDependencies)
return dependencies;
const copy = new Map(dependencies);
for (const [name, reference] of Object.entries(pkgJson.devDependencies))
copy.delete(name);
return copy;
}
function traverseDependencyTree(pnp, cb) {
const traversed = new Set();
function traversePackage(locator) {
const packageInformation = pnp.getPackageInformation(locator);
// Skip packages that haven't been installed
if (!packageInformation.packageLocation)
return;
// Skip packages that have already been traversed
if (traversed.has(packageInformation))
return;
// Don't recurse on the top-level devDependencies?
const dependencies = locator.name === null && prodOnly
? filterDevDependencies(packageInformation, packageInformation.packageDependencies)
: packageInformation.packageDependencies;
traversed.add(packageInformation);
cb(locator, packageInformation, dependencies);
for (const [name, reference] of dependencies.entries()) {
traversePackage({name, reference});
}
}
traversePackage(pnp.topLevel);
}
function addPackages(pnp, packages, parentInformation, packageDependencies) {
for (const [name, reference] of packageDependencies.entries()) {
const dependencyInformation = pnp.getPackageInformation({name, reference});
const dependencyLocation = dependencyInformation.packageLocation;
if (!dependencyLocation)
continue;
const path = relative(parentInformation.packageLocation, dependencyLocation) || `./`;
const pkgJson = getPackageJson(resolve(dependencyLocation, `package.json`));
const main = pkgJson.browser || pkgJson.main;
packages[name] = {path, main};
}
}
const topLevelInformation = pnp.getPackageInformation(pnp.topLevel);
traverseDependencyTree(pnp, (locator, packageInformation, packageDependencies) => {
if (locator.name === null)
return addPackages(pnp, packageNameMaps.packages, topLevelInformation, packageDependencies);
const scope = relative(topLevelInformation.packageLocation, packageInformation.packageLocation);
packageNameMaps.scopes[scope] = {packages:{}};
addPackages(pnp, packageNameMaps.scopes[scope].packages, packageInformation, packageDependencies);
});
const target = `package-name-maps.json`;
const newSource = JSON.stringify(packageNameMaps, null, 2);
let isOutdated = existsSync(target)
? readFileSync(target, `utf8`) !== newSource
: true;
if (isOutdated) {
writeFileSync(target, newSource);
}