Skip to content

Commit 6a16313

Browse files
Preferentially Execute Local wp-env (#50980)
Instead of always running the chosen version of `wp-env` we are going to try and find a local version in the current directory. This prevents the global `wp-env` from being used when a different local version is expected.
1 parent b3ea2b6 commit 6a16313

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

packages/env/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### New feature
6+
7+
- Execute the local package's `wp-env` instead of the globally installed version if one is available.
8+
59
## 8.0.0 (2023-05-24)
610

711
### Breaking Change

packages/env/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ If your project already has a package.json, it's also possible to use `wp-env` a
4444
$ npm i @wordpress/env --save-dev
4545
```
4646

47-
At this point, you can use the local, project-level version of wp-env via [`npx`](https://www.npmjs.com/package/npx), a utility automatically installed with `npm`.`npx` finds binaries like wp-env installed through node modules. As an example: `npx wp-env start --update`.
47+
If you have also installed `wp-env` globally, running it will automatically execute the local, project-level package. Alternatively, you can execute `wp-env` via [`npx`](https://www.npmjs.com/package/npx), a utility automatically installed with `npm`.`npx` finds binaries like `wp-env` installed through node modules. As an example: `npx wp-env start --update`.
4848

49-
If you don't wish to use `npx`, modify your package.json and add an extra command to npm `scripts` (https://docs.npmjs.com/misc/scripts):
49+
If you don't wish to use the global installation or `npx`, modify your `package.json` and add an extra command to npm `scripts` (https://docs.npmjs.com/misc/scripts):
5050

5151
```json
5252
"scripts": {

packages/env/bin/wp-env

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
#!/usr/bin/env node
22
'use strict';
3-
const command = process.argv.slice( 2 );
4-
require( '../lib/cli' )().parse( command.length ? command : [ '--help' ] );
3+
4+
// Remove 'node' and the name of the script from the arguments.
5+
let command = process.argv.slice( 2 );
6+
// Default to help text when they aren't running any commands.
7+
if ( ! command.length ) {
8+
command = [ '--help' ];
9+
}
10+
11+
// Rather than just executing the current CLI we will attempt to find a local version
12+
// and execute that one instead. This prevents users from accidentally using the
13+
// global CLI when a potentially different local version is expected.
14+
const localPath = require.resolve( '@wordpress/env/lib/cli.js', {
15+
paths: [ process.cwd(), __dirname ],
16+
} );
17+
const cli = require( localPath )();
18+
19+
// Now we can execute the CLI with the given command.
20+
cli.parse( command );

packages/env/lib/cli.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const { execSync } = require( 'child_process' );
1111
/**
1212
* Internal dependencies
1313
*/
14+
const pkg = require( '../package.json' );
1415
const env = require( './env' );
1516
const parseXdebugMode = require( './parse-xdebug-mode' );
1617
const {
@@ -110,6 +111,10 @@ module.exports = function cli() {
110111
'populate--': true,
111112
} );
112113

114+
// Since we might be running a different CLI version than the one that was called
115+
// we need to set the version manually from the correct package.json.
116+
yargs.version( pkg.version );
117+
113118
yargs.command(
114119
'start',
115120
wpGreen(

0 commit comments

Comments
 (0)