diff --git a/cli.js b/cli.js index 7544d0d..db419bb 100755 --- a/cli.js +++ b/cli.js @@ -17,6 +17,19 @@ const resolveFromRoot = (...paths) => { return join(process.cwd(), ...paths) } +const getTsConfigForTypeScriptFile = (filePath) => { + let currentDirectory = dirname(filePath) + while (currentDirectory && currentDirectory !== '/' && currentDirectory !== '.') { + const tsconfigPath = join(currentDirectory, 'tsconfig.json') + if (fs.existsSync(tsconfigPath)) { + return tsconfigPath + } + currentDirectory = dirname(currentDirectory) + } + + return resolveFromRoot('tsconfig.json') +} + const args = process.argv.slice(2) const argsProjectIndex = args.findIndex(arg => @@ -38,51 +51,58 @@ if (argsProjectIndex !== -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}`) - -// Write a temp config file -const tmpTsconfigPath = resolveFromRoot(`tsconfig.${randomChars()}.json`) -const tmpTsconfig = { - ...tsconfig, - compilerOptions: { - ...tsconfig.compilerOptions, - skipLibCheck: true, - }, - files, - include: [], -} -fs.writeFileSync(tmpTsconfigPath, JSON.stringify(tmpTsconfig, null, 2)) - -// Attach cleanup handlers -let didCleanup = false -for (const eventName of ['exit', 'SIGHUP', 'SIGINT', 'SIGTERM']) { - process.on(eventName, exitCode => { - if (didCleanup) return - didCleanup = true - - fs.unlinkSync(tmpTsconfigPath) - - if (eventName !== 'exit') { - process.exit(exitCode) - } - }) -} - -// Type-check our files -const { status } = spawnSync( - // See: https://github.com/gustavopch/tsc-files/issues/44#issuecomment-1250783206 - process.versions.pnp - ? 'tsc' - : resolveFromModule( +const tsconfigPaths = argsProjectValue ? [argsProjectValue] : new Set(files.map(getTsConfigForTypeScriptFile)) +for (const tsconfigPath of tsconfigPaths) { + const cwd = dirname(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 = `tsconfig.${randomChars()}.json` + const tmpTsconfig = { + ...tsconfig, + compilerOptions: { + ...tsconfig.compilerOptions, + skipLibCheck: true, + noEmit: true, + declaration: true + }, + files: files.map(file => file.replace(cwd + '/', '')), + include: [], + } + fs.writeFileSync(join(cwd, tmpTsconfigPath), JSON.stringify(tmpTsconfig, null, 2)) + + // Attach cleanup handlers + let didCleanup = false + for (const eventName of ['exit', 'SIGHUP', 'SIGINT', 'SIGTERM']) { + process.on(eventName, exitCode => { + if (didCleanup) return + didCleanup = true + + fs.unlinkSync(join(cwd, tmpTsconfigPath)) + + if (eventName !== 'exit') { + process.exit(exitCode) + } + }) + } + + // Type-check our files + const { status } = spawnSync( + // See: https://github.com/gustavopch/tsc-files/issues/44#issuecomment-1250783206 + process.versions.pnp + ? 'tsc' + : resolveFromModule( 'typescript', `../.bin/tsc${process.platform === 'win32' ? '.cmd' : ''}`, ), - ['-p', tmpTsconfigPath, ...remainingArgsToForward], - { stdio: 'inherit' }, -) - -process.exit(status) + ['--build', join(cwd, tmpTsconfigPath), ...remainingArgsToForward], + { stdio: 'inherit' }, + ) + + if(status) { + process.exit(status) + } +} \ No newline at end of file diff --git a/package.json b/package.json index bff1420..e21d630 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,6 @@ "typescript": ">=3" }, "devDependencies": { - "typescript": "4.1.3" + "typescript": "^5.3.3" } } diff --git a/yarn.lock b/yarn.lock index 0db10e7..cd07bce 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,7 +2,7 @@ # yarn lockfile v1 -typescript@4.1.3: - version "4.1.3" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7" - integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg== +typescript@^5.3.3: + version "5.3.3" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.3.3.tgz#b3ce6ba258e72e6305ba66f5c9b452aaee3ffe37" + integrity sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==