-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #224.
- Loading branch information
Showing
5 changed files
with
83 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* A fast `npx` alternative. | ||
*/ | ||
|
||
const path = require('node:path'); | ||
const getPathKey = require('path-key'); | ||
|
||
/** | ||
* The key for `process.env` containing the path, generally `PATH`. | ||
* | ||
* @type {string} | ||
*/ | ||
const pathKey = getPathKey(); | ||
|
||
/** | ||
* Prepends `node_modules/.bin` up to the root directory to the PATH. | ||
* | ||
* @remarks | ||
* When `--elm-format-path` was _not_ provided, we used to execute | ||
* `elm-format` like this: | ||
* | ||
* 1. Try `npx elm-format` | ||
* 2. Try `elm-format` | ||
* | ||
* Just starting `npx` takes 200 ms though. Luckily, `npx` isn’t even | ||
* necessary, because the common ways of running `elm-review` are: | ||
* | ||
* 1. Install everything globally and run just `elm-review`. | ||
* 2. Install everything locally and run `npx elm-review`. | ||
* 3. Use the `--elm-format-path`. | ||
* | ||
* However, `npx` adds all potential `node_modules/.bin` up to current directory to the | ||
* beginning of PATH, for example: | ||
* | ||
* ```sh | ||
* $ npx node -p 'process.env.PATH.split(require("path").delimiter)' | ||
* [ | ||
* '/Users/you/stuff/node_modules/.bin', | ||
* '/Users/you/node_modules/.bin', | ||
* '/Users/node_modules/.bin', | ||
* '/node_modules/.bin', | ||
* '/usr/bin', | ||
* 'etc' | ||
* ] | ||
* ``` | ||
* | ||
* So if a user runs `npx elm-review`, when we later try to spawn just | ||
* `elm-format`, it’ll be found since when spawning we inherit the same PATH. | ||
* | ||
* The `npx elm-format` approach has been removed to avoid those unnecessary 200 ms, | ||
* but to stay backwards compatible we prepend the same paths to the beginning | ||
* of PATH just like `npx` would (see above). This is needed when: | ||
* | ||
* - Executing `elm-review` _without_ `npx`/`npm exec`, npm scripts, or `--elm-format-path`. | ||
* - And expecting a _local_ `elm-format` to be used. | ||
* That’s an odd use case, but was supported due to the `npx` approach. | ||
* | ||
* This can be removed in a major version. | ||
*/ | ||
function backwardsCompatiblePath() { | ||
return [ | ||
...process | ||
.cwd() | ||
.split(path.sep) | ||
.map((_, index, parts) => | ||
[...parts.slice(0, index + 1), 'node_modules', '.bin'].join(path.sep) | ||
) | ||
.reverse(), | ||
process.env[pathKey] | ||
].join(path.delimiter); | ||
} | ||
|
||
module.exports = { | ||
backwardsCompatiblePath, | ||
pathKey | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters