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

Configure ts-node via tsconfig #921

Merged
merged 26 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ coverage/
.DS_Store
npm-debug.log
dist/
tsconfig.schema.json
tsconfig.schemastore-schema.json
214 changes: 214 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@
"files": [
"dist/",
"register/",
"LICENSE"
"LICENSE",
"tsconfig.schema.json",
"tsconfig.schemastore-schema.json"
],
"scripts": {
"lint": "tslint \"src/**/*.ts\" --project tsconfig.json",
"build": "rimraf dist && tsc",
"lint:fix": "tslint \"src/**/*.ts\" --project tsconfig.json --fix",
"clean": "rimraf dist && rimraf tsconfig.schema.json && rimraf tsconfig.schemastore-schema.json",
"build": "npm run clean && npm run build:tsc && npm run build:configSchema",
"build:tsc": "tsc",
"build:configSchema": "typescript-json-schema --topRef --refs --validationKeywords allOf --out tsconfig.schema.json tsconfig.json TsConfigSchema && node --require ./register ./scripts/create-merged-schema",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Consistent dash casing is currently used.

"test-spec": "mocha dist/**/*.spec.js -R spec --bail",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- \"dist/**/*.spec.js\" -R spec --bail",
"test": "npm run build && npm run lint && npm run test-cov",
Expand Down Expand Up @@ -55,6 +61,7 @@
"@types/react": "^16.0.2",
"@types/semver": "^6.0.0",
"@types/source-map-support": "^0.5.0",
"axios": "^0.19.0",
"chai": "^4.0.1",
"istanbul": "^0.4.0",
"mocha": "^6.1.4",
Expand All @@ -65,7 +72,8 @@
"semver": "^6.1.0",
"tslint": "^5.11.0",
"tslint-config-standard": "^9.0.0",
"typescript": "^3.7.2"
"typescript": "^3.7.2",
"typescript-json-schema": "0.40.0"
},
"peerDependencies": {
"typescript": ">=2.7"
Expand Down
58 changes: 58 additions & 0 deletions scripts/create-merged-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env ts-node
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this line doesn't work, since we use node --require in the script?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should work if you have ts-node installed globally, but that's not technically true all the time, so I put node --require in the package script.

/*
* Create a complete JSON schema for tsconfig.json
* by merging the schemastore schema with our ts-node additions.
* This merged schema can be submitted in a pull request to
* SchemaStore.
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this script necessary? Why can't we specify a URL within our schema to have it already be up-to-date with the remote schema? I'm worried this will mean that every TypeScript version I'll need to be generating and releasing the updated schema. You're meant to be able to use $ref for this use-case: https://cswr.github.io/JsonSchema/spec/definitions_references/.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, I'm generating 2 schemas. First is tsconfig.schema.json, which does use $ref. That was my initial approach, same thinking as you.

@G-Rath suggested patching ts-node's additions into the official file on Schemastore. This script produces that merged schema. The idea is that it would live in SchemaStore, so editors would tab-complete the ts-node fields without any configuration on the user's part. I'm not sure if they'll ever merge it, though, because if someone tab-completes the ts-node options, they'll still need to install ts-node. It could be nice publicity for the library; I dunno.

Copy link
Contributor

@G-Rath G-Rath Dec 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They will merge it, since it's not a breaking change (they don't reject PRs - like, ever) :)

The configuration options are harmless if ts-node isn't there.


import axios from 'axios';
import * as Path from 'path';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Casing on Path should be path, but you could just import resolve only.

import * as fs from 'fs';

async function main() {
/** schemastore definition */
const schemastoreSchema = (await axios.get(
'https://schemastore.azurewebsites.net/schemas/json/tsconfig.json',
{ responseType: "json" }
)).data;

/** ts-node schema auto-generated from ts-node source code */
const typescriptNodeSchema = require('../tsconfig.schema.json');

/** Patch ts-node stuff into the schemastore definition. */
const mergedSchema = {
...schemastoreSchema,
definitions: {
...schemastoreSchema.definitions,
tsNodeDefinition: {
properties: {
'ts-node': {
...typescriptNodeSchema.definitions.TsConfigOptions,
description: typescriptNodeSchema.definitions.TsConfigSchema.properties['ts-node'].description,
properties: {
...typescriptNodeSchema.definitions.TsConfigOptions.properties,
compilerOptions: {
...typescriptNodeSchema.definitions.TsConfigOptions.properties.compilerOptions,
allOf: [{
$ref: '#/definitions/compilerOptionsDefinition/properties/compilerOptions'
}]
}
}
}
}
},
},
allOf: [
...schemastoreSchema.allOf.slice(0, 4),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean? Why 4?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just so it gets spliced into the array at a point that looks nice. https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/tsconfig.json#L630-L631

{ "$ref": "#/definitions/tsNodeDefinition" },
...schemastoreSchema.allOf.slice(4),
]
};
fs.writeFileSync(
Path.resolve(__dirname, '../tsconfig.schemastore-schema.json'),
JSON.stringify(mergedSchema, null, 2)
);
}

main();
Loading