Skip to content

Commit 36d9c66

Browse files
fix: fix generated shell scripts for MSYS2/MINGW (#48)
* fix: fix generated shell scripts for MSYS2/MINGW reference: npm#30 * feat: update shell scripts to match npm * fix: fix generated shell scripts
1 parent b9c389b commit 36d9c66

File tree

3 files changed

+87
-18
lines changed

3 files changed

+87
-18
lines changed

src/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ import {promisify} from 'util'
103103

104104
import path = require('path')
105105
import isWindows = require('is-windows')
106+
const isCygwin = () => isWindows() && (process.env.TERM === 'CYGWIN' || process.env.MSYSTEM !== undefined)
106107
import CMD_EXTENSION = require('cmd-extension')
107108
const shebangExpr = /^#!\s*(?:\/usr\/bin\/env(?:\s+-S\s*)?)?\s*([^ \t]+)(.*)$/
108109
const DEFAULT_OPTIONS = {
@@ -437,7 +438,11 @@ function generateShShim (src: string, to: string, opts: InternalOptions): string
437438
// basedir=`dirname "$0"`
438439
//
439440
// case `uname` in
440-
// *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
441+
// *CYGWIN*|*MINGW*|*MSYS*)
442+
// if command -v cygpath > /dev/null 2>&1; then
443+
// basedir=`cygpath -w "$basedir"`
444+
// fi
445+
// ;;
441446
// esac
442447
//
443448
// export NODE_PATH="<nodepath>"
@@ -453,7 +458,11 @@ function generateShShim (src: string, to: string, opts: InternalOptions): string
453458
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
454459
455460
case \`uname\` in
456-
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
461+
*CYGWIN*|*MINGW*|*MSYS*)
462+
if command -v cygpath > /dev/null 2>&1; then
463+
basedir=\`cygpath -w "$basedir"\`
464+
fi
465+
;;
457466
esac
458467
459468
`
@@ -663,7 +672,7 @@ function normalizePathEnvVar (nodePath: undefined | string | string[]): Normaliz
663672
let result = {} as NormalizedPathEnvVar
664673
for (let i = 0; i < split.length; i++) {
665674
const win32 = split[i].split('/').join('\\')
666-
const posix = isWindows() ? split[i].split('\\').join('/').replace(/^([^:\\/]*):/, (_, $1) => `/mnt/${$1.toLowerCase()}`) : split[i]
675+
const posix = isWindows() ? split[i].split('\\').join('/').replace(/^([^:\\/]*):/, (_, $1) => `${isCygwin() ? '/proc/cygdrive' : '/mnt'}/${$1.toLowerCase()}`) : split[i]
667676

668677
result.win32 = result.win32 ? `${result.win32};${win32}` : win32
669678
result.posix = result.posix ? `${result.posix}:${posix}` : posix

test/__snapshots__/e2e.test.js.snap

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ exports[`create a command shim for a .exe file 1`] = `
55
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
66
77
case \`uname\` in
8-
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
8+
*CYGWIN*|*MINGW*|*MSYS*)
9+
if command -v cygpath > /dev/null 2>&1; then
10+
basedir=\`cygpath -w "$basedir"\`
11+
fi
12+
;;
913
esac
1014
1115
"$basedir/foo" "$@"

test/__snapshots__/test.js.snap

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ exports[`custom node executable env.shim 1`] = `
55
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
66
77
case \`uname\` in
8-
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
8+
*CYGWIN*|*MINGW*|*MSYS*)
9+
if command -v cygpath > /dev/null 2>&1; then
10+
basedir=\`cygpath -w "$basedir"\`
11+
fi
12+
;;
913
esac
1014
1115
"/.pnpm/nodejs/16.0.0/node" "$basedir/src.env" "$@"
@@ -44,7 +48,11 @@ exports[`env shebang env.shim 1`] = `
4448
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
4549
4650
case \`uname\` in
47-
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
51+
*CYGWIN*|*MINGW*|*MSYS*)
52+
if command -v cygpath > /dev/null 2>&1; then
53+
basedir=\`cygpath -w "$basedir"\`
54+
fi
55+
;;
4856
esac
4957
5058
if [ -x "$basedir/node" ]; then
@@ -103,7 +111,11 @@ exports[`env shebang with NODE_PATH env.shim 1`] = `
103111
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
104112
105113
case \`uname\` in
106-
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
114+
*CYGWIN*|*MINGW*|*MSYS*)
115+
if command -v cygpath > /dev/null 2>&1; then
116+
basedir=\`cygpath -w "$basedir"\`
117+
fi
118+
;;
107119
esac
108120
109121
if [ -z "$NODE_PATH" ]; then
@@ -185,7 +197,11 @@ exports[`env shebang with PATH extending env.shim 1`] = `
185197
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
186198
187199
case \`uname\` in
188-
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
200+
*CYGWIN*|*MINGW*|*MSYS*)
201+
if command -v cygpath > /dev/null 2>&1; then
202+
basedir=\`cygpath -w "$basedir"\`
203+
fi
204+
;;
189205
esac
190206
191207
export PATH="/add-to-path:$PATH"
@@ -256,7 +272,11 @@ exports[`env shebang with args env.args.shim 1`] = `
256272
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
257273
258274
case \`uname\` in
259-
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
275+
*CYGWIN*|*MINGW*|*MSYS*)
276+
if command -v cygpath > /dev/null 2>&1; then
277+
basedir=\`cygpath -w "$basedir"\`
278+
fi
279+
;;
260280
esac
261281
262282
if [ -x "$basedir/node" ]; then
@@ -315,7 +335,11 @@ exports[`env shebang with default args env.shim 1`] = `
315335
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
316336
317337
case \`uname\` in
318-
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
338+
*CYGWIN*|*MINGW*|*MSYS*)
339+
if command -v cygpath > /dev/null 2>&1; then
340+
basedir=\`cygpath -w "$basedir"\`
341+
fi
342+
;;
319343
esac
320344
321345
if [ -x "$basedir/node" ]; then
@@ -374,7 +398,11 @@ exports[`env shebang with no NODE_PATH env.shim 1`] = `
374398
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
375399
376400
case \`uname\` in
377-
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
401+
*CYGWIN*|*MINGW*|*MSYS*)
402+
if command -v cygpath > /dev/null 2>&1; then
403+
basedir=\`cygpath -w "$basedir"\`
404+
fi
405+
;;
378406
esac
379407
380408
if [ -x "$basedir/node" ]; then
@@ -433,7 +461,11 @@ exports[`explicit shebang sh.shim 1`] = `
433461
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
434462
435463
case \`uname\` in
436-
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
464+
*CYGWIN*|*MINGW*|*MSYS*)
465+
if command -v cygpath > /dev/null 2>&1; then
466+
basedir=\`cygpath -w "$basedir"\`
467+
fi
468+
;;
437469
esac
438470
439471
if [ -x "$basedir//usr/bin/sh" ]; then
@@ -492,7 +524,11 @@ exports[`explicit shebang with args sh.args.shim 1`] = `
492524
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
493525
494526
case \`uname\` in
495-
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
527+
*CYGWIN*|*MINGW*|*MSYS*)
528+
if command -v cygpath > /dev/null 2>&1; then
529+
basedir=\`cygpath -w "$basedir"\`
530+
fi
531+
;;
496532
esac
497533
498534
if [ -x "$basedir//usr/bin/sh" ]; then
@@ -551,7 +587,11 @@ exports[`explicit shebang with args, linking to another drive on Windows sh.args
551587
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
552588
553589
case \`uname\` in
554-
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
590+
*CYGWIN*|*MINGW*|*MSYS*)
591+
if command -v cygpath > /dev/null 2>&1; then
592+
basedir=\`cygpath -w "$basedir"\`
593+
fi
594+
;;
555595
esac
556596
557597
if [ -x "$basedir//usr/bin/sh" ]; then
@@ -610,7 +650,11 @@ exports[`explicit shebang with prog args sh.args.shim 1`] = `
610650
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
611651
612652
case \`uname\` in
613-
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
653+
*CYGWIN*|*MINGW*|*MSYS*)
654+
if command -v cygpath > /dev/null 2>&1; then
655+
basedir=\`cygpath -w "$basedir"\`
656+
fi
657+
;;
614658
esac
615659
616660
if [ -x "$basedir//usr/bin/sh" ]; then
@@ -669,7 +713,11 @@ exports[`no cmd file exe.shim 1`] = `
669713
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
670714
671715
case \`uname\` in
672-
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
716+
*CYGWIN*|*MINGW*|*MSYS*)
717+
if command -v cygpath > /dev/null 2>&1; then
718+
basedir=\`cygpath -w "$basedir"\`
719+
fi
720+
;;
673721
esac
674722
675723
"$basedir/src.exe" "$@"
@@ -702,7 +750,11 @@ exports[`no shebang exe.shim 1`] = `
702750
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
703751
704752
case \`uname\` in
705-
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
753+
*CYGWIN*|*MINGW*|*MSYS*)
754+
if command -v cygpath > /dev/null 2>&1; then
755+
basedir=\`cygpath -w "$basedir"\`
756+
fi
757+
;;
706758
esac
707759
708760
"$basedir/src.exe" "$@"
@@ -741,7 +793,11 @@ exports[`shebang with -S from.env.S.shim 1`] = `
741793
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
742794
743795
case \`uname\` in
744-
*CYGWIN*) basedir=\`cygpath -w "$basedir"\`;;
796+
*CYGWIN*|*MINGW*|*MSYS*)
797+
if command -v cygpath > /dev/null 2>&1; then
798+
basedir=\`cygpath -w "$basedir"\`
799+
fi
800+
;;
745801
esac
746802
747803
if [ -x "$basedir/node" ]; then

0 commit comments

Comments
 (0)