This repository has been archived by the owner on May 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
pnp-name-maps.js
59 lines (42 loc) · 1.96 KB
/
pnp-name-maps.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
const {relative, resolve} = require(`path`);
const pnp = require(process.argv[2] || `pnpapi`);
const packageNameMaps = {path_prefix: `/`, packages: {}, scopes: {}};
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;
traversed.add(packageInformation);
cb(locator, packageInformation);
for (const [name, reference] of packageInformation.packageDependencies.entries()) {
traversePackage({name, reference});
}
}
traversePackage(pnp.topLevel);
}
function addPackages(pnp, packages, parentInformation, parentDependencies) {
for (const [name, reference] of parentDependencies.entries()) {
const dependencyInformation = pnp.getPackageInformation({name, reference});
const dependencyLocation = dependencyInformation.packageLocation;
if (!dependencyLocation)
continue;
const path = relative(parentInformation.packageLocation, dependencyLocation) || `./`;
const pkgJson = require(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);
});
console.log(JSON.stringify(packageNameMaps, null, 2));