From ccef1f728712e1b791e87710f35509c9a3cc84bb Mon Sep 17 00:00:00 2001 From: Christopher Allford <6451942+ObliviousHarmony@users.noreply.github.com> Date: Thu, 25 May 2023 11:32:06 -0700 Subject: [PATCH 1/7] Preferentially Execute Local `wp-env` 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. --- packages/env/bin/wp-env | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/env/bin/wp-env b/packages/env/bin/wp-env index 7ce3e39103bcd1..396a509e751ff4 100755 --- a/packages/env/bin/wp-env +++ b/packages/env/bin/wp-env @@ -1,4 +1,25 @@ #!/usr/bin/env node 'use strict'; -const command = process.argv.slice( 2 ); -require( '../lib/cli' )().parse( command.length ? command : [ '--help' ] ); + +/** + * External dependencies. + */ +const path = require( 'path' ); +const fs = require( 'fs' ); + +// 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 = path.resolve( './node_modules/@wordpress/env/lib/cli' ); +const cliPath = fs.existsSync( localPath ) ? localPath : '../lib/cli'; +const cli = require( cliPath )(); + +// Now we can execute the CLI with the given command. +cli.parse( command ); From f44e63a2cce478418a7003482022bd62691fe0bd Mon Sep 17 00:00:00 2001 From: Christopher Allford <6451942+ObliviousHarmony@users.noreply.github.com> Date: Thu, 25 May 2023 11:35:54 -0700 Subject: [PATCH 2/7] Changelog --- packages/env/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/env/CHANGELOG.md b/packages/env/CHANGELOG.md index 54bbd44d2b4485..1c2b35b28da947 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 From a06252fd27e594fb7576654c005e8b3db5d9f4aa Mon Sep 17 00:00:00 2001 From: Christopher Allford <6451942+ObliviousHarmony@users.noreply.github.com> Date: Thu, 25 May 2023 11:54:18 -0700 Subject: [PATCH 3/7] Documentation Update --- packages/env/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/env/README.md b/packages/env/README.md index 0bc36092830330..30dc6719a49504 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. You can also execute it 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": { From 9dbf5e248368821e57c1ff5341f1a2ae418e9b57 Mon Sep 17 00:00:00 2001 From: Christopher Allford <6451942+ObliviousHarmony@users.noreply.github.com> Date: Thu, 25 May 2023 12:13:51 -0700 Subject: [PATCH 4/7] Fixed Version Loading --- packages/env/bin/wp-env | 4 ++-- packages/env/lib/cli.js | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/env/bin/wp-env b/packages/env/bin/wp-env index 396a509e751ff4..8cdac7a5157094 100755 --- a/packages/env/bin/wp-env +++ b/packages/env/bin/wp-env @@ -17,8 +17,8 @@ if ( ! command.length ) { // 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 = path.resolve( './node_modules/@wordpress/env/lib/cli' ); -const cliPath = fs.existsSync( localPath ) ? localPath : '../lib/cli'; +const localPath = path.resolve( './node_modules/@wordpress/env/lib/cli.js' ); +const cliPath = fs.existsSync( localPath ) ? localPath : '../lib/cli.js'; const cli = require( cliPath )(); // Now we can execute the CLI with the given command. diff --git a/packages/env/lib/cli.js b/packages/env/lib/cli.js index 72a5eec911e087..865c6e03260e8e 100644 --- a/packages/env/lib/cli.js +++ b/packages/env/lib/cli.js @@ -7,10 +7,12 @@ const ora = require( 'ora' ); const yargs = require( 'yargs' ); const terminalLink = require( 'terminal-link' ); const { execSync } = require( 'child_process' ); +const path = require( 'path' ); /** * Internal dependencies */ +const pkg = require( path.resolve( __dirname, '../package.json' ) ); const env = require( './env' ); const parseXdebugMode = require( './parse-xdebug-mode' ); const { @@ -110,6 +112,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( From 32890b1252c6bfe4f54e706b8f0f1091db96ede9 Mon Sep 17 00:00:00 2001 From: Christopher Allford <6451942+ObliviousHarmony@users.noreply.github.com> Date: Thu, 25 May 2023 12:57:47 -0700 Subject: [PATCH 5/7] Removed Unnecessary Path Resolution --- packages/env/lib/cli.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/env/lib/cli.js b/packages/env/lib/cli.js index 865c6e03260e8e..1788315b60b9db 100644 --- a/packages/env/lib/cli.js +++ b/packages/env/lib/cli.js @@ -7,12 +7,11 @@ const ora = require( 'ora' ); const yargs = require( 'yargs' ); const terminalLink = require( 'terminal-link' ); const { execSync } = require( 'child_process' ); -const path = require( 'path' ); /** * Internal dependencies */ -const pkg = require( path.resolve( __dirname, '../package.json' ) ); +const pkg = require( '../package.json' ); const env = require( './env' ); const parseXdebugMode = require( './parse-xdebug-mode' ); const { From b1baba312bfe717085f7ad173ba1706cafcd94a0 Mon Sep 17 00:00:00 2001 From: Christopher Allford <6451942+ObliviousHarmony@users.noreply.github.com> Date: Fri, 26 May 2023 11:24:56 -0700 Subject: [PATCH 6/7] Update packages/env/README.md Co-authored-by: Noah Allen --- packages/env/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/env/README.md b/packages/env/README.md index 30dc6719a49504..fb9e9751d9c666 100644 --- a/packages/env/README.md +++ b/packages/env/README.md @@ -44,7 +44,7 @@ If your project already has a package.json, it's also possible to use `wp-env` a $ npm i @wordpress/env --save-dev ``` -If you have also installed `wp-env` globally, running it will automatically execute the local, project-level package. You can also execute it 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 the global installation or `npx`, modify your `package.json` and add an extra command to npm `scripts` (https://docs.npmjs.com/misc/scripts): From c359fd4bfaa677d987bd83003909685a61937375 Mon Sep 17 00:00:00 2001 From: Christopher Allford <6451942+ObliviousHarmony@users.noreply.github.com> Date: Fri, 26 May 2023 12:35:42 -0700 Subject: [PATCH 7/7] Replaced Local Path Resolution This changes the path resolution to use Node's module resolution algorithm to be consistent with other usages of Node CLI tools. --- packages/env/bin/wp-env | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/packages/env/bin/wp-env b/packages/env/bin/wp-env index 8cdac7a5157094..a6c4784a3e7fb5 100755 --- a/packages/env/bin/wp-env +++ b/packages/env/bin/wp-env @@ -1,25 +1,20 @@ #!/usr/bin/env node 'use strict'; -/** - * External dependencies. - */ -const path = require( 'path' ); -const fs = require( 'fs' ); - // 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' ]; + 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 = path.resolve( './node_modules/@wordpress/env/lib/cli.js' ); -const cliPath = fs.existsSync( localPath ) ? localPath : '../lib/cli.js'; -const cli = require( cliPath )(); +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 );