-
Notifications
You must be signed in to change notification settings - Fork 0
/
tms-typescript-app-project.ts
539 lines (493 loc) · 16.3 KB
/
tms-typescript-app-project.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
import { sep } from "path";
import { Component, JsonPatch, SampleDir, TextFile } from "projen";
import {
NodePackageManager,
TypeScriptCompilerOptions,
TypeScriptModuleResolution,
TypescriptConfig,
TypescriptConfigExtends,
UpdateSnapshot,
} from "projen/lib/javascript";
import {
TypeScriptAppProject,
TypeScriptProject,
TypeScriptProjectOptions,
} from "projen/lib/typescript";
import { deepMerge } from "projen/lib/util";
export enum TmsTSConfigBase {
NODE_LTS = "node-lts",
NODE18 = "node18",
NODE20 = "node20",
}
const RESET_COMPILER_OPTIONS = {
alwaysStrict: undefined,
declaration: undefined,
esModuleInterop: undefined,
exactOptionalPropertyTypes: undefined,
experimentalDecorators: undefined,
forceConsistentCasingInFileNames: undefined,
inlineSourceMap: undefined,
inlineSources: undefined,
isolatedModules: undefined,
lib: undefined,
module: undefined,
noEmitOnError: undefined,
noFallthroughCasesInSwitch: undefined,
noImplicitAny: undefined,
noImplicitOverride: undefined,
noImplicitReturns: undefined,
noImplicitThis: undefined,
noPropertyAccessFromIndexSignature: undefined,
noUncheckedIndexedAccess: undefined,
noUnusedLocals: undefined,
noUnusedParameters: undefined,
resolveJsonModule: undefined,
skipLibCheck: undefined,
strict: undefined,
strictNullChecks: undefined,
strictPropertyInitialization: undefined,
stripInternal: undefined,
target: undefined,
} satisfies Partial<TypeScriptCompilerOptions>;
const TSCONFIG_BASE_STRICTEST_SNAPSHOT = {
strict: true,
exactOptionalPropertyTypes: true,
noFallthroughCasesInSwitch: true,
noImplicitOverride: true,
noImplicitReturns: true,
noPropertyAccessFromIndexSignature: true,
noUncheckedIndexedAccess: true,
noUnusedLocals: true,
noUnusedParameters: true,
isolatedModules: true,
esModuleInterop: true,
skipLibCheck: true,
forceConsistentCasingInFileNames: true,
// Must be applied separately, not recognized by projen
// allowUnusedLabels: false,
// allowUnreachableCode: false,
// checkJs: true,
} satisfies Partial<TypeScriptCompilerOptions>;
export interface TmsTypeScriptAppProjectOptions
extends TypeScriptProjectOptions {
/**
* Add a default bundle to the project.
*
* Will bundle ./src/ to ./dist/ using esbuild.
*
* @default true
* @featured
*/
readonly addDefaultBundle?: boolean;
/**
* Configure for ESM
*
* @default true
* @featured
*/
readonly esmSupportConfig?: boolean;
/**
* When configuring for ESM, add a banner to the bundle to support `require` and `__dirname` and `__filename`
*
* @default (esmSupportConfig === true && addDefaultBundle === true)
* @featured
*/
readonly esmSupportAddRequireShim?: boolean;
/**
* Change the default-set eslint auto-fixable rules to "warn" instead of "error"
*
* @default true
* @featured
*/
readonly eslintFixableAsWarn?: boolean;
/**
* Declare a specific node version to put in `.nvmrc` for `nvm` or `fnm` to use
*
* NOTE: As of this writing ts-node (v10.9.1) has issues with node versions 18.19.x and newer (including 20.x)
* when esm is enabled (`esmSupportConfig: true`)
*
* @default v18.18.2
* @featured
*/
readonly nodeVersion?: string;
/**
* TSConfig base configuration selection
*
* Using one of the options from https://github.com/tsconfig/bases as a base, then any explicit settings override
* those. Note that only nodes18 and above are supported.
*
* @remarks
*
* Not all options are supported, perticularly those of node version that are older than is supported by Projen.
*
* @see {@link tsconfigBaseStrictest}
*
* @default TmsTSConfigBase.Node18
*
*/
readonly tsconfigBase?: TmsTSConfigBase;
/**
* TSConfig base configuration selection for `tsconfig.dev.json`, used to run projen itslef via `ts-node`
*
* @remarks
*
* Due to a bug in `ts-node` (v10.9.1) when used with Typescript 5.3.2 the tsconfig "extends" property when referring
* to a file that's in `node_modules` will not be looked up properly. The workaround currently is to make the
* reference relative to the tsconfig file.
*
* @see {@link tsconfigBase}
*
* @default TmsTSConfigBase.Node18
*
*/
readonly tsconfigBaseDev?: TmsTSConfigBase;
/**
* Include TSConfig "strinctest" configuration to {@link tsconfigBase}
*
* Using one of the options from https://github.com/tsconfig/bases as a base, then any explicit settings override
* those. Note that only nodes18 and above are supported.
*
* @remarks
*
* If true will add the "strictest" configuration to the "extends" of the tsconfig.json file, *before* the base set by
* the {@link tsconfigBase} option.
*
* Does **not** apply to `tsconfig.dev.json`.
*
* @see {@link tsconfigBase}
*
* @default true
*
*/
readonly tsconfigBaseStrictest?: boolean;
/**
* Workaround `ts-node` bug with "extends" in `tsconfig*.json` files
*
* @remarks
*
* When {@link tsconfigBaseStrictest} is `true`, instead of extending the "strictest" configuration by making an
* `extends` array in `tsconfig.json` it will copy a snapshot the contents of the "strictest" configuration instead.
*
* Once this bug is fixed you can safely choose to set this to `false` to get the benefits of the `extends` array.
*
* @see https://github.com/TypeStrong/ts-node/issues/2000
*
* @default true
*
*/
readonly tsconfigBaseNoArrayWorkaround?: boolean;
/**
* Workaround `ts-node` bug with Node 18.19 and newer, where running `ts-node` with `esm` support enabled will fail
* with the error `ERR_UNKNOWN_FILE_EXTENSION` when you run `projen` itself.
*
* This workaround will work with node 16 and later, and has not been tested with earlier versions.
*
* THIS DOES NOT FIX ANY OTHER USAGE OF `ts-node` WITH `esm` SUPPORT, ONLY WHEN RUNNING `projen` ITSELF.
*
* If you are using `ts-node` with `esm` support in your project with Node 18.19 or newer, you will need to use the
* workaround in your own:
*
* ```bash
* # instead of
* ts-node --project tsconfig.special.json src/index.ts
*
* # use
* tsc .projenrc.ts && \
* TS_NODE_PROJECT=tsconfig.special.json node --loader ts-node/esm --no-warnings=ExperimentalWarning src/index.ts
* ```
*
* If there are any type errors, the `node --loader ts-node/esm` yields a difficult-to-read error message, so we run
* `tsc` first separately to get the type errors before running the `node` command.
*
* The `tsc` command assumes the correct tsconfig file where the target is `include`d has `noEmit` set to `true`. If
* not, add `--noemit` to the `tsc` command.
*
* @see https://github.com/TypeStrong/ts-node/issues/2094
*
* @default if (node18_19_or_newer) { true } else { false }
*
*/
readonly tsNodeUnknownFileExtensionWorkaround?: boolean;
}
/**
* Create a [TypeScriptAppProject](https://projen.io/api/API.html#typescriptappproject-) with
* [Ten Mile Square](https://tenmilesquare.com) opinionated defaults.
*
* @pjid tms-typescript-app
*/
export class TmsTypeScriptAppProject extends TypeScriptAppProject {
constructor(options: TmsTypeScriptAppProjectOptions) {
const tsconfigBaseStrictestExtended =
(options.tsconfigBaseStrictest ?? true) &&
!(options.tsconfigBaseNoArrayWorkaround ?? true);
const tsconfigBaseStrictestEmbedded =
(options.tsconfigBaseStrictest ?? true) &&
(options.tsconfigBaseNoArrayWorkaround ?? true);
const nodeVersionSplit = process.versions.node
.split(".")
.map((v) => parseInt(v, 10));
const node18_19_or_newer =
nodeVersionSplit[0] > 18 ||
(nodeVersionSplit[0] === 18 && nodeVersionSplit[1] >= 19);
const defaultOptions = {
eslint: true,
packageManager: NodePackageManager.NPM,
prettier: true,
projenrcTs: true,
projenVersion: ">=0.79.23",
nodeVersion: `v${process.versions.node}`,
vscode: true,
tsconfig: {
compilerOptions: {
...RESET_COMPILER_OPTIONS,
...(tsconfigBaseStrictestEmbedded
? TSCONFIG_BASE_STRICTEST_SNAPSHOT
: {}),
moduleResolution: TypeScriptModuleResolution.NODE16,
noEmit: options.addDefaultBundle ?? true,
},
extends: TypescriptConfigExtends.fromPaths([
...(tsconfigBaseStrictestExtended
? ["@tsconfig/strictest/tsconfig.json"]
: []),
`@tsconfig/${
options.tsconfigBase ?? TmsTSConfigBase.NODE18
}/tsconfig.json`,
]),
},
tsconfigDev: {
compilerOptions: {
...RESET_COMPILER_OPTIONS,
moduleResolution: TypeScriptModuleResolution.NODE16,
noEmit: true,
},
extends: TypescriptConfigExtends.fromPaths([
`@tsconfig/${
options.tsconfigBaseDev ?? TmsTSConfigBase.NODE18
}/tsconfig.json`,
]),
},
jest: true,
jestOptions: {
updateSnapshot: UpdateSnapshot.NEVER,
},
tsJestOptions: {
transformPattern: "^.+\\.m?[tj]sx?$",
transformOptions: {
useESM: options.esmSupportConfig ?? true,
},
},
addDefaultBundle: true,
esmSupportConfig: true,
esmSupportAddRequireShim:
(options.addDefaultBundle ?? true) &&
(options.esmSupportConfig ?? true),
tsconfigBase: TmsTSConfigBase.NODE18,
tsconfigBaseDev: TmsTSConfigBase.NODE18,
tsconfigBaseStrictest: true,
tsconfigBaseNoArrayWorkaround: true,
tsNodeUnknownFileExtensionWorkaround:
options.tsNodeUnknownFileExtensionWorkaround ?? node18_19_or_newer,
} satisfies Partial<TmsTypeScriptAppProjectOptions>;
const mergedOptions = deepMerge(
[
defaultOptions, // will get mutated
options,
],
true,
) as TmsTypeScriptAppProjectOptions;
super({ ...mergedOptions, sampleCode: false });
this.addDevDeps(
...new Set([
`@tsconfig/${mergedOptions.tsconfigBase ?? TmsTSConfigBase.NODE18}`,
`@tsconfig/${mergedOptions.tsconfigBaseDev ?? TmsTSConfigBase.NODE18}`,
...(tsconfigBaseStrictestExtended ? ["@tsconfig/strictest"] : []),
]),
"ts-node@>=10.9.2",
);
if (this.tsconfig && tsconfigBaseStrictestEmbedded) {
// Options not yet internally supported by projen
this.tsconfig.file.addOverride(
"compilerOptions.allowUnusedLabels",
false,
);
this.tsconfig.file.addOverride(
"compilerOptions.allowUnreachableCode",
false,
);
this.tsconfig.file.addOverride("compilerOptions.checkJs", true);
}
// Note: should adjust for https://eslint.style/guide/getting-started
if (mergedOptions.eslintFixableAsWarn ?? true) {
this.eslint?.addRules({
"prettier/prettier": ["warn"],
"import/order": [
"warn",
{
groups: ["builtin", "external"],
alphabetize: {
order: "asc",
caseInsensitive: true,
},
},
],
"key-spacing": ["warn"],
"no-multiple-empty-lines": ["warn"],
"no-trailing-spaces": ["warn"],
"dot-notation": ["warn"],
});
}
if (mergedOptions.nodeVersion) {
new TextFile(this, ".nvmrc", {
lines: [mergedOptions.nodeVersion],
});
}
if (mergedOptions.esmSupportConfig) {
this.package.addField("type", "module");
[this.tsconfig, this.tsconfigDev].forEach(
(tsconfig) =>
tsconfig &&
tsconfig.file.patch(
JsonPatch.add("/ts-node", {
esm: true,
preferTsExts: false,
experimentalSpecifierResolution: "node",
}),
),
);
} else {
[this.tsconfig, this.tsconfigDev].forEach(
(tsconfig) =>
tsconfig &&
tsconfig.file.patch(
JsonPatch.add("/ts-node", {
compilerOptions: {
module: "commonjs",
},
preferTsExts: true,
experimentalSpecifierResolution: "node",
}),
),
);
}
if (mergedOptions.jest && this.jest) {
this.jest.config.globals = undefined;
this.jest.config.moduleNameMapper = {
"^(\\.{1,2}/.*)\\.m?js$": "$1",
};
if (mergedOptions.esmSupportConfig ?? true) {
// "If you are using custom transform config, please remove preset from your Jest config to avoid issues that
// Jest doesn't transform files correctly."
// - https://kulshekhar.github.io/ts-jest/docs/next/guides/esm-support/
// this.jest.config.preset = "ts-jest/presets/default-esm";
// Since $NODE_OPTIONS may already have a value for debugging, we need to add to it
this.testTask.env(
"NODE_OPTIONS",
"$(echo $NODE_OPTIONS --experimental-vm-modules)",
);
}
}
if (mergedOptions.vscode) {
if (!this.vscode?.settings) {
throw new Error("vscode settings not found, but should have been");
}
if (!this.vscode?.extensions) {
throw new Error("vscode extensions not found, but should have been");
}
const settings = this.vscode?.settings;
settings.addSetting("jest.jestCommandLine", "npm test --");
settings.addSetting("jest.rootPath", "./");
const extensions = this.vscode?.extensions;
extensions.addRecommendations(
"dbaeumer.vscode-eslint",
"Orta.vscode-jest",
);
}
if (mergedOptions.addDefaultBundle ?? true) {
this.bundler.addBundle([this.srcdir, "index.ts"].join(sep), {
target: "node18",
platform: "node",
format: mergedOptions.esmSupportConfig ?? true ? "esm" : "cjs",
sourcemap: true,
watchTask: true,
...(mergedOptions.esmSupportAddRequireShim
? {
banner: `import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const __filename = (await import('node:url')).fileURLToPath(import.meta.url);
const __dirname = (await import('node:path')).dirname(__filename);
`.replace(/\n/g, ""),
}
: undefined),
});
}
if (mergedOptions.sampleCode ?? true) {
new SampleCode(this);
}
const PROJEN_TSCONFIG_FILENAME = "tsconfig.projenrc.json";
if (
mergedOptions.tsNodeUnknownFileExtensionWorkaround &&
this.defaultTask
) {
const projenTsconfig = new TypescriptConfig(this, {
fileName: PROJEN_TSCONFIG_FILENAME,
include: [
".projenrc.ts",
"projenrc/**/*.ts", // added by projen for tsconfig.dev - gives a place for projenrc included files
],
extends: TypescriptConfigExtends.fromTypescriptConfigs([
this.tsconfigDev,
]),
compilerOptions: {
...RESET_COMPILER_OPTIONS,
},
});
this.defaultTask.reset(
`tsc --project ${projenTsconfig.fileName} && node --loader ts-node/esm --no-warnings=ExperimentalWarning .projenrc.ts`,
);
this.defaultTask.env("TS_NODE_PROJECT", projenTsconfig.fileName);
this.defaultTask.description =
"Run projen with ts-node/esm (workaround for Node 18.19+ applied)";
}
}
}
class SampleCode extends Component {
constructor(project: TypeScriptProject) {
super(project);
const indexSrcCode = [
'import { Hello } from "./hello.js";',
"",
"console.log(await new Hello().sayHello(2000));",
].join("\n");
const helloSrcCode = [
'import { setTimeout } from "timers/promises";',
"export class Hello {",
" public async sayHello(delay: number = 100): Promise<string> {",
" await setTimeout(delay);",
' return "hello, world!";',
" }",
"}",
].join("\n");
const testCode = [
'import { Hello } from "../src/hello.js";',
"",
'test("hello", async () => {',
" const hello = new Hello();",
' expect(await hello.sayHello()).toBe("hello, world!");',
"});",
].join("\n");
new SampleDir(project, project.srcdir, {
files: {
"index.ts": indexSrcCode,
"hello.ts": helloSrcCode,
},
});
if (project.jest) {
new SampleDir(project, project.testdir, {
files: {
"hello.test.ts": testCode,
},
});
}
}
}