Skip to content

Commit

Permalink
feat(jsii): allow customizing tsconfig.json file name (#3076)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Elad Ben-Israel authored Oct 19, 2021
1 parent eca552e commit c611f26
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/jsii/bin/jsii.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('generate-tsconfig', {
type: 'string',
default: 'tsconfig.json',
desc: 'Name of the typescript configuration file to generate with compiler settings',
}),
)
.option('verbose', {
Expand Down Expand Up @@ -108,6 +113,7 @@ const warningTypes = Object.keys(enabledWarnings);
failOnWarnings: argv['fail-on-warnings'],
stripDeprecated: argv['strip-deprecated'],
addDeprecationWarnings: argv['add-deprecation-warnings'],
generateTypeScriptConfig: argv['generate-tsconfig'],
});

const emitResult = await (argv.watch ? compiler.watch() : compiler.emit());
Expand Down
9 changes: 8 additions & 1 deletion packages/jsii/lib/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +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"
*/
generateTypeScriptConfig?: string;
}

export interface TypescriptConfig {
Expand All @@ -79,9 +84,11 @@ export class Compiler implements Emitter {
},
);

const configFileName = options.generateTypeScriptConfig ?? 'tsconfig.json';

this.configPath = path.join(
this.options.projectInfo.projectRoot,
'tsconfig.json',
configFileName,
);

this.projectReferences =
Expand Down
72 changes: 71 additions & 1 deletion packages/jsii/test/compiler.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
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';

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'),
generateTypeScriptConfig: '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);
Expand Down Expand Up @@ -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: [join('**', '*.ts')],
};
}

0 comments on commit c611f26

Please sign in to comment.