Skip to content

Commit

Permalink
Preferentially Execute Local wp-env (#50980)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ObliviousHarmony authored May 26, 2023
1 parent b3ea2b6 commit 6a16313
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/env/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### New feature

- Execute the local package's `wp-env` instead of the globally installed version if one is available.

## 8.0.0 (2023-05-24)

### Breaking Change
Expand Down
4 changes: 2 additions & 2 deletions packages/env/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ If your project already has a package.json, it's also possible to use `wp-env` a
$ npm i @wordpress/env --save-dev
```

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`.
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`.

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):
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):

```json
"scripts": {
Expand Down
20 changes: 18 additions & 2 deletions packages/env/bin/wp-env
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
#!/usr/bin/env node
'use strict';
const command = process.argv.slice( 2 );
require( '../lib/cli' )().parse( command.length ? command : [ '--help' ] );

// Remove 'node' and the name of the script from the arguments.
let command = process.argv.slice( 2 );
// Default to help text when they aren't running any commands.
if ( ! command.length ) {
command = [ '--help' ];
}

// Rather than just executing the current CLI we will attempt to find a local version
// and execute that one instead. This prevents users from accidentally using the
// global CLI when a potentially different local version is expected.
const localPath = require.resolve( '@wordpress/env/lib/cli.js', {
paths: [ process.cwd(), __dirname ],
} );
const cli = require( localPath )();

// Now we can execute the CLI with the given command.
cli.parse( command );
5 changes: 5 additions & 0 deletions packages/env/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { execSync } = require( 'child_process' );
/**
* Internal dependencies
*/
const pkg = require( '../package.json' );
const env = require( './env' );
const parseXdebugMode = require( './parse-xdebug-mode' );
const {
Expand Down Expand Up @@ -110,6 +111,10 @@ module.exports = function cli() {
'populate--': true,
} );

// Since we might be running a different CLI version than the one that was called
// we need to set the version manually from the correct package.json.
yargs.version( pkg.version );

yargs.command(
'start',
wpGreen(
Expand Down

1 comment on commit 6a16313

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 6a16313.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5094989157
📝 Reported issues:

Please sign in to comment.