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 support for custom URL's #78

Closed
Closed
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
17 changes: 17 additions & 0 deletions packages/wp-now/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface CliOptions {
path?: string;
wp?: string;
port?: number;
url?: string;
customport?: number;
}

export const enum WPNowMode {
Expand All @@ -41,6 +43,8 @@ export interface WPNowOptions {
wpContentPath?: string;
wordPressVersion?: string;
numberOfPhpInstances?: number;
customSiteURL?: string;
customPort?: number;
}

export const DEFAULT_OPTIONS: WPNowOptions = {
Expand All @@ -50,6 +54,8 @@ export const DEFAULT_OPTIONS: WPNowOptions = {
projectPath: process.cwd(),
mode: WPNowMode.AUTO,
numberOfPhpInstances: 1,
customSiteURL: null,
customPort: null,
};

export interface WPEnvOptions {
Expand Down Expand Up @@ -96,6 +102,8 @@ export default async function getWpNowConfig(
projectPath: args.path as string,
wordPressVersion: args.wp as string,
port,
customSiteURL: args.url as string,
customPort: args.customport as number,
};

const options: WPNowOptions = {} as WPNowOptions;
Expand All @@ -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"'
Expand Down
12 changes: 12 additions & 0 deletions packages/wp-now/src/run-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ function commonParameters(yargs) {
.option('php', {
describe: 'PHP version to use.',
type: 'string',
})
.option('url', {
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 tunneling service (e.g. ngrok), usually 80.",
type: 'number',
});
}

Expand Down Expand Up @@ -76,6 +86,8 @@ export async function runCli() {
php: argv.php as SupportedPHPVersion,
wp: argv.wp as string,
port: argv.port as number,
url: argv.url as string,
customport: argv.customport as number,
});
portFinder.setPort(options.port as number);
const { url } = await startServer(options);
Expand Down
8 changes: 6 additions & 2 deletions packages/wp-now/src/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
44 changes: 37 additions & 7 deletions packages/wp-now/src/wp-now.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,20 @@ async function runWpContentMode(
wpContentPath,
projectPath,
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);

php.mount(projectPath, `${documentRoot}/wp-content`);
Expand All @@ -173,26 +179,33 @@ 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);

const { initializeDefaultDatabase } = await initWordPress(
php,
'user-provided',
documentRoot,
absoluteUrl
customSiteURL || absoluteUrl
);

if (
Expand All @@ -214,6 +227,7 @@ async function runPluginOrThemeMode(
projectPath,
wpContentPath,
absoluteUrl,
customSiteURL,
mode,
}: WPNowOptions
) {
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down