diff --git a/cli.js b/cli.js index 7544d0d..02d38d4 100755 --- a/cli.js +++ b/cli.js @@ -1,9 +1,11 @@ #!/usr/bin/env node -const { spawnSync } = require('child_process') const fs = require('fs') +const { spawnSync } = require('child_process') const { dirname, join } = require('path') +const { minimatch } = require('minimatch') + const randomChars = () => { return Math.random().toString(36).slice(2) } @@ -26,24 +28,35 @@ const argsProjectIndex = args.findIndex(arg => const argsProjectValue = argsProjectIndex !== -1 ? args[argsProjectIndex + 1] : undefined -const files = args.filter(file => /\.(ts|tsx)$/.test(file)) +const argsLeaveIncludesUntouched = + args.findIndex(arg => ['-i'].includes(arg)) !== -1 + +// Load existing config +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 = {} +eval(`tsconfig = ${tsconfigContent}`) + +const isExcluded = file => + (tsconfig?.exclude ?? []).some(pattern => minimatch(file, pattern)) + +const filesInArgs = args.filter(file => /\.(ts|tsx)$/.test(file)) + +const files = filesInArgs.filter(file => !isExcluded(file)) + if (files.length === 0) { process.exit(0) } -const remainingArgsToForward = args.slice().filter(arg => !files.includes(arg)) +const remainingArgsToForward = args + .slice() + .filter(arg => !filesInArgs.includes(arg)) if (argsProjectIndex !== -1) { remainingArgsToForward.splice(argsProjectIndex, 2) } -// Load existing config -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 = {} -eval(`tsconfig = ${tsconfigContent}`) - // Write a temp config file const tmpTsconfigPath = resolveFromRoot(`tsconfig.${randomChars()}.json`) const tmpTsconfig = { @@ -53,7 +66,7 @@ const tmpTsconfig = { skipLibCheck: true, }, files, - include: [], + ...(argsLeaveIncludesUntouched ? {} : { includes: [] }), } fs.writeFileSync(tmpTsconfigPath, JSON.stringify(tmpTsconfig, null, 2)) diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..72bf338 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,65 @@ +{ + "name": "tsc-files", + "version": "0.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "tsc-files", + "version": "0.0.0", + "license": "MIT", + "dependencies": { + "minimatch": "^9.0.3" + }, + "bin": { + "tsc-files": "cli.js" + }, + "devDependencies": { + "typescript": "4.1.3" + }, + "peerDependencies": { + "typescript": ">=3" + } + }, + "node_modules/balanced-match": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", + "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" + }, + "node_modules/brace-expansion": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", + "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "dependencies": { + "balanced-match": "^1.0.0" + } + }, + "node_modules/minimatch": { + "version": "9.0.3", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.3.tgz", + "integrity": "sha512-RHiac9mvaRw0x3AYRgDC1CxAP7HTcNrrECeA8YYJeWnpo+2Q5CegtZjaotWTWxDG3UeGA1coE05iH1mPjT/2mg==", + "dependencies": { + "brace-expansion": "^2.0.1" + }, + "engines": { + "node": ">=16 || 14 >=14.17" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/typescript": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz", + "integrity": "sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=4.2.0" + } + } + } +} diff --git a/package.json b/package.json index bff1420..1172c23 100644 --- a/package.json +++ b/package.json @@ -22,5 +22,8 @@ }, "devDependencies": { "typescript": "4.1.3" + }, + "dependencies": { + "minimatch": "^9.0.3" } } diff --git a/yarn.lock b/yarn.lock index 0db10e7..65aa609 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,7 +2,26 @@ # yarn lockfile v1 +balanced-match@^1.0.0: + version "1.0.2" + resolved "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz" + integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw== + +brace-expansion@^2.0.1: + version "2.0.1" + resolved "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz" + integrity sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA== + dependencies: + balanced-match "^1.0.0" + +minimatch@^9.0.3: + version "9.0.3" + resolved "https://registry.npmjs.org/minimatch/-/minimatch-9.0.3.tgz" + integrity sha512-RHiac9mvaRw0x3AYRgDC1CxAP7HTcNrrECeA8YYJeWnpo+2Q5CegtZjaotWTWxDG3UeGA1coE05iH1mPjT/2mg== + dependencies: + brace-expansion "^2.0.1" + typescript@4.1.3: version "4.1.3" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7" + resolved "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz" integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==