-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure.mjs
349 lines (309 loc) · 10.6 KB
/
configure.mjs
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import {
NinjaBuilder,
getInput,
orderOnlyDeps,
validations,
} from "@ninjutsu-build/core";
import {
getEntryPointsFromConfig,
makeTSCRule,
makeTypeCheckRule,
} from "@ninjutsu-build/tsc";
import { makeNodeTestRule } from "@ninjutsu-build/node";
import {
makeCheckFormattedRule,
makeFormatRule,
makeLintRule,
} from "@ninjutsu-build/biome";
import { basename, dirname, extname, join, relative } from "node:path/posix";
import {
resolve as resolveNative,
relative as relativeNative,
} from "node:path";
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { platform } from "os";
import isCi from "is-ci";
if (isCi) {
console.log("Running in CI mode");
}
// Create a rule to run `npm ci` in a particular directory
function makeNpmCiRule(ninja) {
const prefix = platform() === "win32" ? "cmd /c " : "";
const npmci = ninja.rule("npmci", {
command: prefix + "npm ci $args --silent",
description: "npm ci $args",
generator: 1,
});
return (a) => {
const { args = "", ...rest } = a;
const cwd = dirname(getInput(a.in));
const withCwd = cwd !== "." ? ` --prefix ${cwd}` : "";
return npmci({
...a,
out: join(cwd, "node_modules", ".package-lock.json"),
args: withCwd + args,
...rest,
});
};
}
function makeTarRule(ninja) {
// Intentionally avoid using `$in` as it must be the full path of the files
// we want to add in order for ninja to set up the dependencies correctly, but
// most of the time we would like to `tar` from a subdirectory. So we keep
// `$in` to help ninja, but we -C into our directory and strip the prefix
// from `$in` and save as the `$files` variable.
const tar = ninja.rule("tar", {
command: "tar -czf $out $args $files",
description: "Creating archive $out",
});
return (a) => {
const { dir, ...rest } = a;
return tar({
...rest,
files:
dir === undefined ? a.in : a.in.map((i) => relative(dir, i)).join(" "),
args: a.dir === undefined ? undefined : "-C " + a.dir,
});
};
}
function makeCopyRule(ninja) {
return ninja.rule("copy", {
command: "cp $in $out",
description: "Copying $in to $out",
});
}
// Given a path to a JS file, return the filename of the
// resulting TS file
function getTSFileName(jspath) {
const ext = extname(jspath);
const extLookup = {
".ts": ".js",
".mts": ".mjs",
".cts": ".cjs",
};
return basename(jspath, ext) + extLookup[ext];
}
// Create a rule to run `swc`, which we used to transpile TypeScript
function makeSWCRule(ninja) {
const swcPath = relativeNative(
resolveNative(process.cwd(), ninja.outputDir),
fileURLToPath(import.meta.resolve("@swc/cli")),
);
const node = platform() === "win32" ? "node.exe" : "node";
const swc = ninja.rule("swc", {
command: `${node} ${swcPath} $in -o $out -q $args`,
description: "Transpiling $in",
});
return (a) => {
const { outDir, ...rest } = a;
const input = getInput(a.in);
const type = extname(input) === ".mts" ? "es6" : "commonjs";
return swc({
out: join(outDir, getTSFileName(getInput(a.in))),
...rest,
args: `-C jsc.target=es2018 -C module.type=${type} -C jsc.parser.syntax=typescript -C module.importInterop=node`,
});
};
}
function formatAndLint(file) {
const formatted = format({ in: file });
return lint({ in: formatted });
}
const ninja = new NinjaBuilder({
builddir: ".builddir",
ninja_required_version: "1.11", // validations were added in 1.11
});
const workspacePkg = "package.json";
const workspaceJSON = JSON.parse(readFileSync(workspacePkg));
ninja.output += "\n";
ninja.comment("Rules + Installation");
const npmci = makeNpmCiRule(ninja);
const { phony } = ninja;
const packagesLinked = npmci({ in: workspacePkg, args: "--workspaces" });
const biomeConfig = "configure/biome.json";
// We would like to check whether `package.json` is formatted correctly.
// Most of the rules inject a build-order dependency on `npm ci` having
// run correctly, but we also need a validation dependency from running
// `npm ci` so we have a cycle (in JS only, ninja is happy with a cycle
// containing a validations edge). This means it's a bit convoluted to
// create the `checkFormatted` rule but that what the code below does.
let checkFormatted;
const toolsInstalled = npmci({
in: "configure/package.json",
[validations]: (toolsInstalled) => {
checkFormatted = makeCheckFormattedRule(ninja, {
configPath: biomeConfig,
[orderOnlyDeps]: toolsInstalled,
});
// Add a validation that `package.json` is formatted correctly.
// If we formatted after running `npmci` it would cause us to run it again
return checkFormatted({ in: "configure/package.json" })[validations];
},
});
const tsc = makeTSCRule(ninja, { [orderOnlyDeps]: toolsInstalled });
const typecheck = makeTypeCheckRule(ninja, {
[orderOnlyDeps]: toolsInstalled,
});
const test = makeNodeTestRule(ninja);
const tar = makeTarRule(ninja);
const format = isCi
? checkFormatted
: makeFormatRule(ninja, {
configPath: biomeConfig,
[orderOnlyDeps]: toolsInstalled,
});
const copy = makeCopyRule(ninja);
const lint = makeLintRule(ninja, {
configPath: biomeConfig,
[orderOnlyDeps]: toolsInstalled,
});
const transpile = makeSWCRule(ninja, {
[orderOnlyDeps]: toolsInstalled,
});
format({ in: "configure/configure.mjs" });
const baseConfig = format({ in: "tsconfig.json" });
// Create a list of all targets that need to be ready before we
// can run `typedoc`.
const docsDependencies = [];
// Go through all of the packages in our workspaces to lint, format,
// typecheck, transpile, and run tests, making sure that we have set
// up the correct intra-package dependencies
const scope = "@ninjutsu-build/";
for (const cwd of workspaceJSON.workspaces) {
const localPKGJSON = JSON.parse(
readFileSync(join(cwd, "package.json")).toString(),
);
// Build up our dependencies that come from npm or locally linking
const localDependecies = Object.keys({
...localPKGJSON.dependencies,
...localPKGJSON.devDependencies,
...localPKGJSON.peerDependencies,
}).filter((d) => d.startsWith(scope));
// Assume there is a target "@ninjutsu-build/foo/runnable" when the
// `foo` package can be executed.
const dependenciesRunnable = [packagesLinked].concat(
localDependecies.map((d) => `${d}/runnable`),
);
// Assume there is a target "@ninjutsu-build/foo/typed" when the `foo`
// package has all type declarations
const dependenciesTyped = [packagesLinked].concat(
localDependecies.map((d) => `${d}/typed`),
);
ninja.output += "\n";
ninja.comment(cwd);
// Format package.json
const packageJSON = format({ in: join(cwd, "package.json") });
// Grab all TypeScript source files and format them
const tsconfig = format({ in: join(cwd, "tsconfig.json") });
const sources = (await getEntryPointsFromConfig(ninja, tsconfig)).map(
formatAndLint,
);
const outDir = join(cwd, "dist");
// Transpile the TypeScript into JavaScript, do this separately from `tsc`
// as `swc` is much faster and this allows us to start executing unit tests
// in parallel to typechecking and type declaration generation
const javascript = sources.map((s) =>
transpile({
in: s,
outDir,
}),
);
// Create a phony target for when the package has all its JavaScript built
// and it is ready to be executed. This will be used by depenendent
// packages to rely on before they can invoke their unit tests.
const packageRunnable = phony({
out: `${localPKGJSON.name}/runnable`,
in: [packageJSON, ...javascript, ...dependenciesRunnable],
});
// Create the TypeScript type declaration files and do typechecking.
const typeDeclarations = await tsc({
tsConfig: tsconfig,
compilerOptions: {
declaration: true,
emitDeclarationOnly: true,
outDir,
},
[orderOnlyDeps]: [...dependenciesTyped, baseConfig],
});
// Create a phony target for when the package has its types generated and
// it can be used from other packages wanting to generate types or type
// check their own code.
const packageHasTypes = phony({
out: `${localPKGJSON.name}/typed`,
in: [packageJSON, ...typeDeclarations],
});
docsDependencies.push(packageHasTypes);
// Format, lint, typecheck, tranpile, and run any unit tests
const testTargets = await (async () => {
if (!existsSync(join(cwd, "tsconfig.tests.json"))) {
return [];
}
const testTSConfig = format({ in: join(cwd, "tsconfig.tests.json") });
const tests = await getEntryPointsFromConfig(ninja, testTSConfig);
if (tests.length === 0) {
return [];
}
const testsFormatted = tests.map(formatAndLint);
return (
await typecheck({
tsConfig: testTSConfig,
out: join(cwd, "dist", "typechecked.stamp"),
[orderOnlyDeps]: [packageHasTypes, baseConfig, ...testsFormatted],
})
).map((t) => {
const js = transpile({
in: t,
outDir,
[orderOnlyDeps]: testsFormatted,
});
return test({
in: js,
out: join("$builddir", cwd, `${js}.result.txt`),
[orderOnlyDeps]: packageRunnable,
});
});
})();
// Tar and gzip our entire package so it can be published to npm
const createTar = (() => {
// We assume packages are published if and only if they have a version number.
// This allows us to avoid creating an archive for the `integration` package.
if (localPKGJSON.version === undefined) {
return [];
}
// Prepare our files to create a tgz of our package, include
// - README.md
// - package.json
// - contents of `lib`
// - contents of `dist`
const stageForTar = (args) => {
const { in: _in, ...rest } = args;
return copy({
in: _in,
out: `$builddir/${cwd}/${relative(cwd, getInput(_in))}`,
...rest,
});
};
let toPack = [];
toPack.push(stageForTar({ in: join(cwd, "README.md") }));
toPack.push(stageForTar({ in: packageJSON }));
toPack = toPack.concat(javascript.map((file) => stageForTar({ in: file })));
return [
tar({
out: `$builddir/${localPKGJSON.name}.tgz`,
in: toPack,
dir: "$builddir/packages",
}),
];
})();
// Create a alias for building and testing the whole package
phony({
out: localPKGJSON.name,
in: [packageHasTypes, packageRunnable, ...createTar, ...testTargets],
});
}
// Create a target that can be used for anyone wanting to prep the
// project to do the bare minimum in order to generate documentation
phony({ out: "prep-for-docs", in: docsDependencies });
// Finally, write the resulting file to disk
writeFileSync("build.ninja", ninja.output);