Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jsii): allow customizing tsconfig.json file name #3076

Merged
merged 7 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
);
eladb marked this conversation as resolved.
Show resolved Hide resolved
});

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')],
};
}