From a11095667dcacfdb682c3bbd9c7b6aafc9535af0 Mon Sep 17 00:00:00 2001 From: Tobias Kuppens Groot Date: Thu, 4 Apr 2024 13:19:05 +0200 Subject: [PATCH 1/2] feat: refactor reading tsconfig file - remove eval function - add function to remove comments from json Signed-off-by: Tobias Kuppens Groot --- cli.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/cli.js b/cli.js index 7544d0d..83fe97f 100755 --- a/cli.js +++ b/cli.js @@ -27,6 +27,15 @@ const argsProjectValue = argsProjectIndex !== -1 ? args[argsProjectIndex + 1] : undefined const files = args.filter(file => /\.(ts|tsx)$/.test(file)) +const removeCommentsFromJson = fileContent => { + return fileContent.replaceAll(/[(\/\*)(\/\/)](.*)/g, '') +} + +// Load existing config +const tsconfigPath = argsProjectValue || resolveFromRoot('tsconfig.json') +const fileContent = fs.readFileSync(tsconfigPath, 'utf-8') +const tsconfig = JSON.parse(removeCommentsFromJson(fileContent)) + if (files.length === 0) { process.exit(0) } @@ -37,13 +46,6 @@ 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 = { From b4ed2537a5598750b88396a340993f5e04a1462a Mon Sep 17 00:00:00 2001 From: Tobias Kuppens Groot Date: Thu, 4 Apr 2024 13:21:14 +0200 Subject: [PATCH 2/2] feat: add handling of checkJs - add file ext for js,cjs,mjs files which would be used if checkJs flag has been set by tsconfig Signed-off-by: Tobias Kuppens Groot --- cli.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cli.js b/cli.js index 83fe97f..7f23bfd 100755 --- a/cli.js +++ b/cli.js @@ -26,7 +26,6 @@ const argsProjectIndex = args.findIndex(arg => const argsProjectValue = argsProjectIndex !== -1 ? args[argsProjectIndex + 1] : undefined -const files = args.filter(file => /\.(ts|tsx)$/.test(file)) const removeCommentsFromJson = fileContent => { return fileContent.replaceAll(/[(\/\*)(\/\/)](.*)/g, '') } @@ -35,7 +34,16 @@ const removeCommentsFromJson = fileContent => { const tsconfigPath = argsProjectValue || resolveFromRoot('tsconfig.json') const fileContent = fs.readFileSync(tsconfigPath, 'utf-8') const tsconfig = JSON.parse(removeCommentsFromJson(fileContent)) +const { + compilerOptions: { checkJs }, +} = tsconfig +const tsOnlyRegex = /\.(ts|tsx)$/ +const checkJsRegex = /\.(js|cjs|mjs|ts|tsx)$/ + +const files = args.filter(file => + checkJs ? checkJsRegex.test(file) : tsOnlyRegex.test(file), +) if (files.length === 0) { process.exit(0) }