-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
create-packages-registry.ts
101 lines (87 loc) · 2.81 KB
/
create-packages-registry.ts
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
import fs from "fs-extra";
import { globSync } from "glob";
import { set } from "lodash-es";
import path from "node:path";
import { createLogger, readTypedJson } from "~/utils";
import { getConfig } from "./config";
import { findPackagesGlobs } from "./find-packages-globs";
export type PackageManifest = {
name: string;
packageManager?: string;
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
main: string;
module?: string;
exports?: Record<string, { require: string; import: string }>;
files: string[];
version?: string;
typings?: string;
scripts?: Record<string, string>;
};
export type WorkspacePackageInfo = {
absoluteDir: string;
/**
* The path of the package relative to the workspace root. This is the path
* referenced in the lock file.
*/
rootRelativeDir: string;
/**
* The package.json file contents
*/
manifest: PackageManifest;
};
export type PackagesRegistry = Record<string, WorkspacePackageInfo>;
/**
* Build a list of all packages in the workspace, depending on the package
* manager used, with a possible override from the config file. The list contains
* the manifest with some directory info mapped by module name.
*/
export async function createPackagesRegistry(
workspaceRootDir: string,
workspacePackagesOverride: string[] | undefined
): Promise<PackagesRegistry> {
const log = createLogger(getConfig().logLevel);
if (workspacePackagesOverride) {
log.debug(
`Override workspace packages via config: ${workspacePackagesOverride}`
);
}
const packagesGlobs =
workspacePackagesOverride ?? findPackagesGlobs(workspaceRootDir);
const cwd = process.cwd();
process.chdir(workspaceRootDir);
const allPackages = packagesGlobs
.flatMap((glob) => globSync(glob))
/**
* Make sure to filter any loose files that might hang around.
*/
.filter((dir) => fs.lstatSync(dir).isDirectory());
const registry: PackagesRegistry = (
await Promise.all(
allPackages.map(async (rootRelativeDir) => {
const manifestPath = path.join(rootRelativeDir, "package.json");
if (!fs.existsSync(manifestPath)) {
log.warn(
`Ignoring directory ./${rootRelativeDir} because it does not contain a package.json file`
);
return;
} else {
log.debug(`Registering package ./${rootRelativeDir}`);
const manifest = await readTypedJson<PackageManifest>(
path.join(rootRelativeDir, "package.json")
);
return {
manifest,
rootRelativeDir,
absoluteDir: path.join(workspaceRootDir, rootRelativeDir),
};
}
})
)
).reduce<PackagesRegistry>(
(acc, info) => (info ? set(acc, info.manifest.name, info) : acc),
{}
);
process.chdir(cwd);
return registry;
}