@@ -79,30 +79,72 @@ export default async function build({
79
79
) ;
80
80
}
81
81
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
+ }
86
126
87
127
if ( ! ( await fs . pathExists ( tsc ) ) ) {
88
128
try {
89
129
tsc = await which ( 'tsc' ) ;
90
130
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
+ }
100
142
} catch ( e ) {
101
143
// Ignore
102
144
}
103
145
}
104
146
105
- if ( ! ( await fs . pathExists ( tsc ) ) ) {
147
+ if ( tsc == null || ! ( await fs . pathExists ( tsc ) ) ) {
106
148
throw new Error (
107
149
`The ${ kleur . blue (
108
150
'tsc'
@@ -139,7 +181,10 @@ export default async function build({
139
181
'--outDir' ,
140
182
output ,
141
183
] ,
142
- { stdio : 'inherit' }
184
+ {
185
+ stdio : 'inherit' ,
186
+ cwd : root ,
187
+ }
143
188
) ;
144
189
145
190
if ( result . status === 0 ) {
0 commit comments