diff options
| author | Xandor Schiefer <me@xandor.co.za> | 2024-09-03 02:44:02 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-02 17:44:02 -0700 |
| commit | f1492683c10cbe4fd5c03e6a75fb0256bd739c2a (patch) | |
| tree | a36012c9f8ac8404aee635a86075a7e44f2f180e /scripts/formatters | |
| parent | 73617ab050b646f099ba7ccb74cbecf94329a67c (diff) | |
Fix `apheleia-npx` in Yarn PnP projects (#301)
* `apheleia-npx` would use an incorrect path for the Yarn PnP ESM
loader.
* `apheleia-npx` did not correctly guard against word splitting.
* `apheleia-npx` was sometimes not able to find formatters in a Yarn PnP
project if there was also a node_modules folder at the root of the
project. Unfortunately, many tools (including
[Prettier](https://github.com/prettier/prettier/issues/13032)) will
create a cache folder in `node_modules` even in Yarn PnP projects. The
presence of any `node_modules` folders are irrelevant when a `.pnp.cjs`
file is present.
---------
Co-authored-by: Radon Rosborough <radon@intuitiveexplanations.com>
Diffstat (limited to 'scripts/formatters')
| -rwxr-xr-x | scripts/formatters/apheleia-npx | 30 |
1 files changed, 16 insertions, 14 deletions
diff --git a/scripts/formatters/apheleia-npx b/scripts/formatters/apheleia-npx index 8f6a366..a540f9b 100755 --- a/scripts/formatters/apheleia-npx +++ b/scripts/formatters/apheleia-npx @@ -15,7 +15,7 @@ if (( "$#" == 0 )); then fi # location of this script -scripts_dir="$(cd $(dirname ${BASH_SOURCE[0]}) &>/dev/null && pwd)" +scripts_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" pnp_bin="${scripts_dir}/pnp-bin.js" # This function prints the name of the current directory if it @@ -36,30 +36,32 @@ find_upwards() { dir="$(find_upwards package.json)" if [[ -d $dir ]]; then - cd $dir + cd "$dir" - pnp_root=$(find_upwards '.pnp.cjs') - npm_root=$(find_upwards 'node_modules') + pnp_root="$(find_upwards .pnp.cjs)" + npm_root="$(find_upwards node_modules)" - if [[ -n ${pnp_root} && ${#pnp_root} -gt ${#npm_root} ]]; then + if [[ -n ${pnp_root} ]]; then # trying pnp pnp_path="${pnp_root}/.pnp.cjs" - bin="$(${pnp_bin} ${pnp_path} $1)" + bin="$(${pnp_bin} "${pnp_path}" "$1")" # note: $bin might not be on the real filesystem, # might be in a zip archive if [[ -n $bin ]]; then - if [[ -f "${pnp_path}/.pnp.loader.mjs" ]]; then - loader_opt="--loader ${pnp_path}/.pnp.loader.mjs" + node="$(which node)" + if [[ -f "${pnp_root}/.pnp.loader.mjs" ]]; then + exec ${node} --require "${pnp_path}" \ + --loader "${pnp_root}/.pnp.loader.mjs" "${bin}" "${@:2}" fi - node=$(which node) - command="${node} --require ${pnp_path} ${loader_opt} ${bin} ${@:2}" - exec ${command} + exec ${node} --require "${pnp_path}" "${bin}" "${@:2}" fi elif [[ -n ${npm_root} ]]; then # trying npm - node_modules_paths=(\ - $(node -e 'console.log(require.resolve.paths("").join("\n"))')) - for path in ${node_modules_paths[@]}; do + node_modules_paths=() + while IFS='' read -r line; do + node_modules_paths+=("$line") + done < <(node -e 'console.log(require.resolve.paths("").join("\n"))') + for path in "${node_modules_paths[@]}"; do if [[ -x "${path}/.bin/$1" ]]; then exec "${path}/.bin/$1" "${@:2}" fi |
