From dfcdee669154e6c09b782c535c5bbb338041e334 Mon Sep 17 00:00:00 2001 From: Nazmus Sayad <87106526+NazmusSayad@users.noreply.github.com> Date: Sat, 24 Aug 2024 02:35:33 +0600 Subject: [PATCH] using typescript itselft to write tsconfig for more reliability --- package-lock.json | 19 ++++---- package.json | 10 ++-- src/__lab.ts | 5 +- src/main.ts | 37 +++++++-------- src/program/init.ts | 49 ++++++++----------- src/scripts/tsconfig.ts | 93 +++++++++++++++++++++++++++++++++++++ src/scripts/tsconfigJSON.ts | 20 -------- 7 files changed, 146 insertions(+), 87 deletions(-) create mode 100644 src/scripts/tsconfig.ts delete mode 100644 src/scripts/tsconfigJSON.ts diff --git a/package-lock.json b/package-lock.json index 4e94f48..d17d91c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,27 +1,27 @@ { "name": "npmize", - "version": "1.0.9", + "version": "1.0.10", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "npmize", - "version": "1.0.9", + "version": "1.0.10", "license": "ISC", "dependencies": { "@babel/parser": "^7.20.7", "ansi-colors": "^4.1.3", "lskit": "^1.0.0", - "noarg": "^3.0.17", - "shelljs": "^0.8.5" + "noarg": "^3.0.18", + "shelljs": "^0.8.5", + "typescript": "^5.3.3" }, "bin": { "npmize": "dist/index.js" }, "devDependencies": { "@types/node": "^18.11.9", - "@types/shelljs": "^0.8.15", - "typescript": "^5.3.3" + "@types/shelljs": "^0.8.15" } }, "node_modules/@babel/helper-string-parser": { @@ -644,9 +644,9 @@ } }, "node_modules/noarg": { - "version": "3.0.17", - "resolved": "https://registry.npmjs.org/noarg/-/noarg-3.0.17.tgz", - "integrity": "sha512-u5ZHOac6odoaabjHv75Lq1swF8QKUBp5z8lfig33dAMKDngcUy6CxQ1cPcrk1orPHkZO8cOp3FeEvfRSCjPVlA==", + "version": "3.0.18", + "resolved": "https://registry.npmjs.org/noarg/-/noarg-3.0.18.tgz", + "integrity": "sha512-m0/oNSuueBcjlia3PAvY/1nh1+4XajG4WXVgViaNBZBNDNd6fd7fXSsJnbut2fCViQFaFIb2iXUAesMJBD0T+Q==", "dependencies": { "@inquirer/prompts": "^5.3.8", "ansi-colors": "^4.1.3", @@ -825,7 +825,6 @@ "version": "5.5.4", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.4.tgz", "integrity": "sha512-Mtq29sKDAEYP7aljRgtPOpTvOfbwRWlS6dPRzwjdE+C0R4brX/GUyhHSecbHMFLNBLcJIPt9nl9yG5TZ1weH+Q==", - "dev": true, "license": "Apache-2.0", "bin": { "tsc": "bin/tsc", diff --git a/package.json b/package.json index 7422baa..2865b6a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "npmize", "description": "Let's create an npm package without worrying about anything.", - "version": "1.0.10", + "version": "1.0.11", "bin": "./dist/index.js", "scripts": { "dev": "tsc -w", @@ -13,13 +13,13 @@ "@babel/parser": "^7.20.7", "ansi-colors": "^4.1.3", "lskit": "^1.0.0", - "noarg": "^3.0.17", - "shelljs": "^0.8.5" + "noarg": "^3.0.18", + "shelljs": "^0.8.5", + "typescript": "^5.3.3" }, "devDependencies": { "@types/node": "^18.11.9", - "@types/shelljs": "^0.8.15", - "typescript": "^5.3.3" + "@types/shelljs": "^0.8.15" }, "keywords": [ "npm", diff --git a/src/__lab.ts b/src/__lab.ts index 04bd110..768ad9a 100644 --- a/src/__lab.ts +++ b/src/__lab.ts @@ -2,7 +2,6 @@ console.clear() import app from './main' -app.start(['dev', '-h', '--noEmit']) -// app.start(['init', '../npmize-test']) -// app.start(['dev', '../npmize-test', '--node']) +app.start(['init', '../npmize-test']) +// app.start(['dev', '../npmize-test']) // app.start(['build', '../npmize-test']) diff --git a/src/main.ts b/src/main.ts index 2365470..2c12cc5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,9 +4,9 @@ import dev from './program/dev' import NoArg from 'noarg' import init from './program/init' import build from './program/build' -import tsconfigJSON from './scripts/tsconfigJSON' import { getVersion } from './utils' import ansiColors from 'ansi-colors' +import { readTSConfig } from './scripts/tsconfig' const app = NoArg.create(config.name, { description: config.description, @@ -57,6 +57,7 @@ app }) .on(([nameArg = '.'], flags) => { const root = path.resolve(nameArg) + init(root, { writeSample: flags.demo, writePackageJSON: flags.pkg, @@ -111,17 +112,16 @@ app ...devAndBuild, }) .on(([rootArg = '.', railingArgs], options) => { - const rootPath = path.resolve(rootArg as string) + const rootPath = path.resolve(rootArg) + const tsConfig = readTSConfig(rootPath) + dev(rootPath, { ...options, - tsc: railingArgs as string[], - outDir: options.outDir - ? path.join(rootPath, options.outDir as string) - : path.join( - rootPath, - tsconfigJSON.read(rootPath)?.compilerOptions?.outDir ?? - config.defaultOutDir - ), + tsc: railingArgs, + outDir: path.join( + rootPath, + options.outDir ?? tsConfig?.outDir ?? config.defaultOutDir + ), }) }) @@ -131,17 +131,16 @@ app ...devAndBuild, }) .on(([rootArg = '.', railingArgs], options) => { - const rootPath = path.resolve(rootArg as string) + const rootPath = path.resolve(rootArg) + const tsConfig = readTSConfig(rootPath) + build(rootPath, { ...options, - tsc: railingArgs as string[], - outDir: options.outDir - ? path.join(rootPath, options.outDir) - : path.join( - rootPath, - tsconfigJSON.read(rootPath)?.compilerOptions?.outDir ?? - config.defaultOutDir - ), + tsc: railingArgs, + outDir: path.join( + rootPath, + options.outDir ?? tsConfig?.outDir ?? config.defaultOutDir + ), }) }) diff --git a/src/program/init.ts b/src/program/init.ts index 263c2c8..12fc4e4 100644 --- a/src/program/init.ts +++ b/src/program/init.ts @@ -5,8 +5,8 @@ import config from '../config' import ansiColors from 'ansi-colors' import packageJSON from '../scripts/packageJSON' import { writeFileSync } from '../utils' -import tsconfigJSON from '../scripts/tsconfigJSON' import ghWorkflows from '../scripts/ghWorkflows' +import { updateTSConfig } from '../scripts/tsconfig' export default function (basePath: string, options: InitOptions) { console.log(`\x1b[1m\x1b[35m▓▒░ NPMIZE ░▒▓\x1b[0m\n`) @@ -46,37 +46,26 @@ export default function (basePath: string, options: InitOptions) { } if (options.writeTSConfig) { - const oldTSConfig = tsconfigJSON.read(basePath) - const newTSConfig = { - compilerOptions: { - target: 'es6', - skipLibCheck: true, - - declaration: true, - inlineSourceMap: false, - - strict: true, - pretty: true, - removeComments: true, - }, - include: ['./src'], - } + updateTSConfig( + path.join(basePath, './tsconfig.json'), + { + compilerOptions: { + baseUrl: '.', - const mixedTsconfig = { - ...oldTSConfig, - ...newTSConfig, - compilerOptions: { - ...oldTSConfig.compilerOptions, - ...newTSConfig.compilerOptions, - }, - include: Array.from( - new Set([...(oldTSConfig.include ?? []), ...newTSConfig.include]) - ), - } + target: 'ES6' as any, + skipLibCheck: true, - writeFileSync( - path.join(basePath, './tsconfig.json'), - JSON.stringify(mixedTsconfig, null, 2) + declaration: true, + inlineSourceMap: false, + + strict: true, + pretty: true, + removeComments: true, + }, + + include: ['./src'], + }, + true ) } diff --git a/src/scripts/tsconfig.ts b/src/scripts/tsconfig.ts new file mode 100644 index 0000000..67a09ab --- /dev/null +++ b/src/scripts/tsconfig.ts @@ -0,0 +1,93 @@ +import * as fs from 'fs' +import * as path from 'path' +import ts from 'typescript' + +export function findTSConfigPath(targetPath: string) { + return ts.findConfigFile(targetPath, ts.sys.fileExists, 'tsconfig.json') +} + +export function readTSConfig(targetPath: string) { + const configFilePath = findTSConfigPath(targetPath) + if (!configFilePath) return null + + const configJsonString = ts.readConfigFile(configFilePath, ts.sys.readFile) + if (configJsonString.error) { + throw new Error( + ts.formatDiagnostic(configJsonString.error, ts.createCompilerHost({})) + ) + } + + const configObj = ts.parseJsonConfigFileContent( + configJsonString.config, + ts.sys, + path.dirname(configFilePath) + ) + + return { + ...configObj.options, + configDirPath: path.dirname(configFilePath), + configFilePath, + } +} + +export function updateTSConfig( + configFilePath: string, + newConfig: { + compilerOptions?: ts.CompilerOptions + include?: string[] + } = {}, + + force?: true +) { + if (force && !fs.existsSync(configFilePath)) { + fs.writeFileSync(configFilePath, '{}', 'utf-8') + } + + const configFileContent = fs.readFileSync(configFilePath, 'utf-8') + const configObj = ts.parseConfigFileTextToJson( + configFilePath, + configFileContent + ) + + if (configObj.error) { + throw new Error( + ts.formatDiagnostic(configObj.error, ts.createCompilerHost({})) + ) + } + + const updatedConfig = { + ...configObj.config, + + compilerOptions: { + ...configObj.config.compilerOptions, + ...newConfig.compilerOptions, + }, + + include: [ + ...(configObj.config.include ?? []), + ...(newConfig.include ?? []), + ], + } + + if (updatedConfig.include?.length) { + const basePath = path.resolve( + path.dirname(configFilePath), + updatedConfig.compilerOptions.baseUrl ?? '' + ) + + const fullIncludePaths = updatedConfig.include.map((includePath: string) => + path.resolve(basePath, includePath) + ) + + updatedConfig.include = [...new Set(fullIncludePaths)] + .map((includePath) => path.relative(basePath, includePath)) + .filter(Boolean) + } + + if (updatedConfig.include?.length === 0) { + delete updatedConfig.include + } + + const updatedConfigString = JSON.stringify(updatedConfig, null, 2) + fs.writeFileSync(configFilePath, updatedConfigString, 'utf-8') +} diff --git a/src/scripts/tsconfigJSON.ts b/src/scripts/tsconfigJSON.ts deleted file mode 100644 index f57d6ca..0000000 --- a/src/scripts/tsconfigJSON.ts +++ /dev/null @@ -1,20 +0,0 @@ -import * as fs from 'fs' -import path from 'path' -import { writeFileSync } from '../utils' - -function read(basePath: string): Record { - try { - const filePath = path.join(basePath, './tsconfig.json') - return JSON.parse(fs.readFileSync(filePath, 'utf8')) - } catch (err: any) { - if (err.code === 'ENOENT') return {} - throw err - } -} - -function write(basePath: string, data: Record) { - const filePath = path.join(basePath, './tsconfig.json') - writeFileSync(filePath, JSON.stringify(data, null, 2)) -} - -export default { read, write }