@@ -4,15 +4,18 @@ import path from 'path';
4
4
import yargs from 'yargs' ;
5
5
import { $ } from 'execa' ;
6
6
import * as babel from '@babel/core' ;
7
+ import { parse } from 'jsonc-parser' ;
7
8
8
9
const $$ = $ ( { stdio : 'inherit' } ) ;
9
10
10
11
async function emitDeclarations ( tsconfig : string , outDir : string ) {
12
+ // eslint-disable-next-line no-console
11
13
console . log ( `Building types for ${ path . resolve ( tsconfig ) } ` ) ;
12
14
await $$ `tsc -p ${ tsconfig } --outDir ${ outDir } --declaration --emitDeclarationOnly` ;
13
15
}
14
16
15
17
async function addImportExtensions ( folder : string ) {
18
+ // eslint-disable-next-line no-console
16
19
console . log ( `Adding import extensions` ) ;
17
20
const dtsFiles = await glob ( '**/*.d.ts' , { absolute : true , cwd : folder } ) ;
18
21
if ( dtsFiles . length === 0 ) {
@@ -42,6 +45,7 @@ async function copyDeclarations(sourceDirectory: string, destinationDirectory: s
42
45
const fullSourceDirectory = path . resolve ( sourceDirectory ) ;
43
46
const fullDestinationDirectory = path . resolve ( destinationDirectory ) ;
44
47
48
+ // eslint-disable-next-line no-console
45
49
console . log ( `Copying declarations from ${ fullSourceDirectory } to ${ fullDestinationDirectory } ` ) ;
46
50
47
51
await fs . cp ( fullSourceDirectory , fullDestinationDirectory , {
@@ -62,25 +66,30 @@ async function copyDeclarations(sourceDirectory: string, destinationDirectory: s
62
66
63
67
interface HandlerArgv {
64
68
skipTsc : boolean ;
69
+ copy : string [ ] ;
65
70
}
66
71
67
72
async function main ( argv : HandlerArgv ) {
68
73
const packageRoot = process . cwd ( ) ;
74
+ const tsconfigPath = path . join ( packageRoot , 'tsconfig.build.json' ) ;
75
+ const tsconfigExists = await fs . access ( tsconfigPath ) . then (
76
+ ( ) => true ,
77
+ ( ) => false ,
78
+ ) ;
79
+
80
+ const tsConfig = tsconfigExists
81
+ ? ( parse ( await fs . readFile ( tsconfigPath , 'utf-8' ) ) as { compilerOptions : { outDir : string } } )
82
+ : null ;
69
83
70
84
const srcPath = path . join ( packageRoot , 'src' ) ;
71
85
const buildFolder = path . join ( packageRoot , 'build' ) ;
72
- const esmFolder = path . join ( buildFolder , 'esm' ) ;
73
- const modernFolder = path . join ( buildFolder , 'modern' ) ;
86
+ const esmOrOutDir = tsConfig ?. compilerOptions . outDir
87
+ ? path . join ( packageRoot , tsConfig . compilerOptions . outDir )
88
+ : path . join ( buildFolder , 'esm' ) ;
74
89
75
- await copyDeclarations ( srcPath , esmFolder ) ;
90
+ await copyDeclarations ( srcPath , esmOrOutDir ) ;
76
91
77
92
if ( ! argv . skipTsc ) {
78
- const tsconfigPath = path . join ( packageRoot , 'tsconfig.build.json' ) ;
79
- const tsconfigExists = await fs . access ( tsconfigPath ) . then (
80
- ( ) => true ,
81
- ( ) => false ,
82
- ) ;
83
-
84
93
if ( ! tsconfigExists ) {
85
94
throw new Error (
86
95
'Unable to find a tsconfig to build this project. ' +
@@ -89,29 +98,36 @@ async function main(argv: HandlerArgv) {
89
98
) ;
90
99
}
91
100
92
- await emitDeclarations ( tsconfigPath , esmFolder ) ;
101
+ await emitDeclarations ( tsconfigPath , esmOrOutDir ) ;
93
102
}
94
103
95
- await addImportExtensions ( esmFolder ) ;
104
+ await addImportExtensions ( esmOrOutDir ) ;
96
105
97
- await copyDeclarations ( esmFolder , buildFolder ) ;
98
- await copyDeclarations ( esmFolder , modernFolder ) ;
106
+ await Promise . all (
107
+ argv . copy . map ( ( copy ) => copyDeclarations ( esmOrOutDir , path . join ( packageRoot , copy ) ) ) ,
108
+ ) ;
99
109
}
100
110
101
111
yargs ( process . argv . slice ( 2 ) )
102
- . command < HandlerArgv > ( {
103
- command : '$0' ,
104
- description :
105
- 'Builds a project with a fix for https://github.com/microsoft/TypeScript/issues/39117' ,
106
- builder : ( command ) => {
107
- return command . option ( 'skipTsc' , {
108
- type : 'boolean' ,
109
- default : false ,
110
- describe : 'Set to `true` if you want the legacy behavior of just copying .d.ts files.' ,
111
- } ) ;
112
+ . command < HandlerArgv > (
113
+ '$0' ,
114
+ 'Builds type definition files and copies them to the specified directories with a fix for https://github.com/microsoft/TypeScript/issues/39117' ,
115
+ ( command ) => {
116
+ return command
117
+ . option ( 'skipTsc' , {
118
+ type : 'boolean' ,
119
+ default : false ,
120
+ describe : 'Set to `true` if you want the legacy behavior of just copying .d.ts files.' ,
121
+ } )
122
+ . option ( 'copy' , {
123
+ alias : 'c' ,
124
+ type : 'array' ,
125
+ description : 'Directories where the type definition files should be copied' ,
126
+ default : [ 'build' , 'build/modern' ] ,
127
+ } ) ;
112
128
} ,
113
- handler : main ,
114
- } )
129
+ main ,
130
+ )
115
131
. help ( )
116
132
. strict ( true )
117
133
. version ( false )
0 commit comments