1
1
import chalk from 'chalk' ;
2
2
import path from 'path' ;
3
3
import child_process from 'child_process' ;
4
+ import fs from 'fs-extra' ;
4
5
import del from 'del' ;
5
6
import { Input } from '../types' ;
6
7
@@ -11,14 +12,40 @@ export default async function build({ root, output, report }: Input) {
11
12
12
13
report . info ( `Generating type definitions with ${ chalk . blue ( 'tsc' ) } ` ) ;
13
14
14
- child_process . execFileSync ( path . join ( root , 'node_modules' , '.bin' , 'tsc' ) , [
15
- '--declaration' ,
16
- '--emitDeclarationOnly' ,
17
- '--outDir' ,
18
- output ,
19
- ] ) ;
15
+ const tsc = path . join ( root , 'node_modules' , '.bin' , 'tsc' ) ;
20
16
21
- report . success (
22
- `Wrote definition files to ${ chalk . blue ( path . relative ( root , output ) ) } `
23
- ) ;
17
+ try {
18
+ if ( await fs . pathExists ( tsc ) ) {
19
+ child_process . execFileSync ( tsc , [
20
+ '--declaration' ,
21
+ '--emitDeclarationOnly' ,
22
+ '--outDir' ,
23
+ output ,
24
+ ] ) ;
25
+
26
+ report . success (
27
+ `Wrote definition files to ${ chalk . blue ( path . relative ( root , output ) ) } `
28
+ ) ;
29
+ } else {
30
+ throw new Error (
31
+ `The ${ chalk . blue (
32
+ 'tsc'
33
+ ) } binary doesn't seem to be installed under ${ chalk . blue (
34
+ 'node_modules'
35
+ ) } . Make sure you have added ${ chalk . blue (
36
+ 'typescript'
37
+ ) } to your ${ chalk . blue ( 'devDependencies' ) } .`
38
+ ) ;
39
+ }
40
+ } catch ( e ) {
41
+ if ( e . stdout ) {
42
+ report . error (
43
+ `Errors found when building definition files.\n${ e . stdout . toString ( ) } `
44
+ ) ;
45
+ } else {
46
+ report . error ( e . message ) ;
47
+ }
48
+
49
+ throw new Error ( 'Failed to build definition files.' ) ;
50
+ }
24
51
}
0 commit comments