Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(build) type gen target #18

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading