Skip to content

Commit

Permalink
fix(build): type gen target
Browse files Browse the repository at this point in the history
  • Loading branch information
Ni55aN committed Jul 29, 2024
1 parent 86b87e2 commit 39ebe77
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
42 changes: 42 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"case": "1.6.3",
"chalk": "4.1.2",
"commander": "9.4.1",
"comment-json": "^4.2.4",
"eslint": "8.24.0",
"eslint-plugin-simple-import-sort": "^8.0.0",
"execa": "5.1.1",
Expand Down
6 changes: 5 additions & 1 deletion src/build/gen-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import execa from 'execa'
import { join, relative } from 'path'

import { SOURCE_FOLDER } from '../consts'
import { readTSConfig } from './utils'

export const typesDirectoryName = '_types'

Expand All @@ -15,13 +16,16 @@ export function getDTSPath(srcScript: string, distPath: string, packageDirectory
}

export async function generateTypes(outputDirectories: string[]) {
const config = await readTSConfig(process.cwd())
const target = config?.compilerOptions?.target || 'es5'

for (let i = 0; i < outputDirectories.length; i++) {
const outputDirectory = outputDirectories[i]

await execa('tsc', [
'-d',
'--pretty',
'--target', 'es5',
'--target', target,
'--outDir', join(outputDirectory, typesDirectoryName),
'--skipLibCheck',
'--declarationMap',
Expand Down
23 changes: 23 additions & 0 deletions src/build/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { parse } from 'comment-json'
import fs from 'fs'
import { join } from 'path'
import { CompilerOptions, ScriptTarget } from 'typescript'

let verbose = false

export function setVerbose(enabled: boolean) {
Expand All @@ -14,3 +19,21 @@ export async function safeExec<T>(func: () => Promise<T>, failMessage: string, e
return error
}
}

interface TSConfig {
compilerOptions?: Omit<CompilerOptions, 'target'> & {
target?: keyof typeof ScriptTarget
}
}

export async function readTSConfig(cwd: string): Promise<null | TSConfig> {
const configPath = join(cwd, 'tsconfig.json')
const exists = await fs.promises.access(configPath).then(() => true).catch(() => false)

if (!exists) return null

const file = await fs.promises.readFile(configPath, 'utf-8')
const config = parse(file) as TSConfig

return config
}

0 comments on commit 39ebe77

Please sign in to comment.