@@ -79,30 +79,72 @@ export default async function build({
7979 ) ;
8080 }
8181
82- let tsc = options ?. tsc
83- ? path . resolve ( root , options . tsc )
84- : path . resolve ( root , 'node_modules' , '.bin' , 'tsc' ) +
85- ( platform ( ) === 'win32' ? '.cmd' : '' ) ;
82+ let tsc ;
83+
84+ if ( options ?. tsc ) {
85+ tsc = path . resolve ( root , options . tsc ) ;
86+
87+ if ( ! ( await fs . pathExists ( tsc ) ) ) {
88+ throw new Error (
89+ `The ${ kleur . blue (
90+ 'tsc'
91+ ) } binary doesn't seem to be installed at ${ kleur . blue (
92+ tsc
93+ ) } . Please specify the correct path in options or remove it to use the workspace's version.`
94+ ) ;
95+ }
96+ } else {
97+ const execpath = process . env . npm_execpath ;
98+ const cli = execpath ?. split ( '/' ) . pop ( ) ?. includes ( 'yarn' ) ? 'yarn' : 'npm' ;
99+
100+ try {
101+ if ( cli === 'yarn' ) {
102+ const result = spawn . sync ( 'yarn' , [ 'bin' , 'tsc' ] , {
103+ stdio : 'pipe' ,
104+ encoding : 'utf-8' ,
105+ cwd : root ,
106+ } ) ;
107+
108+ tsc = result . stdout . trim ( ) ;
109+ } else {
110+ const result = spawn . sync ( 'npm' , [ 'bin' ] , {
111+ stdio : 'pipe' ,
112+ encoding : 'utf-8' ,
113+ cwd : root ,
114+ } ) ;
115+
116+ tsc = path . resolve ( result . stdout . trim ( ) , 'tsc' ) ;
117+ }
118+ } catch ( e ) {
119+ tsc = path . resolve ( root , 'node_modules' , '.bin' , 'tsc' ) ;
120+ }
121+
122+ if ( platform ( ) === 'win32' && ! tsc . endsWith ( '.cmd' ) ) {
123+ tsc += '.cmd' ;
124+ }
125+ }
86126
87127 if ( ! ( await fs . pathExists ( tsc ) ) ) {
88128 try {
89129 tsc = await which ( 'tsc' ) ;
90130
91- report . warn (
92- `Using a global version of ${ kleur . blue (
93- 'tsc'
94- ) } . Consider adding ${ kleur . blue ( 'typescript' ) } to your ${ kleur . blue (
95- 'devDependencies'
96- ) } or specifying the ${ kleur . blue (
97- 'tsc'
98- ) } option for the typescript target.`
99- ) ;
131+ if ( await fs . pathExists ( tsc ) ) {
132+ report . warn (
133+ `Failed to locate 'tsc' in the workspace. Falling back to the globally installed version. Consider adding ${ kleur . blue (
134+ 'typescript'
135+ ) } to your ${ kleur . blue (
136+ 'devDependencies'
137+ ) } or specifying the ${ kleur . blue (
138+ 'tsc'
139+ ) } option for the typescript target.`
140+ ) ;
141+ }
100142 } catch ( e ) {
101143 // Ignore
102144 }
103145 }
104146
105- if ( ! ( await fs . pathExists ( tsc ) ) ) {
147+ if ( tsc == null || ! ( await fs . pathExists ( tsc ) ) ) {
106148 throw new Error (
107149 `The ${ kleur . blue (
108150 'tsc'
@@ -139,7 +181,10 @@ export default async function build({
139181 '--outDir' ,
140182 output ,
141183 ] ,
142- { stdio : 'inherit' }
184+ {
185+ stdio : 'inherit' ,
186+ cwd : root ,
187+ }
143188 ) ;
144189
145190 if ( result . status === 0 ) {
0 commit comments