From 31adc951f6947a6268416a494677f46fd7a1e207 Mon Sep 17 00:00:00 2001 From: Sergey Mitroshin Date: Tue, 13 Jun 2023 21:12:34 -0400 Subject: [PATCH 1/3] wp-now: Add support for custom URL's. --- packages/wp-now/src/config.ts | 17 +++++++++++ packages/wp-now/src/run-cli.ts | 12 ++++++++ packages/wp-now/src/start-server.ts | 8 ++++-- packages/wp-now/src/wp-now.ts | 44 ++++++++++++++++++++++++----- 4 files changed, 72 insertions(+), 9 deletions(-) diff --git a/packages/wp-now/src/config.ts b/packages/wp-now/src/config.ts index 52b8af60..c941a6a1 100644 --- a/packages/wp-now/src/config.ts +++ b/packages/wp-now/src/config.ts @@ -18,6 +18,8 @@ export interface CliOptions { path?: string; wp?: string; port?: number; + siteurl?: string; + customport?: number; } export const enum WPNowMode { @@ -41,6 +43,8 @@ export interface WPNowOptions { wpContentPath?: string; wordPressVersion?: string; numberOfPhpInstances?: number; + customSiteURL?: string; + customPort?: number; } export const DEFAULT_OPTIONS: WPNowOptions = { @@ -50,6 +54,8 @@ export const DEFAULT_OPTIONS: WPNowOptions = { projectPath: process.cwd(), mode: WPNowMode.AUTO, numberOfPhpInstances: 1, + customSiteURL: null, + customPort: null, }; export interface WPEnvOptions { @@ -96,6 +102,8 @@ export default async function getWpNowConfig( projectPath: args.path as string, wordPressVersion: args.wp as string, port, + customSiteURL: args.siteurl as string, + customPort: args.customport as number, }; const options: WPNowOptions = {} as WPNowOptions; @@ -120,6 +128,15 @@ export default async function getWpNowConfig( if (!options.absoluteUrl) { options.absoluteUrl = await getAbsoluteURL(); } + + if (options.customSiteURL) { + const customPort = + options.customPort || (await portFinder.getOpenPort()); + if (customPort && customPort !== 80) { + options.customSiteURL += `:${customPort}`; + } + } + if (!isValidWordPressVersion(options.wordPressVersion)) { throw new Error( 'Unrecognized WordPress version. Please use "latest" or numeric versions such as "6.2", "6.0.1", "6.2-beta1", or "6.2-RC1"' diff --git a/packages/wp-now/src/run-cli.ts b/packages/wp-now/src/run-cli.ts index 72429c1f..eb365ce9 100644 --- a/packages/wp-now/src/run-cli.ts +++ b/packages/wp-now/src/run-cli.ts @@ -31,6 +31,16 @@ function commonParameters(yargs) { .option('php', { describe: 'PHP version to use.', type: 'string', + }) + .option('siteurl', { + describe: + "Custom site URL to access the site ('home' and 'siteurl' option values).", + type: 'string', + }) + .option('customport', { + describe: + "Custom port number. Needed if you're using a reverse proxy (e.g. ngrok), usually 80.", + type: 'number', }); } @@ -76,6 +86,8 @@ export async function runCli() { php: argv.php as SupportedPHPVersion, wp: argv.wp as string, port: argv.port as number, + siteurl: argv.siteurl as string, + customport: argv.customport as number, }); portFinder.setPort(options.port as number); const { url } = await startServer(options); diff --git a/packages/wp-now/src/start-server.ts b/packages/wp-now/src/start-server.ts index 54f45c53..e195d899 100644 --- a/packages/wp-now/src/start-server.ts +++ b/packages/wp-now/src/start-server.ts @@ -103,11 +103,15 @@ export async function startServer( output?.trace(e); } }); - const url = options.absoluteUrl; + const internalUrl = options.absoluteUrl; + const customSiteUrl = options.customSiteURL; app.listen(port, () => { - output?.log(`Server running at ${url}`); + output?.log(`Server running at ${internalUrl}`); + customSiteUrl && output?.log(`Custom site URL: ${customSiteUrl}`); }); + const url = customSiteUrl || internalUrl; + return { url, php, diff --git a/packages/wp-now/src/wp-now.ts b/packages/wp-now/src/wp-now.ts index 4f628747..224fa249 100644 --- a/packages/wp-now/src/wp-now.ts +++ b/packages/wp-now/src/wp-now.ts @@ -154,6 +154,7 @@ async function runWpContentMode( wpContentPath, projectPath, absoluteUrl, + customSiteURL, }: WPNowOptions ) { const wordPressPath = path.join( @@ -161,7 +162,12 @@ async function runWpContentMode( wordPressVersion ); php.mount(wordPressPath, documentRoot); - await initWordPress(php, wordPressVersion, documentRoot, absoluteUrl); + await initWordPress( + php, + wordPressVersion, + documentRoot, + customSiteURL || absoluteUrl + ); fs.ensureDirSync(wpContentPath); php.mount(projectPath, `${documentRoot}/wp-content`); @@ -173,18 +179,25 @@ async function runWpContentMode( async function runWordPressDevelopMode( php: NodePHP, - { documentRoot, projectPath, absoluteUrl }: WPNowOptions + { documentRoot, projectPath, absoluteUrl, customSiteURL }: WPNowOptions ) { await runWordPressMode(php, { documentRoot, projectPath: projectPath + '/build', absoluteUrl, + customSiteURL, }); } async function runWordPressMode( php: NodePHP, - { documentRoot, wpContentPath, projectPath, absoluteUrl }: WPNowOptions + { + documentRoot, + wpContentPath, + projectPath, + absoluteUrl, + customSiteURL, + }: WPNowOptions ) { php.mount(projectPath, documentRoot); @@ -192,7 +205,7 @@ async function runWordPressMode( php, 'user-provided', documentRoot, - absoluteUrl + customSiteURL || absoluteUrl ); if ( @@ -214,6 +227,7 @@ async function runPluginOrThemeMode( projectPath, wpContentPath, absoluteUrl, + customSiteURL, mode, }: WPNowOptions ) { @@ -222,7 +236,12 @@ async function runPluginOrThemeMode( wordPressVersion ); php.mount(wordPressPath, documentRoot); - await initWordPress(php, wordPressVersion, documentRoot, absoluteUrl); + await initWordPress( + php, + wordPressVersion, + documentRoot, + customSiteURL || absoluteUrl + ); fs.ensureDirSync(wpContentPath); fs.copySync( @@ -260,14 +279,25 @@ async function runPluginOrThemeMode( async function runWpPlaygroundMode( php: NodePHP, - { documentRoot, wordPressVersion, wpContentPath, absoluteUrl }: WPNowOptions + { + documentRoot, + wordPressVersion, + wpContentPath, + absoluteUrl, + customSiteURL, + }: WPNowOptions ) { const wordPressPath = path.join( getWordpressVersionsPath(), wordPressVersion ); php.mount(wordPressPath, documentRoot); - await initWordPress(php, wordPressVersion, documentRoot, absoluteUrl); + await initWordPress( + php, + wordPressVersion, + documentRoot, + customSiteURL || absoluteUrl + ); fs.ensureDirSync(wpContentPath); fs.copySync( From 234be2d6650404664764e22f42f369f31505d2b9 Mon Sep 17 00:00:00 2001 From: Sergey Mitroshin Date: Tue, 13 Jun 2023 21:15:57 -0400 Subject: [PATCH 2/3] Clarify argument description. --- packages/wp-now/src/run-cli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/wp-now/src/run-cli.ts b/packages/wp-now/src/run-cli.ts index eb365ce9..cd01e171 100644 --- a/packages/wp-now/src/run-cli.ts +++ b/packages/wp-now/src/run-cli.ts @@ -39,7 +39,7 @@ function commonParameters(yargs) { }) .option('customport', { describe: - "Custom port number. Needed if you're using a reverse proxy (e.g. ngrok), usually 80.", + "Custom port number. Needed if you're using a tunneling service (e.g. ngrok), usually 80.", type: 'number', }); } From 97c19288121b1ace9e980978c256ec895a0c2ceb Mon Sep 17 00:00:00 2001 From: Sergey Mitroshin Date: Wed, 14 Jun 2023 13:00:58 -0400 Subject: [PATCH 3/3] Rename `siteurl` option to `url`. --- packages/wp-now/src/config.ts | 4 ++-- packages/wp-now/src/run-cli.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/wp-now/src/config.ts b/packages/wp-now/src/config.ts index c941a6a1..52fde4e4 100644 --- a/packages/wp-now/src/config.ts +++ b/packages/wp-now/src/config.ts @@ -18,7 +18,7 @@ export interface CliOptions { path?: string; wp?: string; port?: number; - siteurl?: string; + url?: string; customport?: number; } @@ -102,7 +102,7 @@ export default async function getWpNowConfig( projectPath: args.path as string, wordPressVersion: args.wp as string, port, - customSiteURL: args.siteurl as string, + customSiteURL: args.url as string, customPort: args.customport as number, }; diff --git a/packages/wp-now/src/run-cli.ts b/packages/wp-now/src/run-cli.ts index cd01e171..3c30feef 100644 --- a/packages/wp-now/src/run-cli.ts +++ b/packages/wp-now/src/run-cli.ts @@ -32,7 +32,7 @@ function commonParameters(yargs) { describe: 'PHP version to use.', type: 'string', }) - .option('siteurl', { + .option('url', { describe: "Custom site URL to access the site ('home' and 'siteurl' option values).", type: 'string', @@ -86,7 +86,7 @@ export async function runCli() { php: argv.php as SupportedPHPVersion, wp: argv.wp as string, port: argv.port as number, - siteurl: argv.siteurl as string, + url: argv.url as string, customport: argv.customport as number, }); portFinder.setPort(options.port as number);