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 4 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('tsconfig-filename', {
eladb marked this conversation as resolved.
Show resolved Hide resolved
type: 'string',
default: 'tsconfig.json',
desc: 'The name of the TypeScript configuration file to generate',
}),
)
.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'],
tsconfigFileName: argv['tsconfig-filename'],
});

const emitResult = await (argv.watch ? compiler.watch() : compiler.emit());
Expand Down
11 changes: 10 additions & 1 deletion packages/jsii/lib/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -79,11 +85,14 @@ export class Compiler implements Emitter {
},
);

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

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


eladb marked this conversation as resolved.
Show resolved Hide resolved
this.projectReferences =
options.projectReferences !== undefined
? options.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'),
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);
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')],
};
}