Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
110 changes: 65 additions & 45 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"typescript": ">=3"
},
"devDependencies": {
"typescript": "4.1.3"
"typescript": "^5.3.3"
}
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==