diff --git a/src/index.ts b/src/index.ts index ba1dcfb..36e2ebc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -263,36 +263,38 @@ interface RuntimeInfo { * @return Promise of infomation of runtime of `target`. */ async function searchScriptRuntime (target: string, opts: InternalOptions): Promise { + let shebang; try { const data = await opts.fs_.readFile(target, 'utf8') // First, check if the bin is a #! of some sort. const firstLine = data.trim().split(/\r*\n/)[0] - const shebang = firstLine.match(shebangExpr) - if (!shebang) { - // If not, infer script type from its extension. - // If the inference fails, it's something that'll be compiled, or some other - // sort of script, and just call it directly. - const targetExtension = path.extname(target).toLowerCase() - return { - // undefined if extension is unknown but it's converted to null. - program: extensionToProgramMap.get(targetExtension) || null, - additionalArgs: '' + shebang = firstLine.match(shebangExpr) + } catch (err: any) { + if (isWindows() && err.code === 'ENOENT') { + if (await opts.fs_.stat(`${target}${getExeExtension()}`)) { + return { + program: null, + additionalArgs: '', + } } } + if (err.code !== 'ENOENT') throw err + } + if (!shebang) { + // If not, infer script type from its extension. + // If the inference fails, it's something that'll be compiled, or some other + // sort of script, and just call it directly. + const targetExtension = path.extname(target).toLowerCase() return { - program: shebang[1], - additionalArgs: shebang[2] - } - } catch (err: any) { - if (!isWindows() || err.code !== 'ENOENT') throw err - if (await opts.fs_.stat(`${target}${getExeExtension()}`)) { - return { - program: null, - additionalArgs: '', - } + // undefined if extension is unknown but it's converted to null. + program: extensionToProgramMap.get(targetExtension) || null, + additionalArgs: '' } - throw err + } + return { + program: shebang[1], + additionalArgs: shebang[2] } } diff --git a/test/__snapshots__/test.js.snap b/test/__snapshots__/test.js.snap index 7279618..0cce4ec 100644 --- a/test/__snapshots__/test.js.snap +++ b/test/__snapshots__/test.js.snap @@ -736,6 +736,22 @@ exit $LASTEXITCODE " `; +exports[`no src file missing.shim 1`] = ` +"#!/bin/sh +basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')") + +case \`uname\` in + *CYGWIN*) basedir=\`cygpath -w "$basedir"\`;; +esac + +if [ -x "$basedir/node" ]; then + exec "$basedir/node" "$basedir/missing.js" "$@" +else + exec node "$basedir/missing.js" "$@" +fi +" +`; + exports[`shebang with -S from.env.S.shim 1`] = ` "#!/bin/sh basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')") diff --git a/test/test.js b/test/test.js index 1cd93a3..23979ce 100755 --- a/test/test.js +++ b/test/test.js @@ -19,6 +19,17 @@ function testFile (fileName, lineEnding = '\n') { }) } +describe('no src file', () => { + const src = path.resolve(fixtures, 'missing.js') + const to = path.resolve(fixtures, 'missing.shim') + + beforeAll(() => { + return cmdShim(src, to, { createCmdFile: false, fs }) + }) + + testFile(to) +}) + describe('no cmd file', () => { const src = path.resolve(fixtures, 'src.exe') const to = path.resolve(fixtures, 'exe.shim')