@@ -29,6 +29,8 @@ import { installDeps, parseDeps } from './deps.js'
2929import { randomId } from './util.js'
3030import { createRequire } from './vendor.js'
3131
32+ const EXT = '.mjs'
33+
3234isMain ( ) &&
3335 main ( ) . catch ( ( err ) => {
3436 if ( err instanceof ProcessOutput ) {
@@ -56,6 +58,7 @@ export function printUsage() {
5658 --postfix=<command> postfix all commands
5759 --cwd=<path> set current directory
5860 --eval=<js>, -e evaluate script
61+ --ext=<.mjs> default extension
5962 --install, -i install dependencies
6063 --version, -v print current zx version
6164 --help, -h print help
@@ -67,7 +70,7 @@ export function printUsage() {
6770}
6871
6972export const argv = minimist ( process . argv . slice ( 2 ) , {
70- string : [ 'shell' , 'prefix' , 'postfix' , 'eval' , 'cwd' ] ,
73+ string : [ 'shell' , 'prefix' , 'postfix' , 'eval' , 'cwd' , 'ext' ] ,
7174 boolean : [
7275 'version' ,
7376 'help' ,
@@ -83,6 +86,7 @@ export const argv = minimist(process.argv.slice(2), {
8386
8487export async function main ( ) {
8588 await import ( './globals.js' )
89+ argv . ext = normalizeExt ( argv . ext )
8690 if ( argv . cwd ) $ . cwd = argv . cwd
8791 if ( argv . verbose ) $ . verbose = true
8892 if ( argv . quiet ) $ . quiet = true
@@ -102,21 +106,21 @@ export async function main() {
102106 return
103107 }
104108 if ( argv . eval ) {
105- await runScript ( argv . eval )
109+ await runScript ( argv . eval , argv . ext )
106110 return
107111 }
108112 const firstArg = argv . _ [ 0 ]
109113 updateArgv ( argv . _ . slice ( firstArg === undefined ? 0 : 1 ) )
110114 if ( ! firstArg || firstArg === '-' ) {
111- const success = await scriptFromStdin ( )
115+ const success = await scriptFromStdin ( argv . ext )
112116 if ( ! success ) {
113117 printUsage ( )
114118 process . exitCode = 1
115119 }
116120 return
117121 }
118122 if ( / ^ h t t p s ? : / . test ( firstArg ) ) {
119- await scriptFromHttp ( firstArg )
123+ await scriptFromHttp ( firstArg , argv . ext )
120124 return
121125 }
122126 const filepath = firstArg . startsWith ( 'file:///' )
@@ -125,12 +129,12 @@ export async function main() {
125129 await importPath ( filepath )
126130}
127131
128- export async function runScript ( script : string ) {
129- const filepath = path . join ( $ . cwd ?? process . cwd ( ) , `zx-${ randomId ( ) } .mjs ` )
132+ export async function runScript ( script : string , ext = EXT ) {
133+ const filepath = path . join ( $ . cwd ?? process . cwd ( ) , `zx-${ randomId ( ) } ${ ext } ` )
130134 await writeAndImport ( script , filepath )
131135}
132136
133- export async function scriptFromStdin ( ) {
137+ export async function scriptFromStdin ( ext ?: string ) {
134138 let script = ''
135139 if ( ! process . stdin . isTTY ) {
136140 process . stdin . setEncoding ( 'utf8' )
@@ -139,14 +143,14 @@ export async function scriptFromStdin() {
139143 }
140144
141145 if ( script . length > 0 ) {
142- await runScript ( script )
146+ await runScript ( script , ext )
143147 return true
144148 }
145149 }
146150 return false
147151}
148152
149- export async function scriptFromHttp ( remote : string ) {
153+ export async function scriptFromHttp ( remote : string , _ext = EXT ) {
150154 const res = await fetch ( remote )
151155 if ( ! res . ok ) {
152156 console . error ( `Error: Can't get ${ remote } ` )
@@ -155,7 +159,7 @@ export async function scriptFromHttp(remote: string) {
155159 const script = await res . text ( )
156160 const pathname = new URL ( remote ) . pathname
157161 const name = path . basename ( pathname )
158- const ext = path . extname ( pathname ) || '.mjs'
162+ const ext = path . extname ( pathname ) || _ext
159163 const cwd = $ . cwd ?? process . cwd ( )
160164 const filepath = path . join ( cwd , `${ name } -${ randomId ( ) } ${ ext } ` )
161165 await writeAndImport ( script , filepath )
@@ -299,3 +303,9 @@ export function isMain(
299303
300304 return false
301305}
306+
307+ export function normalizeExt ( ext ? : string ) {
308+ if ( ! ext ) return
309+ if ( ! / ^ \. ? \w + ( \. \w + ) * $ / . test ( ext ) ) throw new Error ( `Invalid extension ${ ext } ` )
310+ return ext [ 0 ] === '.' ? ext : `.${ ext } `
311+ }
0 commit comments