Skip to content

Commit

Permalink
Replaced Local Path Resolution
Browse files Browse the repository at this point in the history
This changes the path resolution to use Node's
module resolution algorithm to be consistent with
other usages of Node CLI tools.
  • Loading branch information
ObliviousHarmony committed May 26, 2023
1 parent b1baba3 commit c359fd4
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions packages/env/bin/wp-env
Original file line number Diff line number Diff line change
@@ -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 );

0 comments on commit c359fd4

Please sign in to comment.