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

Fix #1425: One of the emitted tsconfig schemas is technically invalid #1618

Merged
merged 2 commits into from Feb 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 39 additions & 13 deletions scripts/create-merged-schema.ts
Expand Up @@ -15,25 +15,49 @@ async function main() {
const schemastoreSchema = await getSchemastoreSchema();

/** ts-node schema auto-generated from ts-node source code */
const typescriptNodeSchema = require('../tsconfig.schema.json');
const originalTsNodeSchema = require('../tsconfig.schema.json');
// Apply this prefix to the names of all ts-node-generated definitions
const tsnodeDefinitionPrefix = 'tsNode';
let tsNodeSchema: any = JSON.parse(
JSON.stringify(originalTsNodeSchema).replace(
/#\/definitions\//g,
`#/definitions/${tsnodeDefinitionPrefix}`
)
);
tsNodeSchema.definitions = Object.fromEntries(
Object.entries(tsNodeSchema.definitions).map(([key, value]) => [
`${tsnodeDefinitionPrefix}${key}`,
value,
])
);
// console.dir(tsNodeSchema, {
// depth: Infinity
// });

/** Patch ts-node stuff into the schemastore definition. */
const mergedSchema = {
...schemastoreSchema,
definitions: {
...schemastoreSchema.definitions,
...Object.fromEntries(
Object.entries(schemastoreSchema.definitions).filter(
([key]) => !key.startsWith(tsnodeDefinitionPrefix)
)
),
...tsNodeSchema.definitions,
tsNodeTsConfigOptions: undefined,
tsNodeTsConfigSchema: undefined,
tsNodeDefinition: {
properties: {
'ts-node': {
...typescriptNodeSchema.definitions.TsConfigOptions,
...tsNodeSchema.definitions.tsNodeTsConfigOptions,
description:
typescriptNodeSchema.definitions.TsConfigSchema.properties[
tsNodeSchema.definitions.tsNodeTsConfigSchema.properties[
'ts-node'
].description,
properties: {
...typescriptNodeSchema.definitions.TsConfigOptions.properties,
...tsNodeSchema.definitions.tsNodeTsConfigOptions.properties,
compilerOptions: {
...typescriptNodeSchema.definitions.TsConfigOptions.properties
...tsNodeSchema.definitions.tsNodeTsConfigOptions.properties
.compilerOptions,
allOf: [
{
Expand All @@ -46,14 +70,16 @@ async function main() {
},
},
},
allOf: [
// Splice into the allOf array at a spot that looks good. Does not affect
// behavior of the schema, but looks nicer if we want to submit as a PR to schemastore.
...schemastoreSchema.allOf.slice(0, 4),
{ $ref: '#/definitions/tsNodeDefinition' },
...schemastoreSchema.allOf.slice(4),
],
};
// Splice into the allOf array at a spot that looks good. Does not affect
// behavior of the schema, but looks nicer if we want to submit as a PR to schemastore.
mergedSchema.allOf = mergedSchema.allOf.filter(
(item: any) => !item.$ref?.includes('tsNode')
);
mergedSchema.allOf.splice(mergedSchema.allOf.length - 1, 0, {
$ref: '#/definitions/tsNodeDefinition',
});

writeFileSync(
resolve(__dirname, '../tsconfig.schemastore-schema.json'),
JSON.stringify(mergedSchema, null, 2)
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Expand Up @@ -349,7 +349,7 @@ export interface CreateOptions {
* `package` overrides either of the above to default behavior, which obeys package.json "type" and
* tsconfig.json "module" options.
*/
moduleTypes?: Record<string, 'cjs' | 'esm' | 'package'>;
moduleTypes?: ModuleTypes;
/**
* @internal
* Set by our configuration loader whenever a config file contains options that
Expand All @@ -366,6 +366,8 @@ export interface CreateOptions {
tsTrace?: (str: string) => void;
}

type ModuleTypes = Record<string, 'cjs' | 'esm' | 'package'>;

/** @internal */
export interface OptionBasePaths {
moduleTypes?: string;
Expand Down