diff --git a/packages/wp-now/src/execute-wp-cli.ts b/packages/wp-now/src/execute-wp-cli.ts index c5d8ec1f..7811e351 100644 --- a/packages/wp-now/src/execute-wp-cli.ts +++ b/packages/wp-now/src/execute-wp-cli.ts @@ -26,6 +26,7 @@ export async function executeWPCli(args: string[]) { try { const vfsWpCliPath = '/wp-cli/wp-cli.phar'; + php.mount('.', '/local'); // mount current dir to run "wp-now wp eval-file /local/foo.php" php.mount(dirname(getWpCliPath()), dirname(vfsWpCliPath)); await php.cli([ 'php', diff --git a/packages/wp-now/src/run-cli.ts b/packages/wp-now/src/run-cli.ts index df18d3c4..96d6a236 100644 --- a/packages/wp-now/src/run-cli.ts +++ b/packages/wp-now/src/run-cli.ts @@ -8,6 +8,7 @@ import { spawn, SpawnOptionsWithoutStdio } from 'child_process'; import { executePHP } from './execute-php'; import { output } from './output'; import { isGitHubCodespace } from './github-codespaces'; +import { executeWPCli } from './execute-wp-cli'; function startSpinner(message: string) { process.stdout.write(`${message}...\n`); @@ -152,6 +153,23 @@ export async function runCli() { } } ) + .command( + 'wp', + 'Run the wp command passing the arguments for wp cli', + (yargs) => { + return yargs.strict(false); + }, + async () => { + // 0: node, 1: wp-now, 2: wp, 3: [wp-cli options...] + const args = process.argv.slice(3); + try { + await executeWPCli(args); + process.exit(0); + } catch (error) { + process.exit(error.status || -1); + } + } + ) .demandCommand(1, 'You must provide a valid command') .help() .alias('h', 'help') diff --git a/packages/wp-now/src/tests/wp-cli-eval-file/total-posts.php b/packages/wp-now/src/tests/wp-cli-eval-file/total-posts.php new file mode 100644 index 00000000..a8af057e --- /dev/null +++ b/packages/wp-now/src/tests/wp-cli-eval-file/total-posts.php @@ -0,0 +1,10 @@ +publish; + return $published_posts; +} + +$published_post_count = count_all_published_posts(); +echo 'Total published posts: ' . $published_post_count; +?> diff --git a/packages/wp-now/src/tests/wp-now.spec.ts b/packages/wp-now/src/tests/wp-now.spec.ts index c29d9d79..3c50fa52 100644 --- a/packages/wp-now/src/tests/wp-now.spec.ts +++ b/packages/wp-now/src/tests/wp-now.spec.ts @@ -845,4 +845,16 @@ describe('wp-cli command', () => { await executeWPCli(['cli', 'version']); expect(output).toMatch(/WP-CLI (\d\.?)+/i); }); + + /** + * Test wp-cli eval-file works correctly. + * We will use the context of Playground mode. + */ + test('wp-cli eval-file works correctly', async () => { + await executeWPCli([ + 'eval-file', + '/local/packages/wp-now/src/tests/wp-cli-eval-file/total-posts.php', + ]); + expect(output).toMatch('Total published posts: 1'); + }); });