From 516b91f4475237eee80e0d3a6e57c8ee61b40261 Mon Sep 17 00:00:00 2001 From: duncup Date: Thu, 27 May 2021 06:29:38 +0000 Subject: [PATCH] Add support for top-level await (#9) --- package.json | 4 +++- run-node.sh | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 run-node.sh diff --git a/package.json b/package.json index e8f0225..3ac8ec9 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,9 @@ "email": "sam.verschueren@gmail.com", "url": "github.com/SamVerschueren" }, + "bin": { + "run-node": "run-node.sh" + }, "maintainers": [ { "name": "Sindre Sorhus", @@ -45,7 +48,6 @@ "execa": "^4.0.0", "find-up": "^4.1.0", "plist": "^3.0.1", - "run-node": "^1.0.0", "tempfile": "^3.0.0" }, "devDependencies": { diff --git a/run-node.sh b/run-node.sh new file mode 100644 index 0000000..e8d274e --- /dev/null +++ b/run-node.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +if [[ -z "$alfred_workflow_cache" ]]; then + echo "This script must be called from Alfred, \$alfred_workflow_cache is missing. Make sure a Bundle ID is set." + exit 1 +fi + +if [[ ! -d "$alfred_workflow_cache" ]]; then + mkdir -p "$alfred_workflow_cache" +fi + +PATH_CACHE="$alfred_workflow_cache"/node_path + +get_user_path() { + eval $(/usr/libexec/path_helper -s) + echo "$($SHELL -i -l -c 'echo -e "\n"PATH=\"$PATH:\$PATH\""\n"' 2>/dev/null | grep "^PATH=")" > "$PATH_CACHE" +} + +set_path() { + if [[ -f "$PATH_CACHE" ]]; then + . "$PATH_CACHE" + else + get_user_path + . "$PATH_CACHE" + fi + + export PATH +} + +has_node() { + command -v node >/dev/null 2>&1 +} + +# Check if we have Node.js, otherwise inherit path from user shell +if ! has_node; then + set_path + + # Retry by deleting old path cache + if ! has_node; then + rm "$PATH_CACHE" + set_path + fi +fi + +if has_node; then + ESM_OPTIONS='{"await":true}' node --require esm "$@" +else + echo $'{"items":[{"title": "Couldn\'t find the `node` binary", "subtitle": "Symlink it to `/usr/local/bin`"}]}' +fi