Skip to content

Commit

Permalink
fix: src types
Browse files Browse the repository at this point in the history
  • Loading branch information
Ni55aN committed Aug 30, 2024
1 parent f1b7187 commit b0005d6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/build/gen-types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import execa from 'execa'
import fs from 'fs'
import { join, relative } from 'path'

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

export const typesDirectoryName = '_types'

Expand All @@ -18,21 +19,44 @@ 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'
const includeFolders = config?.include

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

await execa('tsc', [
'-d',
'--pretty',
'--target', target,
'--outDir', join(outputDirectory, typesDirectoryName),
'--outDir', typesDir,
'--skipLibCheck',
'--declarationMap',
'--downlevelIteration',
'--emitDeclarationOnly'
], { stdio: i === 0
? 'inherit'
: 'ignore' })

if (includeFolders && includeFolders.length > 1) {
await normalizeTypes(outputDirectory, typesDir)
}
}
}

/**
* move the src folder to the root of the types directory
*/
async function normalizeTypes(outputDirectory: string, typesDir: string) {
const existsSrcFolder = await fs.promises.access(join(typesDir, SOURCE_FOLDER))
.then(() => true)
.catch(() => false)
const tmp = join(outputDirectory, '_tmp')

if (!existsSrcFolder) throw new ExecError('src folder not found when multiple include are specified')

await fs.promises.rename(typesDir, tmp)
await fs.promises.rename(join(tmp, SOURCE_FOLDER), typesDir)
await fs.promises.rm(tmp, { recursive: true })
}

4 changes: 4 additions & 0 deletions src/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ export function setVerbose(enabled: boolean) {
verbose = enabled
}

export class ExecError extends Error {}

export async function safeExec<T>(func: () => Promise<T>, failMessage: string, exit?: number): Promise<unknown> {
try {
await func()
} catch (error) {
console.error(failMessage)
if (verbose) console.error(error)
else if (error instanceof ExecError) console.error(error.message)
if (Number.isInteger(exit)) process.exit(exit)
return error
}
Expand All @@ -24,6 +27,7 @@ interface TSConfig {
compilerOptions?: Omit<CompilerOptions, 'target'> & {
target?: keyof typeof ScriptTarget
}
include?: string[]
}

export async function readTSConfig(cwd: string): Promise<null | TSConfig> {
Expand Down

0 comments on commit b0005d6

Please sign in to comment.