11import chalk from 'chalk' ;
22import path from 'path' ;
33import child_process from 'child_process' ;
4+ import fs from 'fs-extra' ;
45import del from 'del' ;
56import { Input } from '../types' ;
67
@@ -11,14 +12,40 @@ export default async function build({ root, output, report }: Input) {
1112
1213 report . info ( `Generating type definitions with ${ chalk . blue ( 'tsc' ) } ` ) ;
1314
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' ) ;
2016
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+ }
2451}
0 commit comments