@@ -29,7 +29,17 @@ import { installDeps, parseDeps } from './deps.js'
2929import { randomId } from './util.js'
3030import { createRequire } from './vendor.js'
3131
32- function printUsage ( ) {
32+ isMain ( ) &&
33+ main ( ) . catch ( ( err ) => {
34+ if ( err instanceof ProcessOutput ) {
35+ console . error ( 'Error:' , err . message )
36+ } else {
37+ console . error ( err )
38+ }
39+ process . exitCode = 1
40+ } )
41+
42+ export function printUsage ( ) {
3343 // language=txt
3444 console . log ( `
3545 ${ chalk . bold ( 'zx ' + getVersion ( ) ) }
@@ -55,7 +65,7 @@ function printUsage() {
5565` )
5666}
5767
58- const argv = minimist ( process . argv . slice ( 2 ) , {
68+ export const argv = minimist ( process . argv . slice ( 2 ) , {
5969 string : [ 'shell' , 'prefix' , 'postfix' , 'eval' , 'cwd' ] ,
6070 boolean : [
6171 'version' ,
@@ -70,7 +80,7 @@ const argv = minimist(process.argv.slice(2), {
7080 stopEarly : true ,
7181} )
7282
73- ; ( async function main ( ) {
83+ export async function main ( ) {
7484 await import ( './globals.js' )
7585 if ( argv . cwd ) $ . cwd = argv . cwd
7686 if ( argv . verbose ) $ . verbose = true
@@ -112,21 +122,14 @@ const argv = minimist(process.argv.slice(2), {
112122 ? url . fileURLToPath ( firstArg )
113123 : path . resolve ( firstArg )
114124 await importPath ( filepath )
115- } ) ( ) . catch ( ( err ) => {
116- if ( err instanceof ProcessOutput ) {
117- console . error ( 'Error:' , err . message )
118- } else {
119- console . error ( err )
120- }
121- process . exitCode = 1
122- } )
125+ }
123126
124- async function runScript ( script : string ) {
127+ export async function runScript ( script : string ) {
125128 const filepath = path . join ( $ . cwd ?? process . cwd ( ) , `zx-${ randomId ( ) } .mjs` )
126129 await writeAndImport ( script , filepath )
127130}
128131
129- async function scriptFromStdin ( ) {
132+ export async function scriptFromStdin ( ) {
130133 let script = ''
131134 if ( ! process . stdin . isTTY ) {
132135 process . stdin . setEncoding ( 'utf8' )
@@ -142,7 +145,7 @@ async function scriptFromStdin() {
142145 return false
143146}
144147
145- async function scriptFromHttp ( remote : string ) {
148+ export async function scriptFromHttp ( remote : string ) {
146149 const res = await fetch ( remote )
147150 if ( ! res . ok ) {
148151 console . error ( `Error: Can't get ${ remote } ` )
@@ -157,7 +160,7 @@ async function scriptFromHttp(remote: string) {
157160 await writeAndImport ( script , filepath )
158161}
159162
160- async function writeAndImport (
163+ export async function writeAndImport (
161164 script : string | Buffer ,
162165 filepath : string ,
163166 origin = filepath
@@ -170,7 +173,7 @@ async function writeAndImport(
170173 }
171174}
172175
173- async function importPath ( filepath : string , origin = filepath ) {
176+ export async function importPath ( filepath : string , origin = filepath ) {
174177 const ext = path . extname ( filepath )
175178 const base = path . basename ( filepath )
176179 const dir = path . dirname ( filepath )
@@ -201,14 +204,14 @@ async function importPath(filepath: string, origin = filepath) {
201204 await import ( url . pathToFileURL ( filepath ) . toString ( ) )
202205}
203206
204- function injectGlobalRequire ( origin : string ) {
207+ export function injectGlobalRequire ( origin : string ) {
205208 const __filename = path . resolve ( origin )
206209 const __dirname = path . dirname ( __filename )
207210 const require = createRequire ( origin )
208211 Object . assign ( globalThis , { __filename, __dirname, require } )
209212}
210213
211- function transformMarkdown ( buf : Buffer ) {
214+ export function transformMarkdown ( buf : Buffer ) {
212215 const source = buf . toString ( )
213216 const output = [ ]
214217 let state = 'root'
@@ -279,6 +282,16 @@ function transformMarkdown(buf: Buffer) {
279282 return output . join ( '\n' )
280283}
281284
282- function getVersion ( ) : string {
285+ export function getVersion ( ) : string {
283286 return createRequire ( import . meta. url ) ( '../package.json' ) . version
284287}
288+
289+ function isMain ( ) {
290+ if ( import . meta. url . startsWith ( 'file:' ) ) {
291+ const modulePath = url . fileURLToPath ( import . meta. url ) . replace ( / \. \w + $ / , '' )
292+ const mainPath = fs . realpathSync ( process . argv [ 1 ] ) . replace ( / \. \w + $ / , '' )
293+ return mainPath === modulePath
294+ }
295+
296+ return false
297+ }
0 commit comments