1+ #!/usr/bin/env node
2+ //@ts -check
3+
4+ import fs from 'node:fs'
5+ import * as url from 'node:url'
6+ import { spawn } from 'node:child_process'
7+
8+ function spawnPromisified ( command , args , options ) {
9+ return new Promise ( ( resolve , reject ) => {
10+ const proc = spawn ( command , args , options )
11+ proc . stdout . setEncoding ( 'utf8' )
12+ proc . stdout . on ( 'data' , ( data ) => {
13+ console . log ( data )
14+ } )
15+ proc . stderr . setEncoding ( 'utf8' )
16+ proc . stderr . on ( 'data' , ( data ) => {
17+ console . error ( data )
18+ } )
19+ proc . on ( 'close' , ( code ) => {
20+ if ( code !== 0 ) {
21+ reject ( code )
22+ } else {
23+ resolve ( code )
24+ }
25+ } )
26+ } )
27+ }
28+
29+ await ( async ( ) => {
30+ // If dependencies are not vendored-in, install them at runtime.
31+ try {
32+ await fs . accessSync (
33+ url . fileURLToPath ( new URL ( './node_modules' , import . meta. url ) ) ,
34+ fs . constants . R_OK
35+ )
36+ } catch {
37+ try {
38+ await spawnPromisified ( 'npm' , [ 'ci' ] , {
39+ cwd : url . fileURLToPath ( new URL ( '.' , import . meta. url ) )
40+ } )
41+ } catch {
42+ process . exit ( 1 )
43+ }
44+ } finally {
45+ // Compile TypeScript.
46+ try {
47+ await spawnPromisified ( 'npm' , [ 'run' , 'build' ] , {
48+ cwd : url . fileURLToPath ( new URL ( '.' , import . meta. url ) )
49+ } )
50+ } catch {
51+ process . exit ( 1 )
52+ }
53+ // Run the main script.
54+ const action = await import ( './dist/index.js' )
55+ await action . default ( )
56+ }
57+ } ) ( )
0 commit comments