1
1
import execa from 'execa'
2
+ import fs from 'fs'
2
3
import { join , relative } from 'path'
3
4
4
5
import { SOURCE_FOLDER } from '../consts'
5
- import { readTSConfig } from './utils'
6
+ import { ExecError , readTSConfig } from './utils'
6
7
7
8
export const typesDirectoryName = '_types'
8
9
@@ -18,21 +19,44 @@ export function getDTSPath(srcScript: string, distPath: string, packageDirectory
18
19
export async function generateTypes ( outputDirectories : string [ ] ) {
19
20
const config = await readTSConfig ( process . cwd ( ) )
20
21
const target = config ?. compilerOptions ?. target ?? 'es5'
22
+ const includeFolders = config ?. include
21
23
22
24
for ( let i = 0 ; i < outputDirectories . length ; i ++ ) {
23
25
const outputDirectory = outputDirectories [ i ]
26
+ const typesDir = join ( outputDirectory , typesDirectoryName )
24
27
25
28
await execa ( 'tsc' , [
26
29
'-d' ,
27
30
'--pretty' ,
28
31
'--target' , target ,
29
- '--outDir' , join ( outputDirectory , typesDirectoryName ) ,
32
+ '--outDir' , typesDir ,
30
33
'--skipLibCheck' ,
31
34
'--declarationMap' ,
32
35
'--downlevelIteration' ,
33
36
'--emitDeclarationOnly'
34
37
] , { stdio : i === 0
35
38
? 'inherit'
36
39
: 'ignore' } )
40
+
41
+ if ( includeFolders && includeFolders . length > 1 ) {
42
+ await normalizeTypes ( outputDirectory , typesDir )
43
+ }
37
44
}
38
45
}
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
+
0 commit comments