-
Notifications
You must be signed in to change notification settings - Fork 38
/
mod.ts
603 lines (555 loc) · 16.8 KB
/
mod.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { outputDiagnostics } from "./lib/compiler.ts";
import { colors, createProjectSync, path, ts } from "./lib/mod.deps.ts";
import { PackageJsonObject } from "./lib/types.ts";
import { glob } from "./lib/utils.ts";
import { transform, TransformOutput } from "./transform.ts";
import * as compilerTransforms from "./lib/compiler_transforms.ts";
export * from "./transform.ts";
export interface BuildOptions {
/** Entrypoint(s) to the Deno module. Ex. `./mod.ts` */
entryPoints: (string | URL)[];
/** Directory to output to. */
outDir: string;
/** Type check the output (defaults to true). */
typeCheck?: boolean;
/** Collect and run test files (defaults to true). */
test?: boolean;
/** Create declaration files (defaults to true). */
declaration?: boolean;
/** Keep the test files after tests run. */
keepTestFiles?: boolean;
/** Root directory to find test files in. Defaults to the cwd. */
rootTestDir?: string;
/** Glob pattern to use to find tests files. Defaults to `deno test`'s pattern. */
testPattern?: string;
/** Package to use for shimming the `Deno` namespace. Defaults to `deno.ns` */
shimPackage?: {
name: string;
version: string;
};
/** Specifiers to map from and to. */
mappings?: {
[specifier: string]: {
/** Name of the specifier to map to. */
name: string;
/** Version to use in the package.json file.
*
* Not specifying a version will exclude it from the package.json file.
*/
version?: string;
};
};
/** Package.json output. You may override dependencies and dev dependencies in here. */
package: PackageJsonObject;
}
/** Emits the specified Deno module to an npm package using the TypeScript compiler. */
export async function build(options: BuildOptions): Promise<void> {
// set defaults
options = {
...options,
typeCheck: options.typeCheck ?? true,
test: options.test ?? true,
declaration: options.declaration ?? true,
};
await Deno.permissions.request({ name: "write", path: options.outDir });
const shimPackage = options.shimPackage ?? {
name: "deno.ns",
version: "0.5.0",
};
const specifierMappings = options.mappings && Object.fromEntries(
Object.entries(options.mappings).map(([key, value]) => {
const lowerCaseKey = key.toLowerCase();
if (
!lowerCaseKey.startsWith("http://") &&
!lowerCaseKey.startsWith("https://")
) {
key = path.toFileUrl(lowerCaseKey).toString();
}
return [key, value];
}),
);
log("Transforming...");
const transformOutput = await transformEntryPoints();
for (const warning of transformOutput.warnings) {
warn(warning);
}
const createdDirectories = new Set<string>();
const writeFile = ((filePath: string, fileText: string) => {
const dir = path.dirname(filePath);
if (!createdDirectories.has(dir)) {
Deno.mkdirSync(dir, { recursive: true });
createdDirectories.add(dir);
}
Deno.writeTextFileSync(filePath, fileText);
});
createPackageJson();
createNpmIgnore();
// npm install in order to prepare for checking TS diagnostics
log("Running npm install...");
const npmInstallPromise = runNpmCommand(["install"]);
if (options.typeCheck || options.declaration) {
// Unfortunately this can't be run in parallel to building the project
// in this case because TypeScript will resolve the npm packages when
// creating the project.
await npmInstallPromise;
}
log("Building project...");
const esmOutDir = path.join(options.outDir, "esm");
const umdOutDir = path.join(options.outDir, "umd");
const typesOutDir = path.join(options.outDir, "types");
const project = createProjectSync({
compilerOptions: {
outDir: typesOutDir,
allowJs: true,
stripInternal: true,
declaration: options.declaration,
esModuleInterop: false,
isolatedModules: true,
useDefineForClassFields: true,
experimentalDecorators: true,
jsx: ts.JsxEmit.React,
jsxFactory: "React.createElement",
jsxFragmentFactory: "React.Fragment",
importsNotUsedAsValues: ts.ImportsNotUsedAsValues.Remove,
module: ts.ModuleKind.ES2015,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
target: ts.ScriptTarget.ES2015,
allowSyntheticDefaultImports: true,
},
});
for (
const outputFile of [
...transformOutput.main.files,
...transformOutput.test.files,
]
) {
project.createSourceFile(
path.join(options.outDir, "src", outputFile.filePath),
outputFile.fileText,
);
}
let program = project.createProgram();
if (options.typeCheck) {
log("Type checking...");
const diagnostics = ts.getPreEmitDiagnostics(program);
if (diagnostics.length > 0) {
outputDiagnostics(diagnostics);
Deno.exit(1);
}
}
// emit only the .d.ts files
if (options.declaration) {
log("Emitting declaration files...");
emit({ onlyDtsFiles: true });
}
// emit the esm files
log("Emitting ESM package...");
project.compilerOptions.set({
declaration: false,
outDir: esmOutDir,
});
program = project.createProgram();
emit();
writeFile(
path.join(esmOutDir, "package.json"),
`{\n "type": "module"\n}\n`,
);
// emit the umd files
log("Emitting CommonJs package...");
project.compilerOptions.set({
declaration: false,
esModuleInterop: true,
outDir: umdOutDir,
module: ts.ModuleKind.UMD,
});
program = project.createProgram();
emit({
transformers: {
before: [compilerTransforms.transformImportMeta],
},
});
writeFile(
path.join(umdOutDir, "package.json"),
`{\n "type": "commonjs"\n}\n`,
);
// ensure this is done before running tests
await npmInstallPromise;
if (options.test) {
log("Running tests...");
await createTestLauncherScript();
await runNpmCommand(["run", "test"]);
if (!options.keepTestFiles) {
await deleteTestFiles();
}
}
log("Complete!");
function emit(
opts?: { onlyDtsFiles?: boolean; transformers?: ts.CustomTransformers },
) {
const emitResult = program.emit(
undefined,
(filePath, data, writeByteOrderMark) => {
if (writeByteOrderMark) {
data = "\uFEFF" + data;
}
writeFile(filePath, data);
},
undefined,
opts?.onlyDtsFiles,
opts?.transformers,
);
if (emitResult.diagnostics.length > 0) {
outputDiagnostics(emitResult.diagnostics);
Deno.exit(1);
}
}
function createPackageJson() {
const entryPointPath = transformOutput
.main.entryPoints[0]
.replace(/\.ts$/i, ".js");
const entryPointDtsFilePath = transformOutput
.main.entryPoints[0]
.replace(/\.ts$/i, ".d.ts");
const dependencies = {
// add dependencies from transform
...Object.fromEntries(
transformOutput.main.dependencies.map((d) => [d.name, d.version]),
),
// add specifier mappings to dependencies
...(specifierMappings && Object.fromEntries(
Object.values(specifierMappings)
.filter((v) => v.version)
.map((value) => [value.name, value.version]),
)) ?? {},
// add shim
...(transformOutput.main.shimUsed
? {
[shimPackage.name]: shimPackage.version,
}
: {}),
// override with specified dependencies
...(options.package.dependencies ?? {}),
};
const devDependencies = options.test
? ({
...(!Object.keys(dependencies).includes("chalk")
? {
"chalk": "4.1.2",
}
: {}),
...(!Object.keys(dependencies).includes("@types/node") &&
(transformOutput.main.shimUsed || transformOutput.test.shimUsed)
? {
"@types/node": "16.11.1",
}
: {}),
// add dependencies from transform
...Object.fromEntries(
transformOutput.test.dependencies.map((d) => [d.name, d.version]) ??
[],
),
// add shim if not in dependencies
...(transformOutput.test.shimUsed &&
!Object.keys(dependencies).includes(shimPackage.name)
? {
[shimPackage.name]: shimPackage.version,
}
: {}),
// override with specified dependencies
...(options.package.devDependencies ?? {}),
})
: options.package.devDependencies;
const scripts = options.test
? ({
test: "node test_runner.js",
// override with specified scripts
...(options.package.scripts ?? {}),
})
: options.package.scripts;
const packageJsonObj = {
module: `./esm/${entryPointPath}`,
main: `./umd/${entryPointPath}`,
types: `./types/${entryPointDtsFilePath}`,
...options.package,
exports: {
...(options.package.exports ?? {}),
".": {
import: `./esm/${entryPointPath}`,
require: `./umd/${entryPointPath}`,
types: options.package.types ?? `./types/${entryPointDtsFilePath}`,
...(options.package.exports?.["."] ?? {}),
},
},
scripts,
dependencies,
devDependencies,
};
writeFile(
path.join(options.outDir, "package.json"),
JSON.stringify(packageJsonObj, undefined, 2),
);
}
function createNpmIgnore() {
if (!options.test) {
return;
}
const fileText = Array.from(getTestFileNames()).join("\n");
writeFile(
path.join(options.outDir, ".npmignore"),
fileText,
);
}
async function deleteTestFiles() {
for (const file of getTestFileNames()) {
await Deno.remove(path.join(options.outDir, file));
}
}
function* getTestFileNames() {
for (const file of transformOutput.test.files) {
const filePath = file.filePath.replace(/\.ts$/i, ".js");
yield `./esm/${filePath}`;
yield `./umd/${filePath}`;
}
yield "./test_runner.js";
}
async function transformEntryPoints(): Promise<TransformOutput> {
return transform({
entryPoints: options.entryPoints,
testEntryPoints: options.test
? await glob({
pattern: getTestPattern(),
rootDir: options.rootTestDir ?? Deno.cwd(),
excludeDirs: [options.outDir],
})
: [],
shimPackageName: shimPackage.name,
specifierMappings: specifierMappings && Object.fromEntries(
Object.entries(specifierMappings).map(([key, value]) => {
return [key, value.name];
}),
),
});
}
function log(message: string) {
console.log(`[dnt] ${message}`);
}
function warn(message: string) {
console.warn(colors.yellow(`[dnt] ${message}`));
}
async function createTestLauncherScript() {
let fileText = `const chalk = require("chalk");\n`;
if (transformOutput.test.shimUsed) {
fileText += `const denoShim = require("${shimPackage.name}");\n` +
`const { testDefinitions } = require("${shimPackage.name}/test-internals");\n\n`;
}
fileText += "const filePaths = [\n";
for (const entryPoint of transformOutput.test.entryPoints) {
fileText += ` "${entryPoint.replace(/\.ts$/, ".js")}",\n`;
}
fileText += "];\n\n";
fileText += `async function main() {
for (const [i, filePath] of filePaths.entries()) {
const umdPath = "./umd/" + filePath;
if (i > 0) {
console.log("");
}
console.log("Running tests in " + chalk.underline(umdPath) + "...\\n");
require(umdPath);
await runTestDefinitions();
const esmPath = "./esm/" + filePath;
console.log("\\nRunning tests in " + chalk.underline(esmPath) + "...\\n");
await import(esmPath);
await runTestDefinitions();
}
}\n\n`;
if (transformOutput.test.shimUsed) {
fileText += `${getRunTestDefinitionsCode()}\n\n`;
}
fileText += "main();\n";
writeFile(
path.join(options.outDir, "test_runner.js"),
fileText,
);
}
async function runNpmCommand(args: string[]) {
const cmd = getCmd();
await Deno.permissions.request({ name: "run", command: cmd[0] });
const process = Deno.run({
cmd,
cwd: options.outDir,
stderr: "inherit",
stdout: "inherit",
stdin: "inherit",
});
try {
const status = await process.status();
if (!status.success) {
throw new Error(
`npm ${args.join(" ")} failed with exit code ${status.code}`,
);
}
} finally {
process.close();
}
function getCmd() {
const cmd = ["npm", ...args];
if (Deno.build.os === "windows") {
return ["cmd", "/c", ...cmd];
} else {
return cmd;
}
}
}
function getTestPattern() {
// * named `test.{ts, tsx, js, mjs, jsx}`,
// * or ending with `.test.{ts, tsx, js, mjs, jsx}`,
// * or ending with `_test.{ts, tsx, js, mjs, jsx}`
return options.testPattern ??
"**/{test.{ts,tsx,js,mjs,jsx},*.test.{ts,tsx,js,mjs,jsx},*_test.{ts,tsx,js,mjs,jsx}}";
}
}
function getRunTestDefinitionsCode() {
// todo: extract out for unit testing
return `
async function runTestDefinitions() {
const currentDefinitions = testDefinitions.splice(0, testDefinitions.length);
const testFailures = [];
for (const definition of currentDefinitions) {
process.stdout.write("test " + definition.name + " ...");
if (definition.ignored) {
process.stdout.write(" ignored\\n");
continue;
}
const context = getTestContext();
let pass = false;
try {
await definition.fn(context);
if (context.hasFailingChild) {
testFailures.push({ name: definition.name, err: new Error("Had failing test step.") });
} else {
pass = true;
}
} catch (err) {
testFailures.push({ name: definition.name, err });
}
const testStepOutput = context.getOutput();
if (testStepOutput.length > 0) {
process.stdout.write(testStepOutput);
} else {
process.stdout.write(" ");
}
process.stdout.write(getStatusText(pass ? "ok" : "fail"));
process.stdout.write("\\n");
}
if (testFailures.length > 0) {
console.log("\\nFAILURES\\n");
for (const failure of testFailures) {
console.log(failure.name);
console.log(indentText(failure.err, 1));
console.log("");
}
process.exit(1);
}
}
function getTestContext() {
return {
name: undefined,
status: "ok",
children: [],
get hasFailingChild() {
return this.children.some(c => c.status === "fail" || c.status === "pending");
},
getOutput() {
let output = "";
if (this.name) {
output += "test " + this.name + " ...";
}
if (this.children.length > 0) {
output += "\\n" + this.children.map(c => indentText(c.getOutput(), 1)).join("\\n") + "\\n";
} else if (!this.err) {
output += " ";
}
if (this.name && this.err) {
output += "\\n";
}
if (this.err) {
output += indentText(this.err.toString(), 1);
if (this.name) {
output += "\\n";
}
}
if (this.name) {
output += getStatusText(this.status);
}
return output;
},
async step(nameOrTestDefinition, fn) {
const definition = getDefinition();
const context = getTestContext();
context.status = "pending";
context.name = definition.name;
context.status = "pending";
this.children.push(context);
if (definition.ignored) {
context.status = "ignored";
return false;
}
try {
await definition.fn(context);
context.status = "ok";
if (context.hasFailingChild) {
context.status = "fail";
return false;
}
return true;
} catch (err) {
context.status = "fail";
context.err = err;
return false;
}
function getDefinition() {
if (typeof nameOrTestDefinition === "string") {
if (!(fn instanceof Function)) {
throw new TypeError("Expected function for second argument.");
}
return {
name: nameOrTestDefinition,
fn,
};
} else if (typeof nameOrTestDefinition === "object") {
return nameOrTestDefinition;
} else {
throw new TypeError(
"Expected a test definition or name and function.",
);
}
}
}
};
}
function getStatusText(status) {
switch (status) {
case "ok":
return chalk.green(status);
case "fail":
case "pending":
return chalk.red(status);
case "ignore":
return chalk.gray(status);
default:
return status;
}
}
function indentText(text, indentLevel) {
if (text === undefined) {
text = "[undefined]";
} else if (text === null) {
text = "[null]";
} else {
text = text.toString();
}
return text.split(/\\r?\\n/).map(line => " ".repeat(indentLevel) + line).join("\\n");
}
`.trim();
}