MCPcopy
hub / github.com/google/zx / readScript

Function readScript

src/cli.ts:199–240  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

197}
198
199async function readScript() {
200 const [firstArg] = argv._
201 let script = ''
202 let scriptPath = ''
203 let tempPath = ''
204 let argSlice = 1
205
206 if (argv.eval) {
207 argSlice = 0
208 script = argv.eval
209 tempPath = getFilepath($.cwd, 'zx', argv.ext)
210 } else if (!firstArg || firstArg === '-') {
211 script = await readScriptFromStdin()
212 tempPath = getFilepath($.cwd, 'zx', argv.ext)
213 if (script.length === 0) {
214 printUsage()
215 process.exitCode = 1
216 throw new Fail('No script provided')
217 }
218 } else if (/^https?:/.test(firstArg)) {
219 const { name, ext = argv.ext } = path.parse(new URL(firstArg).pathname)
220 script = await readScriptFromHttp(firstArg)
221 tempPath = getFilepath($.cwd, name, ext)
222 } else {
223 script = await fs.readFile(firstArg, 'utf8')
224 scriptPath = firstArg.startsWith('file:')
225 ? url.fileURLToPath(firstArg)
226 : path.resolve(firstArg)
227 }
228
229 const { ext, base, dir } = path.parse(tempPath || scriptPath)
230 if (ext === '' || (argv.ext && !EXT_RE.test(ext))) {
231 tempPath = getFilepath(dir, base)
232 }
233 if (ext === '.md') {
234 script = transformMarkdown(script)
235 tempPath = getFilepath(dir, base)
236 }
237 if (argSlice) updateArgv(argv._.slice(argSlice))
238
239 return { script, scriptPath, tempPath }
240}
241
242async function readScriptFromStdin(): Promise<string> {
243 return process.stdin.isTTY ? '' : stdin()

Callers 1

mainFunction · 0.85

Calls 7

transformMarkdownFunction · 0.90
updateArgvFunction · 0.90
getFilepathFunction · 0.85
readScriptFromStdinFunction · 0.85
printUsageFunction · 0.85
readScriptFromHttpFunction · 0.85
parseMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…