Skip to content

Commit

Permalink
fix: shim non-existent source
Browse files Browse the repository at this point in the history
  • Loading branch information
trappar committed Feb 25, 2023
1 parent 585872e commit 8855d40
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
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
}
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

0 comments on commit 8855d40

Please sign in to comment.