@@ -26,6 +26,7 @@ import {
26
26
fetch ,
27
27
fs ,
28
28
path ,
29
+ stdin ,
29
30
VERSION ,
30
31
} from './index.js'
31
32
import { installDeps , parseDeps } from './deps.js'
@@ -113,100 +114,95 @@ export async function main() {
113
114
await startRepl ( )
114
115
return
115
116
}
116
- if ( argv . eval ) {
117
- await runScript ( argv . eval , argv . ext )
118
- return
119
- }
120
- const [ firstArg ] = argv . _
121
- updateArgv ( argv . _ . slice ( firstArg === undefined ? 0 : 1 ) )
122
- if ( ! firstArg || firstArg === '-' ) {
123
- const success = await scriptFromStdin ( argv . ext )
124
- if ( ! success ) {
125
- printUsage ( )
126
- process . exitCode = 1
127
- }
128
- return
129
- }
130
- if ( / ^ h t t p s ? : / . test ( firstArg ) ) {
131
- await scriptFromHttp ( firstArg , argv . ext )
132
- return
133
- }
134
- const filepath = firstArg . startsWith ( 'file:' )
135
- ? url . fileURLToPath ( firstArg )
136
- : path . resolve ( firstArg )
137
- await importPath ( filepath )
138
- }
139
117
140
- export async function runScript ( script : string , ext ?: string ) {
141
- const filepath = getFilepath ( $ . cwd , 'zx' , ext )
142
- await writeAndImport ( script , filepath )
118
+ const { script, scriptPath, tempPath } = await readScript ( )
119
+ await runScript ( script , scriptPath , tempPath )
143
120
}
144
121
145
- export async function scriptFromStdin ( ext ?: string ) : Promise < boolean > {
146
- let script = ''
147
- if ( ! process . stdin . isTTY ) {
148
- process . stdin . setEncoding ( 'utf8' )
149
- for await ( const chunk of process . stdin ) {
150
- script += chunk
122
+ async function runScript (
123
+ script : string ,
124
+ scriptPath : string ,
125
+ tempPath : string
126
+ ) : Promise < void > {
127
+ const rmTemp = ( ) => fs . rmSync ( tempPath , { force : true } )
128
+ try {
129
+ if ( tempPath ) {
130
+ scriptPath = tempPath
131
+ await fs . writeFile ( tempPath , script )
151
132
}
152
133
153
- if ( script . length > 0 ) {
154
- await runScript ( script , ext )
155
- return true
134
+ if ( argv . install ) {
135
+ await installDeps (
136
+ parseDeps ( script ) ,
137
+ path . dirname ( scriptPath ) ,
138
+ argv . registry
139
+ )
156
140
}
157
- }
158
- return false
159
- }
160
141
161
- export async function scriptFromHttp ( remote : string , _ext ? : string ) {
162
- const res = await fetch ( remote )
163
- if ( ! res . ok ) {
164
- console . error ( `Error: Can't get ${ remote } ` )
165
- process . exit ( 1 )
166
- }
167
- const script = await res . text ( )
168
- const pathname = new URL ( remote ) . pathname
169
- const { name , ext } = path . parse ( pathname )
170
- const filepath = getFilepath ( $ . cwd , name , _ext || ext )
171
- await writeAndImport ( script , filepath )
172
- }
142
+ injectGlobalRequire ( scriptPath )
143
+ process . once ( 'exit' , rmTemp )
173
144
174
- export async function writeAndImport (
175
- script : string | Buffer ,
176
- filepath : string ,
177
- origin = filepath
178
- ) {
179
- await fs . writeFile ( filepath , script )
180
- try {
181
- process . once ( 'exit' , ( ) => fs . rmSync ( filepath , { force : true } ) )
182
- await importPath ( filepath , origin )
145
+ // TODO: fix unanalyzable-dynamic-import to work correctly with jsr.io
146
+ await import ( url . pathToFileURL ( scriptPath ) . toString ( ) )
183
147
} finally {
184
- await fs . rm ( filepath )
148
+ rmTemp ( )
185
149
}
186
150
}
187
151
188
- export async function importPath (
189
- filepath : string ,
190
- origin = filepath
191
- ) : Promise < void > {
192
- const contents = await fs . readFile ( filepath , 'utf8' )
193
- const { ext, base, dir } = path . parse ( filepath )
194
- const tempFilename = getFilepath ( dir , base )
152
+ async function readScript ( ) {
153
+ const [ firstArg ] = argv . _
154
+ let script = ''
155
+ let scriptPath = ''
156
+ let tempPath = ''
157
+ let argSlice = 1
158
+
159
+ if ( argv . eval ) {
160
+ argSlice = 0
161
+ script = argv . eval
162
+ tempPath = getFilepath ( $ . cwd , 'zx' , argv . ext )
163
+ } else if ( ! firstArg || firstArg === '-' ) {
164
+ script = await readScriptFromStdin ( )
165
+ tempPath = getFilepath ( $ . cwd , 'zx' , argv . ext )
166
+ if ( script . length === 0 ) {
167
+ printUsage ( )
168
+ process . exitCode = 1
169
+ throw new Error ( 'No script provided' )
170
+ }
171
+ } else if ( / ^ h t t p s ? : / . test ( firstArg ) ) {
172
+ const { name, ext = argv . ext } = path . parse ( new URL ( firstArg ) . pathname )
173
+ script = await readScriptFromHttp ( firstArg )
174
+ tempPath = getFilepath ( $ . cwd , name , ext )
175
+ } else {
176
+ script = await fs . readFile ( firstArg , 'utf8' )
177
+ scriptPath = firstArg . startsWith ( 'file:' )
178
+ ? url . fileURLToPath ( firstArg )
179
+ : path . resolve ( firstArg )
180
+ }
195
181
182
+ const { ext, base, dir } = path . parse ( tempPath || scriptPath )
196
183
if ( ext === '' ) {
197
- return writeAndImport ( contents , tempFilename , origin )
184
+ tempPath = getFilepath ( dir , base )
198
185
}
199
186
if ( ext === '.md' ) {
200
- return writeAndImport ( transformMarkdown ( contents ) , tempFilename , origin )
201
- }
202
- if ( argv . install ) {
203
- const deps = parseDeps ( contents )
204
- await installDeps ( deps , dir , argv . registry )
187
+ script = transformMarkdown ( script )
188
+ tempPath = getFilepath ( dir , base )
205
189
}
190
+ if ( argSlice ) updateArgv ( argv . _ . slice ( argSlice ) )
191
+
192
+ return { script, scriptPath, tempPath }
193
+ }
194
+
195
+ async function readScriptFromStdin ( ) : Promise < string > {
196
+ return ! process . stdin . isTTY ? stdin ( ) : ''
197
+ }
206
198
207
- injectGlobalRequire ( origin )
208
- // TODO: fix unanalyzable-dynamic-import to work correctly with jsr.io
209
- await import ( url . pathToFileURL ( filepath ) . toString ( ) )
199
+ async function readScriptFromHttp ( remote : string ) : Promise < string > {
200
+ const res = await fetch ( remote )
201
+ if ( ! res . ok ) {
202
+ console . error ( `Error: Can't get ${ remote } ` )
203
+ process . exit ( 1 )
204
+ }
205
+ return res . text ( )
210
206
}
211
207
212
208
export function injectGlobalRequire ( origin : string ) {
0 commit comments