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: src types #25

Merged
merged 1 commit into from
Aug 30, 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
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
Loading