@@ -19,7 +19,7 @@ import { ConsoleLogger } from "#logger/node";
1919import { DistDirectory , EntryPoint , PackageRoot } from "#paths/node" ;
2020
2121import { NodeEnvironment } from "@goauthentik/core/environment/node" ;
22- import { MonoRepoRoot , resolvePackage } from "@goauthentik/core/paths/node" ;
22+ import { MonoRepoRoot } from "@goauthentik/core/paths/node" ;
2323import { BuildIdentifier } from "@goauthentik/core/version/node" ;
2424
2525import { deepmerge } from "deepmerge-ts" ;
@@ -37,8 +37,6 @@ const publicBundledDefinitions = Object.fromEntries(
3737) ;
3838logger . info ( publicBundledDefinitions , "Bundle definitions" ) ;
3939
40- const patternflyPath = resolvePackage ( "@patternfly/patternfly" , import . meta) ;
41-
4240/**
4341 * @type {Readonly<BuildOptions> }
4442 */
@@ -53,6 +51,7 @@ const BASE_ESBUILD_OPTIONS = {
5351 minify : NodeEnvironment === "production" ,
5452 legalComments : "external" ,
5553 splitting : true ,
54+ color : ! process . env . NO_COLOR ,
5655 treeShaking : true ,
5756 tsconfig : path . resolve ( PackageRoot , "tsconfig.build.json" ) ,
5857 loader : {
@@ -80,7 +79,6 @@ const BASE_ESBUILD_OPTIONS = {
8079 } ,
8180 ] ,
8281 } ) ,
83- styleLoaderPlugin ( ) ,
8482 mdxPlugin ( {
8583 root : MonoRepoRoot ,
8684 } ) ,
@@ -140,9 +138,11 @@ function doHelp() {
140138 process . exit ( 0 ) ;
141139}
142140
141+ /**
142+ *
143+ * @returns {Promise<() => Promise<void>> } dispose
144+ */
143145async function doWatch ( ) {
144- const { promise, resolve, reject } = Promise . withResolvers ( ) ;
145-
146146 logger . info ( `🤖 Watching entry points:\n\t${ Object . keys ( EntryPoint ) . join ( "\n\t" ) } ` ) ;
147147
148148 const entryPoints = Object . values ( EntryPoint ) ;
@@ -158,7 +158,7 @@ async function doWatch() {
158158
159159 const buildOptions = createESBuildOptions ( {
160160 entryPoints,
161- plugins : developmentPlugins ,
161+ plugins : [ ... developmentPlugins , styleLoaderPlugin ( { logger , watch : true } ) ] ,
162162 } ) ;
163163
164164 const buildContext = await esbuild . context ( buildOptions ) ;
@@ -176,34 +176,23 @@ async function doWatch() {
176176 logger . info ( `🔓 ${ httpURL . href } ` ) ;
177177 logger . info ( `🔒 ${ httpsURL . href } ` ) ;
178178
179- let disposing = false ;
180-
181- const delegateShutdown = ( ) => {
179+ return ( ) => {
182180 logger . flush ( ) ;
183- console . log ( "" ) ;
181+ console . info ( "" ) ;
182+ console . info ( "🛑 Stopping file watcher..." ) ;
184183
185- // We prevent multiple attempts to dispose the context
186- // because ESBuild will repeatedly restart its internal clean-up logic.
187- // However, sending a second SIGINT will still exit the process immediately.
188- if ( disposing ) return ;
189-
190- disposing = true ;
191-
192- return buildContext . dispose ( ) . then ( resolve ) . catch ( reject ) ;
184+ return buildContext . dispose ( ) ;
193185 } ;
194-
195- process . on ( "SIGINT" , delegateShutdown ) ;
196-
197- return promise ;
198186}
199187
200188async function doBuild ( ) {
201- logger . info ( `🤖 Watching entry points:\n\t${ Object . keys ( EntryPoint ) . join ( "\n\t" ) } ` ) ;
189+ logger . info ( `🤖 Building entry points:\n\t${ Object . keys ( EntryPoint ) . join ( "\n\t" ) } ` ) ;
202190
203191 const entryPoints = Object . values ( EntryPoint ) ;
204192
205193 const buildOptions = createESBuildOptions ( {
206194 entryPoints,
195+ plugins : [ styleLoaderPlugin ( { logger } ) ] ,
207196 } ) ;
208197
209198 await esbuild . build ( buildOptions ) ;
@@ -245,10 +234,43 @@ await cleanDistDirectory()
245234 // ---
246235 . then ( ( ) =>
247236 delegateCommand ( )
248- . then ( ( ) => {
249- process . exit ( 0 ) ;
237+ . then ( ( dispose ) => {
238+ if ( ! dispose ) {
239+ process . exit ( 0 ) ;
240+ }
241+
242+ /**
243+ * @type {Promise<void> }
244+ */
245+ const signalListener = new Promise ( ( resolve ) => {
246+ // We prevent multiple attempts to dispose the context
247+ // because ESBuild will repeatedly restart its internal clean-up logic.
248+ // However, sending a second SIGINT will still exit the process immediately.
249+ let signalCount = 0 ;
250+
251+ process . on ( "SIGINT" , ( ) => {
252+ if ( signalCount > 3 ) {
253+ // Something is taking too long and the user wants to exit now.
254+ console . log ( "🛑 Forcing exit..." ) ;
255+ process . exit ( 0 ) ;
256+ }
257+ } ) ;
258+
259+ process . once ( "SIGINT" , ( ) => {
260+ signalCount ++ ;
261+
262+ dispose ( ) . finally ( ( ) => {
263+ console . log ( "✅ Done!" ) ;
264+
265+ resolve ( ) ;
266+ } ) ;
267+ } ) ;
268+
269+ logger . info ( "🚪 Press Ctrl+C to exit." ) ;
270+ } ) ;
271+
272+ return signalListener ;
250273 } )
251- . catch ( ( ) => {
252- process . exit ( 1 ) ;
253- } ) ,
274+ . then ( ( ) => process . exit ( 0 ) )
275+ . catch ( ( ) => process . exit ( 1 ) ) ,
254276 ) ;
0 commit comments