Skip to content

Commit b0005d6

Browse files
committed
fix: src types
1 parent f1b7187 commit b0005d6

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/build/gen-types.ts

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import execa from 'execa'
2+
import fs from 'fs'
23
import { join, relative } from 'path'
34

45
import { SOURCE_FOLDER } from '../consts'
5-
import { readTSConfig } from './utils'
6+
import { ExecError, readTSConfig } from './utils'
67

78
export const typesDirectoryName = '_types'
89

@@ -18,21 +19,44 @@ export function getDTSPath(srcScript: string, distPath: string, packageDirectory
1819
export async function generateTypes(outputDirectories: string[]) {
1920
const config = await readTSConfig(process.cwd())
2021
const target = config?.compilerOptions?.target ?? 'es5'
22+
const includeFolders = config?.include
2123

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

2528
await execa('tsc', [
2629
'-d',
2730
'--pretty',
2831
'--target', target,
29-
'--outDir', join(outputDirectory, typesDirectoryName),
32+
'--outDir', typesDir,
3033
'--skipLibCheck',
3134
'--declarationMap',
3235
'--downlevelIteration',
3336
'--emitDeclarationOnly'
3437
], { stdio: i === 0
3538
? 'inherit'
3639
: 'ignore' })
40+
41+
if (includeFolders && includeFolders.length > 1) {
42+
await normalizeTypes(outputDirectory, typesDir)
43+
}
3744
}
3845
}
46+
47+
/**
48+
* move the src folder to the root of the types directory
49+
*/
50+
async function normalizeTypes(outputDirectory: string, typesDir: string) {
51+
const existsSrcFolder = await fs.promises.access(join(typesDir, SOURCE_FOLDER))
52+
.then(() => true)
53+
.catch(() => false)
54+
const tmp = join(outputDirectory, '_tmp')
55+
56+
if (!existsSrcFolder) throw new ExecError('src folder not found when multiple include are specified')
57+
58+
await fs.promises.rename(typesDir, tmp)
59+
await fs.promises.rename(join(tmp, SOURCE_FOLDER), typesDir)
60+
await fs.promises.rm(tmp, { recursive: true })
61+
}
62+

src/build/utils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ export function setVerbose(enabled: boolean) {
99
verbose = enabled
1010
}
1111

12+
export class ExecError extends Error {}
13+
1214
export async function safeExec<T>(func: () => Promise<T>, failMessage: string, exit?: number): Promise<unknown> {
1315
try {
1416
await func()
1517
} catch (error) {
1618
console.error(failMessage)
1719
if (verbose) console.error(error)
20+
else if (error instanceof ExecError) console.error(error.message)
1821
if (Number.isInteger(exit)) process.exit(exit)
1922
return error
2023
}
@@ -24,6 +27,7 @@ interface TSConfig {
2427
compilerOptions?: Omit<CompilerOptions, 'target'> & {
2528
target?: keyof typeof ScriptTarget
2629
}
30+
include?: string[]
2731
}
2832

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

0 commit comments

Comments
 (0)