@jonasgeiler/tsc-files
Advanced tools
Comparing version 2.2.4 to 2.3.0
99
cli.js
@@ -8,2 +8,3 @@ #!/usr/bin/env node | ||
import process from 'node:process'; | ||
import crypto from 'node:crypto'; | ||
@@ -17,8 +18,2 @@ // This code is forked from the `tsc-files` package and modified, so it's an | ||
/** | ||
* Generate a random string of characters. | ||
* @returns {string} | ||
*/ | ||
const randomChars = () => Math.random().toString(36).slice(2); | ||
/** | ||
* Resolve a path from a module. | ||
@@ -30,3 +25,5 @@ * @param {string} moduleName | ||
const resolveFromModule = (moduleName, ...paths) => { | ||
const modulePath = path.dirname(require.resolve(`${moduleName}/package.json`)); | ||
const modulePath = path.dirname( | ||
require.resolve(`${moduleName}/package.json`), | ||
); | ||
return path.join(modulePath, ...paths); | ||
@@ -43,15 +40,14 @@ }; | ||
// Get arguments without the node executable and this script | ||
const args = process.argv.slice(2); | ||
const argv = process.argv.slice(2); | ||
// Get the project argument and its value | ||
const argsProjectIndex = args.findIndex(arg => | ||
[ '-p', '--project' ].includes(arg), | ||
const argvProjectIndex = argv.findIndex((argument) => | ||
['-p', '--project'].includes(argument), | ||
); | ||
const argsProjectValue = argsProjectIndex === -1 | ||
? undefined | ||
: args[argsProjectIndex + 1]; | ||
const argvProjectValue = | ||
argvProjectIndex === -1 ? undefined : argv[argvProjectIndex + 1]; | ||
// Get the files to type-check and check if we should show the help message | ||
const files = args.filter(file => /\.(c|m)?(j|t)sx?$/.test(file)); | ||
if (args.includes('-h') || args.includes('--help') || files.length === 0) { | ||
const files = argv.filter((file) => /\.(c|m)?(j|t)sx?$/.test(file)); | ||
if (argv.includes('-h') || argv.includes('--help') || files.length === 0) { | ||
console.log(` | ||
@@ -75,9 +71,11 @@ Usage: tsc-files [files...] [options] | ||
// Get the arguments to forward to tsc | ||
const remainingArgsToForward = args.filter(arg => !files.includes(arg)); | ||
if (argsProjectIndex !== -1) { | ||
remainingArgsToForward.splice(argsProjectIndex, 2); | ||
const remainingArgvToForward = argv.filter( | ||
(argument) => !files.includes(argument), | ||
); | ||
if (argvProjectIndex !== -1) { | ||
remainingArgvToForward.splice(argvProjectIndex, 2); | ||
} | ||
// Load existing config | ||
const tsconfigPath = argsProjectValue || resolveFromRoot('tsconfig.json'); | ||
const tsconfigPath = argvProjectValue || resolveFromRoot('tsconfig.json'); | ||
const tsconfigContent = fs.readFileSync(tsconfigPath).toString(); | ||
@@ -88,33 +86,37 @@ | ||
// Get a temporary tsconfig file path | ||
let temporaryTsconfigPath = resolveFromRoot( | ||
`tsconfig.tsc-files-${randomChars()}.json`, | ||
// Create a tsconfig configuration with the files to type-check and hash it | ||
const temporaryTsconfig = { | ||
...tsconfig, | ||
compilerOptions: { | ||
...tsconfig.compilerOptions, | ||
skipLibCheck: true, | ||
}, | ||
files, | ||
include: [], | ||
}; | ||
let temporaryTsconfigHash = crypto | ||
.createHash('md5') | ||
.update(JSON.stringify(temporaryTsconfig)) | ||
.digest('hex'); | ||
// Add the tsBuildInfoFile to the temporary tsconfig configuration and re-hash | ||
temporaryTsconfig.compilerOptions.tsBuildInfoFile = resolveFromRoot( | ||
`./tsc-files-${temporaryTsconfigHash}.tsbuildinfo`, | ||
); | ||
while (fs.existsSync(temporaryTsconfigPath)) { | ||
temporaryTsconfigPath = resolveFromRoot( | ||
`tsconfig.tsc-files-${randomChars()}.json`, | ||
); | ||
} | ||
const temporaryTsconfigContent = JSON.stringify(temporaryTsconfig, null, 2); | ||
temporaryTsconfigHash = crypto | ||
.createHash('md5') | ||
.update(temporaryTsconfigContent) | ||
.digest('hex'); | ||
// Create a new temporary config file with the files to type-check | ||
fs.writeFileSync( | ||
temporaryTsconfigPath, | ||
JSON.stringify({ | ||
...tsconfig, | ||
compilerOptions: { | ||
...tsconfig.compilerOptions, | ||
skipLibCheck: true, | ||
}, | ||
files, | ||
include: [], | ||
}, undefined, 2), | ||
{ | ||
flag: 'wx', // Fail if the file already exists | ||
}, | ||
// Create the temporary tsconfig file | ||
const temporaryTsconfigPath = resolveFromRoot( | ||
`./tsconfig.tsc-files-${temporaryTsconfigHash}.json`, | ||
); | ||
fs.writeFileSync(temporaryTsconfigPath, temporaryTsconfigContent); | ||
// Attach cleanup handlers to remove the temporary config file on exit | ||
let didCleanup = false; | ||
for (const eventName of [ 'exit', 'SIGHUP', 'SIGINT', 'SIGTERM' ]) { | ||
process.on(eventName, exitCode => { | ||
for (const eventName of ['exit', 'SIGHUP', 'SIGINT', 'SIGTERM']) { | ||
process.on(eventName, (exitCode) => { | ||
if (didCleanup) { | ||
@@ -126,2 +128,5 @@ return; | ||
fs.unlinkSync(temporaryTsconfigPath); | ||
if (fs.existsSync(temporaryTsconfig.compilerOptions.tsBuildInfoFile)) { | ||
fs.unlinkSync(temporaryTsconfig.compilerOptions.tsBuildInfoFile); | ||
} | ||
@@ -136,3 +141,3 @@ if (eventName !== 'exit') { | ||
let tsc = ''; | ||
if (process.versions.pnp) { | ||
if (process.versions['pnp']) { | ||
tsc = 'tsc'; | ||
@@ -161,6 +166,6 @@ } else { | ||
tsc, | ||
[ '-p', temporaryTsconfigPath, ...remainingArgsToForward ], | ||
['-p', temporaryTsconfigPath, ...remainingArgvToForward], | ||
{ | ||
stdio: 'inherit', | ||
env: { ...process.env }, | ||
env: {...process.env}, | ||
shell: process.platform === 'win32', | ||
@@ -167,0 +172,0 @@ }, |
{ | ||
"name": "@jonasgeiler/tsc-files", | ||
"version": "2.2.4", | ||
"version": "2.3.0", | ||
"description": "A tiny tool to run tsc on specific files without ignoring tsconfig.json", | ||
"homepage": "https://github.com/skayo/tsc-files#readme", | ||
"bugs": "https://github.com/skayo/tsc-files/issues", | ||
"homepage": "https://github.com/jonasgeiler/tsc-files#readme", | ||
"bugs": "https://github.com/jonasgeiler/tsc-files/issues", | ||
"license": "MIT", | ||
"author": "Jonas Geiler <npm@jonasgeiler.com> (https://skayo.dev)", | ||
"repository": "github:skayo/tsc-files", | ||
"author": "Jonas Geiler <npm@jonasgeiler.com> (https://jonasgeiler.com)", | ||
"repository": "github:jonasgeiler/tsc-files", | ||
"devDependencies": { | ||
"@commitlint/cli": "^17.8.0", | ||
"@commitlint/config-conventional": "^17.8.0", | ||
"@types/node": "^20.8.2", | ||
"husky": "^8.0.3", | ||
"lint-staged": "^15.0.1", | ||
"typescript": "^5.2.2", | ||
"xo": "^0.56.0" | ||
"@commitlint/cli": "19.3.0", | ||
"@commitlint/config-conventional": "19.2.2", | ||
"@types/node": "20.12.12", | ||
"husky": "9.0.11", | ||
"lint-staged": "15.2.5", | ||
"typescript": "5.4.5", | ||
"xo": "0.58.0" | ||
}, | ||
"engines": { | ||
"node": ">=18" | ||
}, | ||
"type": "module", | ||
@@ -27,16 +30,7 @@ "bin": "./cli.js", | ||
], | ||
"engines": { | ||
"node": "^18 || >=20" | ||
}, | ||
"xo": { | ||
"prettier": true, | ||
"rules": { | ||
"object-curly-spacing": [ | ||
"error", | ||
"always" | ||
], | ||
"array-bracket-spacing": [ | ||
"error", | ||
"always" | ||
], | ||
"no-new-func": "off" | ||
"no-new-func": "off", | ||
"dot-notation": "off" | ||
} | ||
@@ -67,5 +61,5 @@ }, | ||
"lint": "xo", | ||
"fix": "xo --fix", | ||
"format": "xo --fix", | ||
"check": "tsc" | ||
} | ||
} |
@@ -0,1 +1,4 @@ | ||
> [!NOTE] | ||
> This is a fork of [`tsc-files`](https://www.npmjs.com/package/tsc-files) which modernizes the whole codebase a bit and implements some unmerged pull requests of the [original repository](https://github.com/gustavopch/tsc-files). | ||
# tsc-files | ||
@@ -15,2 +18,6 @@ | ||
```sh | ||
pnpm add -D @jonasgeiler/tsc-files | ||
``` | ||
## Why | ||
@@ -17,0 +24,0 @@ |
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
8495
147
45