11#!/usr/bin/env node
2- import { spawnSync } from 'node:child_process' ;
2+ import { spawn } from 'node:child_process' ;
33import { mkdtempSync , readFileSync , rmSync , writeFileSync } from 'node:fs' ;
44import { createRequire } from 'node:module' ;
55import { availableParallelism , tmpdir } from 'node:os' ;
@@ -23,14 +23,36 @@ if (projects.length === 0) {
2323 process . exit ( 2 ) ;
2424}
2525
26+ // TypeScript follows `./`, `../`, and absolute paths from the config file, and
27+ // resolves every other `extends` as a module. A leading-dot check drops the
28+ // absolute config `typecheck:files` writes, plus package and bare specifiers,
29+ // so the wrapper below would paint 5.9 defaults over options the project set.
30+ function resolveExtends ( project , base ) {
31+ if ( typeof base !== 'string' || base . length === 0 ) return undefined ;
32+ if ( path . isAbsolute ( base ) || base . startsWith ( '.' ) ) {
33+ const resolved = path . resolve ( path . dirname ( project ) , base ) ;
34+ return resolved . endsWith ( '.json' ) ? resolved : `${ resolved } .json` ;
35+ }
36+ const require = createRequire ( project ) ;
37+ const specifiers = base . endsWith ( '.json' ) ? [ base ] : [ base , `${ base } .json` ] ;
38+ for ( const specifier of specifiers ) {
39+ try {
40+ const resolved = require . resolve ( specifier ) ;
41+ if ( resolved . endsWith ( '.json' ) ) return resolved ;
42+ } catch {
43+ // Try the next candidate. An unresolvable base is reported by tsc.
44+ }
45+ }
46+ return undefined ;
47+ }
48+
2649function declaredOptions ( project , seen = new Set ( ) ) {
2750 if ( seen . has ( project ) ) return { } ;
2851 seen . add ( project ) ;
2952 const config = jsonc . parse ( readFileSync ( project , 'utf8' ) ) ?? { } ;
30- const bases = [ config . extends ?? [ ] ] . flat ( ) . filter ( ( base ) => base . startsWith ( '.' ) ) ;
31- const inherited = bases . map ( ( base ) => {
32- const resolved = path . resolve ( path . dirname ( project ) , base ) ;
33- return declaredOptions ( resolved . endsWith ( '.json' ) ? resolved : `${ resolved } .json` , seen ) ;
53+ const inherited = [ config . extends ?? [ ] ] . flat ( ) . map ( ( base ) => {
54+ const resolved = resolveExtends ( project , base ) ;
55+ return resolved ? declaredOptions ( resolved , seen ) : { } ;
3456 } ) ;
3557 return Object . assign ( { } , ...inherited , config . compilerOptions ) ;
3658}
@@ -49,65 +71,99 @@ function compatibilityOptions(project) {
4971 } ;
5072}
5173
74+ const showConfig = passthrough . includes ( '--showConfig' ) ;
75+ if ( showConfig && new Set ( projects ) . size !== 1 ) {
76+ console . error ( 'octane-tsc --showConfig takes exactly one -p <tsconfig>' ) ;
77+ process . exit ( 2 ) ;
78+ }
79+
5280// The mapper is enabled from a generated sibling wrapper rather than the
5381// project's own tsconfig: contentMappers is an error without --runExternalCode,
5482// so editors and every other TypeScript would reject it. The wrapper must sit in
5583// the project's directory because the root config decides type-root lookup.
84+ // Those siblings are removed on every exit path. The checker is async so a
85+ // SIGINT/SIGTERM handler can run during the build; spawnSync would block the
86+ // loop and skip both the handler and this cleanup.
5687const buildInfoDir = mkdtempSync ( path . join ( tmpdir ( ) , 'octane-tsc-' ) ) ;
57- const wrappers = [ ...new Set ( projects ) ] . map ( ( project , index ) => {
58- const wrapper = path . join (
59- path . dirname ( project ) ,
60- `.octane-tsc-${ process . pid } .${ path . basename ( project ) } ` ,
61- ) ;
62- writeFileSync (
63- wrapper ,
64- JSON . stringify ( {
65- extends : `./${ path . basename ( project ) } ` ,
66- compilerOptions : {
67- ...compatibilityOptions ( project ) ,
68- tsBuildInfoFile : path . join ( buildInfoDir , `${ index } .tsbuildinfo` ) , // --build records build state even with --noEmit
69- } ,
70- contentMappers : [
71- {
72- package : '@tsrx/content-mapper' ,
73- extensions : [ '.tsrx' ] ,
74- options : { compiler : 'octane/compiler/volar' } , // auto-detection prefers @tsrx/react, which bindings install for parity tests
75- } ,
76- ] ,
77- } ) ,
78- ) ;
79- return wrapper ;
80- } ) ;
88+ const wrappers = [ ] ;
89+ let child ;
90+ let abortCode ;
8191
82- const showConfig = passthrough . includes ( '--showConfig' ) ;
83- if ( showConfig && wrappers . length !== 1 ) {
84- console . error ( 'octane-tsc --showConfig takes exactly one -p <tsconfig>' ) ;
85- process . exit ( 2 ) ;
92+ function cleanup ( ) {
93+ for ( const wrapper of wrappers ) rmSync ( wrapper , { force : true } ) ;
94+ rmSync ( buildInfoDir , { recursive : true , force : true } ) ;
8695}
8796
88- let result ;
97+ function abort ( signal , code ) {
98+ if ( abortCode !== undefined ) return ;
99+ abortCode = code ;
100+ if ( child && child . exitCode === null && child . signalCode === null ) {
101+ child . kill ( signal ) ;
102+ return ;
103+ }
104+ cleanup ( ) ;
105+ process . exit ( code ) ;
106+ }
107+
108+ process . on ( 'SIGINT' , ( ) => abort ( 'SIGINT' , 130 ) ) ;
109+ process . on ( 'SIGTERM' , ( ) => abort ( 'SIGTERM' , 143 ) ) ;
110+
89111try {
90- result = spawnSync (
91- process . execPath ,
92- showConfig
93- ? [ tsc , '--showConfig' , '-p' , wrappers [ 0 ] , '--runExternalCode' ]
94- : [
95- tsc ,
96- '--build' ,
97- ...wrappers ,
98- '--noEmit' ,
99- '--runExternalCode' ,
100- '--builders' ,
101- String ( availableParallelism ( ) ) ,
102- '--checkers' ,
103- '1' ,
104- ...passthrough ,
112+ for ( const [ index , project ] of [ ...new Set ( projects ) ] . entries ( ) ) {
113+ const wrapper = path . join (
114+ path . dirname ( project ) ,
115+ `.octane-tsc-${ process . pid } .${ path . basename ( project ) } ` ,
116+ ) ;
117+ writeFileSync (
118+ wrapper ,
119+ JSON . stringify ( {
120+ extends : `./${ path . basename ( project ) } ` ,
121+ compilerOptions : {
122+ ...compatibilityOptions ( project ) ,
123+ tsBuildInfoFile : path . join ( buildInfoDir , `${ index } .tsbuildinfo` ) , // --build records build state even with --noEmit
124+ } ,
125+ contentMappers : [
126+ {
127+ package : '@tsrx/content-mapper' ,
128+ extensions : [ '.tsrx' ] ,
129+ options : { compiler : 'octane/compiler/volar' } , // auto-detection prefers @tsrx/react, which bindings install for parity tests
130+ } ,
105131 ] ,
106- { stdio : 'inherit' } ,
107- ) ;
108- } finally {
109- for ( const wrapper of wrappers ) rmSync ( wrapper , { force : true } ) ;
110- rmSync ( buildInfoDir , { recursive : true , force : true } ) ;
132+ } ) ,
133+ ) ;
134+ wrappers . push ( wrapper ) ;
135+ }
136+
137+ const status = await new Promise ( ( resolve , reject ) => {
138+ child = spawn (
139+ process . execPath ,
140+ showConfig
141+ ? [ tsc , '--showConfig' , '-p' , wrappers [ 0 ] , '--runExternalCode' ]
142+ : [
143+ tsc ,
144+ '--build' ,
145+ ...wrappers ,
146+ '--noEmit' ,
147+ '--runExternalCode' ,
148+ '--builders' ,
149+ String ( availableParallelism ( ) ) ,
150+ '--checkers' ,
151+ '1' ,
152+ ...passthrough ,
153+ ] ,
154+ { stdio : 'inherit' } ,
155+ ) ;
156+ child . on ( 'error' , reject ) ;
157+ child . on ( 'exit' , ( code , signal ) => {
158+ if ( abortCode !== undefined ) resolve ( abortCode ) ;
159+ else if ( signal === 'SIGINT' ) resolve ( 130 ) ;
160+ else if ( signal === 'SIGTERM' ) resolve ( 143 ) ;
161+ else resolve ( code ?? 1 ) ;
162+ } ) ;
163+ } ) ;
164+ cleanup ( ) ;
165+ process . exit ( status ) ;
166+ } catch ( error ) {
167+ cleanup ( ) ;
168+ throw error ;
111169}
112- if ( result . error ) throw result . error ;
113- process . exit ( result . status ?? 1 ) ;
0 commit comments