From dbbb3b15b1a0d0d68bb2cfe00d9c7e2acf53da4f Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 21 May 2020 18:54:55 -0300 Subject: [PATCH 1/9] style: reformat with Prettier --- lib/index.js | 2 +- lib/utils.js | 4 +--- package.json | 1 + 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/index.js b/lib/index.js index 7ce18f7..993d92a 100755 --- a/lib/index.js +++ b/lib/index.js @@ -41,4 +41,4 @@ const { status } = spawnSync( // Delete temp config file fs.unlinkSync(tmpTsconfigPath) -process.exit(status); +process.exit(status) diff --git a/lib/utils.js b/lib/utils.js index 8461421..d0067ce 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,9 +1,7 @@ const { dirname, join } = require('path') const randomChars = () => { - return Math.random() - .toString(36) - .slice(2) + return Math.random().toString(36).slice(2) } const resolveFromModule = (moduleName, ...paths) => { diff --git a/package.json b/package.json index 2626e36..b445c69 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ } }, "prettier": { + "arrowParens": "avoid", "semi": false, "singleQuote": true, "trailingComma": "all" From 6de8553c28c8f42782dce319dbcebfef923c9113 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 21 May 2020 19:14:46 -0300 Subject: [PATCH 2/9] refactor: uninstall "yargs-parser" --- lib/index.js | 5 ++--- package.json | 3 --- yarn.lock | 15 +-------------- 3 files changed, 3 insertions(+), 20 deletions(-) diff --git a/lib/index.js b/lib/index.js index 993d92a..c23743d 100755 --- a/lib/index.js +++ b/lib/index.js @@ -2,13 +2,12 @@ const { spawnSync } = require('child_process') const fs = require('fs') -const yargsParser = require('yargs-parser') const { randomChars, resolveFromModule, resolveFromRoot } = require('./utils') -const args = yargsParser(process.argv.slice(2)) +const args = process.argv.slice(2) -const filesToCheck = args._.filter(file => /\.(ts|tsx)$/.test(file)) +const filesToCheck = args.filter(file => /\.(ts|tsx)$/.test(file)) // Nothing to be type-checked? if (filesToCheck.length === 0) { diff --git a/package.json b/package.json index b445c69..3beefb9 100644 --- a/package.json +++ b/package.json @@ -27,9 +27,6 @@ "singleQuote": true, "trailingComma": "all" }, - "dependencies": { - "yargs-parser": "^18.1.1" - }, "peerDependencies": { "typescript": "3.x" }, diff --git a/yarn.lock b/yarn.lock index 4131c52..370cf05 100644 --- a/yarn.lock +++ b/yarn.lock @@ -287,11 +287,6 @@ camelcase@^4.1.0: resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-4.1.0.tgz#d545635be1e33c542649c69173e5de6acfae34dd" integrity sha1-1UVjW+HjPFQmScaRc+Xeas+uNN0= -camelcase@^5.0.0: - version "5.3.1" - resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-5.3.1.tgz#e3c9b31569e106811df242f715725a1f4c494320" - integrity sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg== - chalk@2.4.2, chalk@^2.0.0, chalk@^2.0.1: version "2.4.2" resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424" @@ -439,7 +434,7 @@ decamelize-keys@^1.0.0: decamelize "^1.1.0" map-obj "^1.0.0" -decamelize@^1.1.0, decamelize@^1.2.0: +decamelize@^1.1.0: version "1.2.0" resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290" integrity sha1-9lNNFRSCabIDUue+4m9QH5oZEpA= @@ -1231,11 +1226,3 @@ yargs-parser@^10.0.0: integrity sha512-VCIyR1wJoEBZUqk5PA+oOBF6ypbwh5aNB3I50guxAL/quggdfs4TtNHQrSazFA3fYZ+tEqfs0zIGlv0c/rgjbQ== dependencies: camelcase "^4.1.0" - -yargs-parser@^18.1.1: - version "18.1.1" - resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-18.1.1.tgz#bf7407b915427fc760fcbbccc6c82b4f0ffcbd37" - integrity sha512-KRHEsOM16IX7XuLnMOqImcPNbLVXMNHYAoFc3BKR8Ortl5gzDbtXvvEoGx9imk5E+X1VeNKNlcHr8B8vi+7ipA== - dependencies: - camelcase "^5.0.0" - decamelize "^1.2.0" From 7e724a1394eba8e85e5a3ad791974529dd7676de Mon Sep 17 00:00:00 2001 From: Jonathan Loss Date: Mon, 27 Apr 2020 19:38:07 +0200 Subject: [PATCH 3/9] fix: run with tsc.cmd binary on Windows --- lib/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index c23743d..3c1ff4e 100755 --- a/lib/index.js +++ b/lib/index.js @@ -32,7 +32,10 @@ fs.writeFileSync(tmpTsconfigPath, JSON.stringify(tmpTsconfig, null, 2)) // Type-check our files const { status } = spawnSync( - resolveFromModule('typescript', 'bin/tsc'), + resolveFromModule( + 'typescript', + `../.bin/tsc${process.platform === 'win32' ? '.cmd' : ''}`, + ), ['-p', tmpTsconfigPath, '--noEmit'], { stdio: 'inherit' }, ) From 42c51536dceff51cf5c602e7e04872001bb3ab5a Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 21 May 2020 19:44:11 -0300 Subject: [PATCH 4/9] fix: correctly parse tsconfig.json when they have comments --- lib/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 3c1ff4e..0cc9814 100755 --- a/lib/index.js +++ b/lib/index.js @@ -16,7 +16,10 @@ if (filesToCheck.length === 0) { // Load existing config const tsconfigPath = resolveFromRoot('tsconfig.json') -const tsconfig = require(tsconfigPath) +const tsconfigContent = fs.readFileSync(tsconfigPath).toString() +// Use 'eval' to read the JSON as regular JavaScript syntax so that comments are allowed +let tsconfig = {} +eval(`tsconfig = ${tsconfigContent}`) // Write a temp config file const tmpTsconfigPath = resolveFromRoot(`tsconfig.${randomChars()}.json`) From 237231eeefdd58cb69c93e3f556195a0bf2622ee Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 21 May 2020 20:08:56 -0300 Subject: [PATCH 5/9] fix: stop forcing "--noEmit" This tool may be used to transpile specific files besides merely type-checking them, so we shouldn't actually force `--noEmit`. --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 0cc9814..faf6015 100755 --- a/lib/index.js +++ b/lib/index.js @@ -39,7 +39,7 @@ const { status } = spawnSync( 'typescript', `../.bin/tsc${process.platform === 'win32' ? '.cmd' : ''}`, ), - ['-p', tmpTsconfigPath, '--noEmit'], + ['-p', tmpTsconfigPath], { stdio: 'inherit' }, ) From cdbc37c7a8069276dceae97ede643230f48aa614 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 21 May 2020 20:25:41 -0300 Subject: [PATCH 6/9] feat: allow specifying custom "-p" or "--project" --- lib/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index faf6015..c2f6711 100755 --- a/lib/index.js +++ b/lib/index.js @@ -6,6 +6,8 @@ const fs = require('fs') const { randomChars, resolveFromModule, resolveFromRoot } = require('./utils') const args = process.argv.slice(2) +const argsProjectIndex = args.findIndex(arg => ['-p', '--project'].includes(arg)) // prettier-ignore +const argsProjectValue = argsProjectIndex !== -1 ? args[argsProjectIndex + 1] : undefined // prettier-ignore const filesToCheck = args.filter(file => /\.(ts|tsx)$/.test(file)) @@ -15,7 +17,7 @@ if (filesToCheck.length === 0) { } // Load existing config -const tsconfigPath = resolveFromRoot('tsconfig.json') +const tsconfigPath = argsProjectValue || resolveFromRoot('tsconfig.json') const tsconfigContent = fs.readFileSync(tsconfigPath).toString() // Use 'eval' to read the JSON as regular JavaScript syntax so that comments are allowed let tsconfig = {} From 745ccaf3530ff726dad512b95a6871a73f2ff150 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 21 May 2020 20:33:51 -0300 Subject: [PATCH 7/9] refactor: rename "filesToCheck" to "files" --- lib/index.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/index.js b/lib/index.js index c2f6711..43f555d 100755 --- a/lib/index.js +++ b/lib/index.js @@ -9,10 +9,8 @@ const args = process.argv.slice(2) const argsProjectIndex = args.findIndex(arg => ['-p', '--project'].includes(arg)) // prettier-ignore const argsProjectValue = argsProjectIndex !== -1 ? args[argsProjectIndex + 1] : undefined // prettier-ignore -const filesToCheck = args.filter(file => /\.(ts|tsx)$/.test(file)) - -// Nothing to be type-checked? -if (filesToCheck.length === 0) { +const files = args.filter(file => /\.(ts|tsx)$/.test(file)) +if (files.length === 0) { process.exit(0) } @@ -31,7 +29,7 @@ const tmpTsconfig = { ...tsconfig.compilerOptions, skipLibCheck: true, }, - files: filesToCheck, + files, } fs.writeFileSync(tmpTsconfigPath, JSON.stringify(tmpTsconfig, null, 2)) From 8a48f4d2dd95134d697114f1a9463f287bec60f2 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 21 May 2020 20:34:30 -0300 Subject: [PATCH 8/9] docs: fix typo in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 44d2bb1..71535b1 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ yarn add -D tsc-files I wanted to type-check **only the staged files** with [lint-staged](https://github.com/okonet/lint-staged). -Unfortunately passing specific files like `tsc --notEmit file1.ts file2.ts` will cause TypeScript to simply ignore your `tsconfig.json`. +Unfortunately passing specific files like `tsc --noEmit file1.ts file2.ts` will cause TypeScript to simply ignore your `tsconfig.json`. There's already an open issue in the TypeScript repo to support this use case — you may want to give a 👍 there: https://github.com/microsoft/TypeScript/issues/27379 From c4a545b007fd41f84488e308023e2db48171ee9e Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 21 May 2020 20:44:17 -0300 Subject: [PATCH 9/9] fix: forward args to tsc --- lib/index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 43f555d..0ebee4b 100755 --- a/lib/index.js +++ b/lib/index.js @@ -14,6 +14,11 @@ if (files.length === 0) { process.exit(0) } +const remainingArgsToForward = args + .slice() + .splice(argsProjectIndex, 2) + .filter(arg => !files.includes(arg)) + // Load existing config const tsconfigPath = argsProjectValue || resolveFromRoot('tsconfig.json') const tsconfigContent = fs.readFileSync(tsconfigPath).toString() @@ -39,7 +44,7 @@ const { status } = spawnSync( 'typescript', `../.bin/tsc${process.platform === 'win32' ? '.cmd' : ''}`, ), - ['-p', tmpTsconfigPath], + ['-p', tmpTsconfigPath, ...remainingArgsToForward], { stdio: 'inherit' }, )