Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,36 +263,38 @@ interface RuntimeInfo {
* @return Promise of infomation of runtime of `target`.
*/
async function searchScriptRuntime (target: string, opts: InternalOptions): Promise<RuntimeInfo> {
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
Comment on lines +274 to +282

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The operation to compare err.code to ENOENT could be executed twice on Windows. You can optimize it by moving if (err.code !== 'ENOENT') throw err up to right under catch.

}
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]
}
}

Expand Down
16 changes: 16 additions & 0 deletions test/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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')")
Expand Down
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down