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

WP-NOW: Add wp-cli integration #212

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions packages/wp-now/src/execute-wp-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
18 changes: 18 additions & 0 deletions packages/wp-now/src/run-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down Expand Up @@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Can we replace the 3 with something that express better your comment?
Eg:

const args = {
    node: 0,
    wpNow: 1,
    wp: 2,
    wpCliOptions: 3
} 

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't yargs provide you a list of everything that comes after the command?

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')
Expand Down
10 changes: 10 additions & 0 deletions packages/wp-now/src/tests/wp-cli-eval-file/total-posts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
function count_all_published_posts() {
$count_posts = wp_count_posts();
$published_posts = $count_posts->publish;
return $published_posts;
}

$published_post_count = count_all_published_posts();
echo 'Total published posts: ' . $published_post_count;
?>
12 changes: 12 additions & 0 deletions packages/wp-now/src/tests/wp-now.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL about that command, how lovely!

await executeWPCli([
'eval-file',
'/local/packages/wp-now/src/tests/wp-cli-eval-file/total-posts.php',
]);
expect(output).toMatch('Total published posts: 1');
});
});
Loading