@@ -29,7 +29,17 @@ import { installDeps, parseDeps } from './deps.js'
29
29
import { randomId } from './util.js'
30
30
import { createRequire } from './vendor.js'
31
31
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 ( ) {
33
43
// language=txt
34
44
console . log ( `
35
45
${ chalk . bold ( 'zx ' + getVersion ( ) ) }
@@ -55,7 +65,7 @@ function printUsage() {
55
65
` )
56
66
}
57
67
58
- const argv = minimist ( process . argv . slice ( 2 ) , {
68
+ export const argv = minimist ( process . argv . slice ( 2 ) , {
59
69
string : [ 'shell' , 'prefix' , 'postfix' , 'eval' , 'cwd' ] ,
60
70
boolean : [
61
71
'version' ,
@@ -70,7 +80,7 @@ const argv = minimist(process.argv.slice(2), {
70
80
stopEarly : true ,
71
81
} )
72
82
73
- ; ( async function main ( ) {
83
+ export async function main ( ) {
74
84
await import ( './globals.js' )
75
85
if ( argv . cwd ) $ . cwd = argv . cwd
76
86
if ( argv . verbose ) $ . verbose = true
@@ -112,21 +122,14 @@ const argv = minimist(process.argv.slice(2), {
112
122
? url . fileURLToPath ( firstArg )
113
123
: path . resolve ( firstArg )
114
124
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
+ }
123
126
124
- async function runScript ( script : string ) {
127
+ export async function runScript ( script : string ) {
125
128
const filepath = path . join ( $ . cwd ?? process . cwd ( ) , `zx-${ randomId ( ) } .mjs` )
126
129
await writeAndImport ( script , filepath )
127
130
}
128
131
129
- async function scriptFromStdin ( ) {
132
+ export async function scriptFromStdin ( ) {
130
133
let script = ''
131
134
if ( ! process . stdin . isTTY ) {
132
135
process . stdin . setEncoding ( 'utf8' )
@@ -142,7 +145,7 @@ async function scriptFromStdin() {
142
145
return false
143
146
}
144
147
145
- async function scriptFromHttp ( remote : string ) {
148
+ export async function scriptFromHttp ( remote : string ) {
146
149
const res = await fetch ( remote )
147
150
if ( ! res . ok ) {
148
151
console . error ( `Error: Can't get ${ remote } ` )
@@ -157,7 +160,7 @@ async function scriptFromHttp(remote: string) {
157
160
await writeAndImport ( script , filepath )
158
161
}
159
162
160
- async function writeAndImport (
163
+ export async function writeAndImport (
161
164
script : string | Buffer ,
162
165
filepath : string ,
163
166
origin = filepath
@@ -170,7 +173,7 @@ async function writeAndImport(
170
173
}
171
174
}
172
175
173
- async function importPath ( filepath : string , origin = filepath ) {
176
+ export async function importPath ( filepath : string , origin = filepath ) {
174
177
const ext = path . extname ( filepath )
175
178
const base = path . basename ( filepath )
176
179
const dir = path . dirname ( filepath )
@@ -201,14 +204,14 @@ async function importPath(filepath: string, origin = filepath) {
201
204
await import ( url . pathToFileURL ( filepath ) . toString ( ) )
202
205
}
203
206
204
- function injectGlobalRequire ( origin : string ) {
207
+ export function injectGlobalRequire ( origin : string ) {
205
208
const __filename = path . resolve ( origin )
206
209
const __dirname = path . dirname ( __filename )
207
210
const require = createRequire ( origin )
208
211
Object . assign ( globalThis , { __filename, __dirname, require } )
209
212
}
210
213
211
- function transformMarkdown ( buf : Buffer ) {
214
+ export function transformMarkdown ( buf : Buffer ) {
212
215
const source = buf . toString ( )
213
216
const output = [ ]
214
217
let state = 'root'
@@ -279,6 +282,16 @@ function transformMarkdown(buf: Buffer) {
279
282
return output . join ( '\n' )
280
283
}
281
284
282
- function getVersion ( ) : string {
285
+ export function getVersion ( ) : string {
283
286
return createRequire ( import . meta. url ) ( '../package.json' ) . version
284
287
}
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