diff --git a/packages/env/CHANGELOG.md b/packages/env/CHANGELOG.md index 54bbd44d2b448..1c2b35b28da94 100644 --- a/packages/env/CHANGELOG.md +++ b/packages/env/CHANGELOG.md @@ -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 diff --git a/packages/env/README.md b/packages/env/README.md index 0bc3609283033..fb9e9751d9c66 100644 --- a/packages/env/README.md +++ b/packages/env/README.md @@ -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": { diff --git a/packages/env/bin/wp-env b/packages/env/bin/wp-env index 7ce3e39103bcd..a6c4784a3e7fb 100755 --- a/packages/env/bin/wp-env +++ b/packages/env/bin/wp-env @@ -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 ); diff --git a/packages/env/lib/cli.js b/packages/env/lib/cli.js index 72a5eec911e08..1788315b60b9d 100644 --- a/packages/env/lib/cli.js +++ b/packages/env/lib/cli.js @@ -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 { @@ -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(