This repository has been archived by the owner on Sep 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtsbuild.js
131 lines (113 loc) · 3.02 KB
/
tsbuild.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { spawnSync } from "child_process";
import { readFile, writeFile } from "fs/promises";
import * as glob from "glob";
import { dirname, join, relative, resolve } from "path";
import Prettier from "prettier";
import { fileURLToPath } from "url";
const WORKSPACE_ROOT = dirname(fileURLToPath(import.meta.url));
const PACKAGE_JSON = "package.json";
const TS_CONFIG = "tsconfig.json";
const prettierConfig = await Prettier.resolveConfig(
join(WORKSPACE_ROOT, TS_CONFIG),
);
const rootPkg = JSON.parse(
await readFile(join(WORKSPACE_ROOT, PACKAGE_JSON), "utf-8"),
);
if (
!rootPkg.workspaces ||
!Array.isArray(rootPkg.workspaces) ||
!rootPkg.workspaces.length
) {
console.error(`ERROR: no workspaces defined`);
process.exit(1);
}
const packages = rootPkg.workspaces.flatMap((x) =>
glob.sync(x, { onlyDirectories: true }).map((p) => resolve(p)),
);
if (!packages.length) {
console.error(
`ERROR: no packages found in workspaces [${rootPkg.workspaces}]`,
);
process.exit(1);
}
const refs = {};
for (const pkg of packages) {
const packageJsonPath = join(pkg, PACKAGE_JSON);
let packageJson;
try {
packageJson = JSON.parse(await readFile(packageJsonPath, "utf8"));
} catch (err) {
console.warn(`WARN: skipping ${relative(WORKSPACE_ROOT, pkg)}: %o`, err);
continue;
}
const deps = [];
if (packageJson.dependencies) {
deps.push(...Object.keys(packageJson.dependencies));
}
if (packageJson.devDependencies) {
deps.push(...Object.keys(packageJson.devDependencies));
}
let tsConfig;
const packageTsConfigPath = join(pkg, TS_CONFIG);
try {
tsConfig = JSON.parse(await readFile(packageTsConfigPath));
if (!tsConfig.compilerOptions?.composite) {
console.warn(
`skipping ${packageJson.name} because composite is not turned on`,
);
continue;
}
} catch {
continue;
}
refs[packageJson.name] = {
name: packageJson.name,
path: pkg,
deps,
tsConfig,
};
}
for (const pkg of Object.values(refs)) {
await addRefsToTsConfig(
join(pkg.path, TS_CONFIG),
pkg.deps.filter((x) => x in refs).map((x) => refs[x].path),
pkg.tsConfig,
);
}
const rootTsConfigPath = join(WORKSPACE_ROOT, TS_CONFIG);
await addRefsToTsConfig(
rootTsConfigPath,
Object.values(refs).map((x) => x.path),
);
const result = spawnSync(
join(WORKSPACE_ROOT, "node_modules/.bin/tsc"),
["-b", rootTsConfigPath, ...process.argv.slice(2)],
{
stdio: "inherit",
shell: process.platform === "win32",
},
);
process.exit(result.status);
async function addRefsToTsConfig(configPath, references, contents) {
const dir = dirname(resolve(configPath));
if (!contents) {
contents = JSON.parse(await readFile(configPath, "utf-8"));
}
const output = JSON.stringify(
{
...contents,
references: references.map((x) => ({
path: relative(dir, x).replace(/\\/g, "/"),
})),
},
null,
2,
);
await writeFile(
configPath,
await Prettier.format(output, {
filepath: configPath,
...prettierConfig,
}),
);
}