Skip to content

Commit 7bea6ec

Browse files
authored
Add the launch browser flag to CLI (#18)
## Motivation for the change, related issues Fixes Automattic/wordpress-playground-private#12 Integrate CLI with `wp-now` features. This PR adds a `--launch-browser` flag to CLI, default false. ## Implementation details It backport the `openInDefaultBrowser` function found in wp-now to CLI `startServer`. ## Testing Instructions (or ideally a Blueprint) Run the script as `bun packages/playground/cli/src/cli.ts server --launch-browser` or similar. The flag should start the default browser.
1 parent 8542e79 commit 7bea6ec

File tree

2 files changed

+77
-15
lines changed

2 files changed

+77
-15
lines changed

packages/playground/cli/src/cli.ts

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import fs from 'fs';
2-
import path from 'path';
3-
import yargs from 'yargs';
4-
import readline from 'readline';
5-
import { startServer } from './server';
1+
import { errorLogPath, logger } from '@php-wasm/logger';
2+
import { createNodeFsMountHandler, loadNodeRuntime } from '@php-wasm/node';
3+
import { EmscriptenDownloadMonitor, ProgressTracker } from '@php-wasm/progress';
64
import {
75
PHP,
86
PHPRequest,
@@ -11,25 +9,31 @@ import {
119
SupportedPHPVersion,
1210
SupportedPHPVersions,
1311
} from '@php-wasm/universal';
14-
import { logger, errorLogPath } from '@php-wasm/logger';
1512
import {
1613
Blueprint,
1714
compileBlueprint,
1815
runBlueprintSteps,
1916
} from '@wp-playground/blueprints';
20-
import { isValidWordPressSlug } from './is-valid-wordpress-slug';
21-
import { EmscriptenDownloadMonitor, ProgressTracker } from '@php-wasm/progress';
22-
import { createNodeFsMountHandler, loadNodeRuntime } from '@php-wasm/node';
2317
import { RecommendedPHPVersion, zipDirectory } from '@wp-playground/common';
24-
import { bootWordPress } from '@wp-playground/wordpress';
18+
import {
19+
bootWordPress,
20+
resolveWordPressRelease,
21+
} from '@wp-playground/wordpress';
22+
import { spawn, SpawnOptionsWithoutStdio } from 'child_process';
23+
import fs from 'fs';
24+
import path from 'path';
25+
import readline from 'readline';
2526
import { rootCertificates } from 'tls';
27+
import yargs from 'yargs';
2628
import {
2729
CACHE_FOLDER,
2830
cachedDownload,
2931
fetchSqliteIntegration,
3032
readAsFile,
3133
} from './download';
32-
import { resolveWordPressRelease } from '@wp-playground/wordpress';
34+
import { isGitHubCodespace } from './github-codespaces';
35+
import { isValidWordPressSlug } from './is-valid-wordpress-slug';
36+
import { startServer } from './server';
3337
export interface Mount {
3438
hostPath: string;
3539
vfsPath: string;
@@ -40,7 +44,7 @@ async function run() {
4044
* @TODO This looks similar to Query API args https://wordpress.github.io/wordpress-playground/developers/apis/query-api/
4145
* Perhaps the two could be handled by the same code?
4246
*/
43-
const yargsObject = await yargs(process.argv.slice(2))
47+
const yargsObject = yargs(process.argv.slice(2))
4448
.usage('Usage: wp-playground <command> [options]')
4549
.positional('command', {
4650
describe: 'Command to run',
@@ -76,7 +80,7 @@ async function run() {
7680
type: 'array',
7781
string: true,
7882
})
79-
.option('mountBeforeInstall', {
83+
.option('mount-before-install', {
8084
describe:
8185
'Mount a directory to the PHP runtime before installing WordPress. You can provide --mount-before-install multiple times. Format: /host/path:/vfs/path',
8286
type: 'array',
@@ -91,12 +95,16 @@ async function run() {
9195
describe: 'Blueprint to execute.',
9296
type: 'string',
9397
})
94-
.option('skipWordPressSetup', {
98+
.option('skip-wordpress-setup', {
9599
describe:
96100
'Do not download, unzip, and install WordPress. Useful for mounting a pre-configured WordPress directory at /wordpress.',
97101
type: 'boolean',
98102
default: false,
99103
})
104+
.deprecateOption(
105+
'skipWordPressSetup',
106+
'Use --skip-wordpress-setup instead.'
107+
)
100108
.option('quiet', {
101109
describe: 'Do not output logs and progress messages.',
102110
type: 'boolean',
@@ -108,6 +116,11 @@ async function run() {
108116
type: 'boolean',
109117
default: false,
110118
})
119+
.option('launch-browser', {
120+
describe: 'Launch the default browser after starting the server.',
121+
type: 'boolean',
122+
default: false,
123+
})
111124
.showHelpOnFail(false)
112125
.check((args) => {
113126
if (args.wp !== undefined && !isValidWordPressSlug(args.wp)) {
@@ -244,6 +257,42 @@ async function run() {
244257
});
245258
}
246259

260+
/**
261+
* Open the default browser at the end of the process.
262+
*
263+
* If the current environment is a GitHub Codespace, the browser is not opened.
264+
*
265+
* @param url
266+
*/
267+
function openInDefaultBrowser(url: string): void {
268+
if (isGitHubCodespace()) {
269+
return;
270+
}
271+
272+
let cmd: string, args: string[] | SpawnOptionsWithoutStdio;
273+
switch (process.platform) {
274+
case 'darwin':
275+
cmd = 'open';
276+
args = [url];
277+
break;
278+
case 'linux':
279+
cmd = 'xdg-open';
280+
args = [url];
281+
break;
282+
case 'win32':
283+
cmd = 'cmd';
284+
args = ['/c', `start ${url}`];
285+
break;
286+
default:
287+
logger.log(`Platform '${process.platform}' not supported`);
288+
return;
289+
}
290+
291+
spawn(cmd, args).on('error', function (err: any) {
292+
logger.error(err.message);
293+
});
294+
}
295+
247296
const command = args._[0] as string;
248297
if (!['run-blueprint', 'server', 'build-snapshot'].includes(command)) {
249298
yargsObject.showHelp();
@@ -265,7 +314,7 @@ async function run() {
265314
logger.log(`Setting up WordPress ${args.wp}`);
266315
let wpDetails: any = undefined;
267316
const monitor = new EmscriptenDownloadMonitor();
268-
if (!args.skipWordPressSetup) {
317+
if (!args.skipWordpressSetup && !args['skipWordPressSetup']) {
269318
// @TODO: Rename to FetchProgressMonitor. There's nothing Emscripten
270319
// about that class anymore.
271320
monitor.addEventListener('progress', ((
@@ -386,6 +435,10 @@ async function run() {
386435
process.exit(0);
387436
} else {
388437
logger.log(`WordPress is running on ${absoluteUrl}`);
438+
439+
if (args.launchBrowser) {
440+
openInDefaultBrowser(absoluteUrl);
441+
}
389442
}
390443
} catch (error) {
391444
if (!args.debug) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* True if the current environment is a GitHub Codespace.
3+
*/
4+
export function isGitHubCodespace() {
5+
return Boolean(
6+
process.env['CODESPACE_NAME'] &&
7+
process.env['GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN']
8+
);
9+
}

0 commit comments

Comments
 (0)