Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preferentially Execute Local wp-env #50980

Merged
merged 7 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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