Skip to content

Commit

Permalink
using typescript itselft to write tsconfig for more reliability
Browse files Browse the repository at this point in the history
  • Loading branch information
NazmusSayad committed Aug 23, 2024
1 parent ef64590 commit dfcdee6
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 87 deletions.
19 changes: 9 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
5 changes: 2 additions & 3 deletions src/__lab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
37 changes: 18 additions & 19 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -57,6 +57,7 @@ app
})
.on(([nameArg = '.'], flags) => {
const root = path.resolve(nameArg)

init(root, {
writeSample: flags.demo,
writePackageJSON: flags.pkg,
Expand Down Expand Up @@ -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
),
})
})

Expand All @@ -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
),
})
})

Expand Down
49 changes: 19 additions & 30 deletions src/program/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down Expand Up @@ -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
)
}

Expand Down
93 changes: 93 additions & 0 deletions src/scripts/tsconfig.ts
Original file line number Diff line number Diff line change
@@ -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<string>(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')
}
20 changes: 0 additions & 20 deletions src/scripts/tsconfigJSON.ts

This file was deleted.

0 comments on commit dfcdee6

Please sign in to comment.