Skip to content

Commit

Permalink
Fix #1425: One of the emitted tsconfig schemas is technically invalid (
Browse files Browse the repository at this point in the history
…#1618)

* fix

* lint-fix
  • Loading branch information
cspotcode committed Feb 1, 2022
1 parent 37b1d4a commit 5284051
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
52 changes: 39 additions & 13 deletions scripts/create-merged-schema.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 5284051

Please sign in to comment.