Skip to content

Commit eff515f

Browse files
committed
fix: shim non-existent source
1 parent b9c389b commit eff515f

File tree

3 files changed

+50
-21
lines changed

3 files changed

+50
-21
lines changed

src/index.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -263,36 +263,38 @@ interface RuntimeInfo {
263263
* @return Promise of infomation of runtime of `target`.
264264
*/
265265
async function searchScriptRuntime (target: string, opts: InternalOptions): Promise<RuntimeInfo> {
266+
let shebang;
266267
try {
267268
const data = await opts.fs_.readFile(target, 'utf8')
268269

269270
// First, check if the bin is a #! of some sort.
270271
const firstLine = data.trim().split(/\r*\n/)[0]
271-
const shebang = firstLine.match(shebangExpr)
272-
if (!shebang) {
273-
// If not, infer script type from its extension.
274-
// If the inference fails, it's something that'll be compiled, or some other
275-
// sort of script, and just call it directly.
276-
const targetExtension = path.extname(target).toLowerCase()
277-
return {
278-
// undefined if extension is unknown but it's converted to null.
279-
program: extensionToProgramMap.get(targetExtension) || null,
280-
additionalArgs: ''
272+
shebang = firstLine.match(shebangExpr)
273+
} catch (err: any) {
274+
if (isWindows() && err.code === 'ENOENT') {
275+
if (await opts.fs_.stat(`${target}${getExeExtension()}`)) {
276+
return {
277+
program: null,
278+
additionalArgs: '',
279+
}
281280
}
282281
}
282+
if (err.code !== 'ENOENT') throw err
283+
}
284+
if (!shebang) {
285+
// If not, infer script type from its extension.
286+
// If the inference fails, it's something that'll be compiled, or some other
287+
// sort of script, and just call it directly.
288+
const targetExtension = path.extname(target).toLowerCase()
283289
return {
284-
program: shebang[1],
285-
additionalArgs: shebang[2]
286-
}
287-
} catch (err: any) {
288-
if (!isWindows() || err.code !== 'ENOENT') throw err
289-
if (await opts.fs_.stat(`${target}${getExeExtension()}`)) {
290-
return {
291-
program: null,
292-
additionalArgs: '',
293-
}
290+
// undefined if extension is unknown but it's converted to null.
291+
program: extensionToProgramMap.get(targetExtension) || null,
292+
additionalArgs: ''
294293
}
295-
throw err
294+
}
295+
return {
296+
program: shebang[1],
297+
additionalArgs: shebang[2]
296298
}
297299
}
298300

test/__snapshots__/test.js.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,22 @@ exit $LASTEXITCODE
736736
"
737737
`;
738738

739+
exports[`no src file missing.shim 1`] = `
740+
"#!/bin/sh
741+
basedir=$(dirname \\"$(echo \\"$0\\" | sed -e 's,\\\\\\\\,/,g')\\")
742+
743+
case \`uname\` in
744+
*CYGWIN*) basedir=\`cygpath -w \\"$basedir\\"\`;;
745+
esac
746+
747+
if [ -x \\"$basedir/node\\" ]; then
748+
exec \\"$basedir/node\\" \\"$basedir/missing.js\\" \\"$@\\"
749+
else
750+
exec node \\"$basedir/missing.js\\" \\"$@\\"
751+
fi
752+
"
753+
`;
754+
739755
exports[`shebang with -S from.env.S.shim 1`] = `
740756
"#!/bin/sh
741757
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")

test/test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ function testFile (fileName, lineEnding = '\n') {
1919
})
2020
}
2121

22+
describe('no src file', () => {
23+
const src = path.resolve(fixtures, 'missing.js')
24+
const to = path.resolve(fixtures, 'missing.shim')
25+
26+
beforeAll(() => {
27+
return cmdShim(src, to, { createCmdFile: false, fs })
28+
})
29+
30+
testFile(to)
31+
})
32+
2233
describe('no cmd file', () => {
2334
const src = path.resolve(fixtures, 'src.exe')
2435
const to = path.resolve(fixtures, 'exe.shim')

0 commit comments

Comments
 (0)