From 6ecbad2733df49a98ccbcb8b3eeb2244753f7210 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Thu, 26 Dec 2024 10:33:38 +0300 Subject: [PATCH] fix(cli): always check if tempfile exists (#1041) --- src/cli.ts | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index 9f8b6378ef..d2be0bf8e0 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -133,8 +133,8 @@ export async function main() { await importPath(filepath) } -export async function runScript(script: string, ext = EXT) { - const filepath = path.join($.cwd ?? process.cwd(), `zx-${randomId()}${ext}`) +export async function runScript(script: string, ext?: string) { + const filepath = getFilepath($.cwd, 'zx', ext) await writeAndImport(script, filepath) } @@ -154,7 +154,7 @@ export async function scriptFromStdin(ext?: string): Promise { return false } -export async function scriptFromHttp(remote: string, _ext = EXT) { +export async function scriptFromHttp(remote: string, _ext?: string) { const res = await fetch(remote) if (!res.ok) { console.error(`Error: Can't get ${remote}`) @@ -162,10 +162,8 @@ export async function scriptFromHttp(remote: string, _ext = EXT) { } const script = await res.text() const pathname = new URL(remote).pathname - const name = path.basename(pathname) - const ext = path.extname(pathname) || _ext - const cwd = $.cwd ?? process.cwd() - const filepath = path.join(cwd, `${name}-${randomId()}${ext}`) + const { name, ext } = path.parse(pathname) + const filepath = getFilepath($.cwd, name, _ext || ext) await writeAndImport(script, filepath) } @@ -188,22 +186,15 @@ export async function importPath( origin = filepath ): Promise { const { ext, base, dir } = path.parse(filepath) + const tempFilename = getFilepath(dir, base) if (ext === '') { - const tmpFilename = fs.existsSync(filepath + EXT) - ? base + '-' + randomId() + EXT - : base + EXT - - return writeAndImport( - await fs.readFile(filepath), - path.join(dir, tmpFilename), - origin - ) + return writeAndImport(await fs.readFile(filepath), tempFilename, origin) } if (ext === '.md') { return writeAndImport( transformMarkdown(await fs.readFile(filepath)), - path.join(dir, base + EXT), + tempFilename, origin ) } @@ -311,3 +302,14 @@ export function isMain( export function normalizeExt(ext?: string): string | undefined { return ext ? path.parse(`foo.${ext}`).ext : ext } + +// prettier-ignore +function getFilepath(cwd = '.', name = 'zx', _ext?: string): string { + const ext = _ext || argv.ext || EXT + return [ + name + ext, + name + '-' + randomId() + ext, + ] + .map(f => path.resolve(process.cwd(), cwd, f)) + .find(f => !fs.existsSync(f))! +}