From fe3c9cd94fbea50ee0c5b81d0bcf009f9bf07849 Mon Sep 17 00:00:00 2001 From: Elad Ben-Israel Date: Mon, 18 Oct 2021 16:14:26 +0300 Subject: [PATCH 1/5] feat(jsii): allow customizing tsconfig.json file name In order to support environment in which users want to use the default name tsconfig.json for a different compiler configuration (e.g. configuration that includes both sources and test files), add a switch to the jsii compiler that allows customizing the name of the generated tsconfig.json file. --- packages/jsii/bin/jsii.ts | 6 +++ packages/jsii/lib/compiler.ts | 12 ++++- packages/jsii/test/compiler.test.ts | 72 ++++++++++++++++++++++++++++- 3 files changed, 88 insertions(+), 2 deletions(-) diff --git a/packages/jsii/bin/jsii.ts b/packages/jsii/bin/jsii.ts index 15ddbb8a69..c67d525656 100644 --- a/packages/jsii/bin/jsii.ts +++ b/packages/jsii/bin/jsii.ts @@ -64,6 +64,11 @@ const warningTypes = Object.keys(enabledWarnings); type: 'boolean', default: false, desc: '[EXPERIMENTAL] Injects warning statements for all deprecated elements, to be printed at runtime', + }) + .option('tsconfig-filename', { + type: 'string', + default: 'tsconfig.json', + desc: 'The name of the TypeScript configuration file to generate', }), ) .option('verbose', { @@ -108,6 +113,7 @@ const warningTypes = Object.keys(enabledWarnings); failOnWarnings: argv['fail-on-warnings'], stripDeprecated: argv['strip-deprecated'], addDeprecationWarnings: argv['add-deprecation-warnings'], + tsconfigFileName: argv['tsconfig-filename'], }); const emitResult = await (argv.watch ? compiler.watch() : compiler.emit()); diff --git a/packages/jsii/lib/compiler.ts b/packages/jsii/lib/compiler.ts index 0a02b9d735..20851a9a25 100644 --- a/packages/jsii/lib/compiler.ts +++ b/packages/jsii/lib/compiler.ts @@ -54,6 +54,12 @@ export interface CompilerOptions { stripDeprecated?: boolean; /** Whether to add warnings for deprecated elements */ addDeprecationWarnings?: boolean; + + /** + * The name of the tsconfig file to generate + * @default "tsconfig.json" + */ + tsconfigFileName?: string; } export interface TypescriptConfig { @@ -79,11 +85,15 @@ export class Compiler implements Emitter { }, ); + const configFileName = options.tsconfigFileName ?? 'tsconfig.json'; + this.configPath = path.join( this.options.projectInfo.projectRoot, - 'tsconfig.json', + configFileName, ); + console.log(this.configPath); + this.projectReferences = options.projectReferences !== undefined ? options.projectReferences diff --git a/packages/jsii/test/compiler.test.ts b/packages/jsii/test/compiler.test.ts index 858637e90a..41cfa62f7b 100644 --- a/packages/jsii/test/compiler.test.ts +++ b/packages/jsii/test/compiler.test.ts @@ -1,4 +1,4 @@ -import { mkdtemp, remove, writeFile, readFile } from 'fs-extra'; +import { mkdtemp, remove, writeFile, readFile, readJson } from 'fs-extra'; import { tmpdir } from 'os'; import { join } from 'path'; @@ -6,6 +6,40 @@ import { Compiler } from '../lib/compiler'; import { ProjectInfo } from '../lib/project-info'; describe(Compiler, () => { + describe('generated tsconfig', () => { + test('default is tsconfig.json', async () => { + const sourceDir = await mkdtemp( + join(tmpdir(), 'jsii-compiler-watch-mode-'), + ); + + const compiler = new Compiler({ + projectInfo: _makeProjectInfo(sourceDir, 'index.d.ts'), + }); + + await compiler.emit(); + + expect(await readJson(join(sourceDir, 'tsconfig.json'), 'utf-8')).toEqual( + expectedTypeScriptConfig(), + ); + }); + + test('file name can be customized', async () => { + const sourceDir = await mkdtemp( + join(tmpdir(), 'jsii-compiler-watch-mode-'), + ); + + const compiler = new Compiler({ + projectInfo: _makeProjectInfo(sourceDir, 'index.d.ts'), + tsconfigFileName: 'tsconfig.jsii.json', + }); + + await compiler.emit(); + + expect( + await readJson(join(sourceDir, 'tsconfig.jsii.json'), 'utf-8'), + ).toEqual(expectedTypeScriptConfig()); + }); + }); test('"watch" mode', async () => { // This can be a little slow, allowing 15 seconds maximum here (default is 5 seconds) jest.setTimeout(15_000); @@ -91,3 +125,39 @@ function _makeProjectInfo(sourceDir: string, types: string): ProjectInfo { excludeTypescript: [], }; } + +function expectedTypeScriptConfig() { + return { + _generated_by_jsii_: + 'Generated by jsii - safe to delete, and ideally should be in .gitignore', + compilerOptions: { + alwaysStrict: true, + charset: 'utf8', + composite: false, + declaration: true, + experimentalDecorators: true, + incremental: true, + inlineSourceMap: true, + inlineSources: true, + lib: ['es2019'], + module: 'CommonJS', + newLine: 'lf', + noEmitOnError: true, + noFallthroughCasesInSwitch: true, + noImplicitAny: true, + noImplicitReturns: true, + noImplicitThis: true, + noUnusedLocals: true, + noUnusedParameters: true, + resolveJsonModule: true, + strict: true, + strictNullChecks: true, + strictPropertyInitialization: true, + stripInternal: false, + target: 'ES2019', + tsBuildInfoFile: 'tsconfig.tsbuildinfo', + }, + exclude: ['node_modules'], + include: ['**/*.ts'], + }; +} From 2fb06b13456195f1e4e0b65daf71b3c3e9d0a992 Mon Sep 17 00:00:00 2001 From: Elad Ben-Israel Date: Tue, 19 Oct 2021 10:15:01 +0300 Subject: [PATCH 2/5] use path.join --- packages/jsii/test/compiler.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jsii/test/compiler.test.ts b/packages/jsii/test/compiler.test.ts index 41cfa62f7b..c821baa4ec 100644 --- a/packages/jsii/test/compiler.test.ts +++ b/packages/jsii/test/compiler.test.ts @@ -158,6 +158,6 @@ function expectedTypeScriptConfig() { tsBuildInfoFile: 'tsconfig.tsbuildinfo', }, exclude: ['node_modules'], - include: ['**/*.ts'], + include: [join('**', '*.ts')], }; } From 5dbc5f229761a93f14106a0db748ae1be0212507 Mon Sep 17 00:00:00 2001 From: Elad Ben-Israel Date: Tue, 19 Oct 2021 11:02:11 +0300 Subject: [PATCH 3/5] Update packages/jsii/lib/compiler.ts --- packages/jsii/lib/compiler.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/jsii/lib/compiler.ts b/packages/jsii/lib/compiler.ts index 20851a9a25..67e31954d7 100644 --- a/packages/jsii/lib/compiler.ts +++ b/packages/jsii/lib/compiler.ts @@ -92,7 +92,6 @@ export class Compiler implements Emitter { configFileName, ); - console.log(this.configPath); this.projectReferences = options.projectReferences !== undefined From b5edee8b318902173899069c26f7d6e53478b771 Mon Sep 17 00:00:00 2001 From: Elad Ben-Israel Date: Tue, 19 Oct 2021 11:02:26 +0300 Subject: [PATCH 4/5] Update packages/jsii/lib/compiler.ts --- packages/jsii/lib/compiler.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/jsii/lib/compiler.ts b/packages/jsii/lib/compiler.ts index 67e31954d7..329562d180 100644 --- a/packages/jsii/lib/compiler.ts +++ b/packages/jsii/lib/compiler.ts @@ -92,7 +92,6 @@ export class Compiler implements Emitter { configFileName, ); - this.projectReferences = options.projectReferences !== undefined ? options.projectReferences From 467b02ed07e6279002414cdb7093fa08163494ce Mon Sep 17 00:00:00 2001 From: Elad Ben-Israel Date: Tue, 19 Oct 2021 12:11:25 +0300 Subject: [PATCH 5/5] rename cli option --- packages/jsii/bin/jsii.ts | 6 +++--- packages/jsii/lib/compiler.ts | 7 ++----- packages/jsii/test/compiler.test.ts | 2 +- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/jsii/bin/jsii.ts b/packages/jsii/bin/jsii.ts index c67d525656..802e99873b 100644 --- a/packages/jsii/bin/jsii.ts +++ b/packages/jsii/bin/jsii.ts @@ -65,10 +65,10 @@ const warningTypes = Object.keys(enabledWarnings); default: false, desc: '[EXPERIMENTAL] Injects warning statements for all deprecated elements, to be printed at runtime', }) - .option('tsconfig-filename', { + .option('generate-tsconfig', { type: 'string', default: 'tsconfig.json', - desc: 'The name of the TypeScript configuration file to generate', + desc: 'Name of the typescript configuration file to generate with compiler settings', }), ) .option('verbose', { @@ -113,7 +113,7 @@ const warningTypes = Object.keys(enabledWarnings); failOnWarnings: argv['fail-on-warnings'], stripDeprecated: argv['strip-deprecated'], addDeprecationWarnings: argv['add-deprecation-warnings'], - tsconfigFileName: argv['tsconfig-filename'], + generateTypeScriptConfig: argv['generate-tsconfig'], }); const emitResult = await (argv.watch ? compiler.watch() : compiler.emit()); diff --git a/packages/jsii/lib/compiler.ts b/packages/jsii/lib/compiler.ts index 20851a9a25..bb1098127f 100644 --- a/packages/jsii/lib/compiler.ts +++ b/packages/jsii/lib/compiler.ts @@ -54,12 +54,11 @@ export interface CompilerOptions { stripDeprecated?: boolean; /** Whether to add warnings for deprecated elements */ addDeprecationWarnings?: boolean; - /** * The name of the tsconfig file to generate * @default "tsconfig.json" */ - tsconfigFileName?: string; + generateTypeScriptConfig?: string; } export interface TypescriptConfig { @@ -85,15 +84,13 @@ export class Compiler implements Emitter { }, ); - const configFileName = options.tsconfigFileName ?? 'tsconfig.json'; + const configFileName = options.generateTypeScriptConfig ?? 'tsconfig.json'; this.configPath = path.join( this.options.projectInfo.projectRoot, configFileName, ); - console.log(this.configPath); - this.projectReferences = options.projectReferences !== undefined ? options.projectReferences diff --git a/packages/jsii/test/compiler.test.ts b/packages/jsii/test/compiler.test.ts index c821baa4ec..ae82311d9c 100644 --- a/packages/jsii/test/compiler.test.ts +++ b/packages/jsii/test/compiler.test.ts @@ -30,7 +30,7 @@ describe(Compiler, () => { const compiler = new Compiler({ projectInfo: _makeProjectInfo(sourceDir, 'index.d.ts'), - tsconfigFileName: 'tsconfig.jsii.json', + generateTypeScriptConfig: 'tsconfig.jsii.json', }); await compiler.emit();