Skip to content

Commit

Permalink
Add a Sass executable wrapper that forwards to the correct exe
Browse files Browse the repository at this point in the history
This allows users to run `npx sass` when they've just installed
`sass-embedded` and directly run the Dart VM. Due to npm/cmd-shim#152,
there's no way to make this work on Windows CMD/Powershell without
substantially curtailing the performance on other operating systems.
  • Loading branch information
nex3 committed Jul 30, 2024
1 parent be8d077 commit 497ff37
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
62 changes: 62 additions & 0 deletions bin/sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash -e

dir="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd)"
if [ "$(basename "$dir")" == bin ]; then
# Global executables are installed in .../bin/, and global libraries
# (including their dependencies) in .../lib/node_modules.
node_modules="$(dirname "$dir")/lib/node_modules"
elif [ "$(basename "$dir")" == .bin ]; then
# Local dependency executables are installed in ./node_modules/.bin, and
# and their libraries in ./node_modules.
node_modules="$(dirname "$dir")"
else
# If all else fails, try to find node_modules in a parent directory. We
# might execute this, for example, if someone runs the script from source.
while [ "$dir" != "/" ]; do
dir="$(dirname "$dir")"
if [ -d "$dir/node_modules" ]; then
node_modules="$dir/node_modules"
break
fi
done
echo "Could not find node_modules above ${BASH_SOURCE[0]}!" >&2
exit 1
fi

case "$(uname -s)" in
Linux*)
if [ ! -z "$ANDROID_ROOT" -a ! -z "$ANDROID_DATA" ]; then
os=android
else
os=linux
fi;;
Darwin*)
os=darwin;;
CYGWIN*|MINGW*|MSYS_NT*)
os=win32;;
*)
echo "Unknown operating system $(uname -s)!" >&2
exit 1
esac

musl=
if [ "$os" == linux ] && grep -q /ld-musl- /bin/bash; then musl=-musl; fi

case "$(uname -m)" in
aarch64*) arch=arm64;;
arm*) arch=arm;;
x86_64) arch=x64;;
i386|i486|i586|i686) arch=ia32;;
*)
echo "Unknown architecture $(uname -m)!" >&2
exit 1
esac

shared="$node_modules/sass-embedded-$os$musl-$arch"
specific="$node_modules/sass-embedded/node_modules/sass-embedded-$os$musl-$arch"
if [ -d "$specific" ]; then
exec "$specific/dart-sass/sass" "$@"
else
exec "$shared/dart-sass/sass" "$@"
fi

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"engines": {
"node": ">=16.0.0"
},
"bin": {"sass": "bin/sass"},
"scripts": {
"init": "ts-node ./tool/init.ts",
"check": "npm-run-all check:gts check:tsc",
Expand Down

0 comments on commit 497ff37

Please sign in to comment.