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 load .zip downloaded from wordpress-playground #137

Open
wants to merge 8 commits into
base: trunk
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# compiled output
/dist
/test
tmp
var
/out-tsc
Expand Down
1 change: 1 addition & 0 deletions packages/wp-now/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const enum WPNowMode {
WORDPRESS_DEVELOP = 'wordpress-develop',
INDEX = 'index',
WP_CONTENT = 'wp-content',
WP_PR = 'wp-pr',
PLAYGROUND = 'playground',
AUTO = 'auto',
}
Expand Down
25 changes: 25 additions & 0 deletions packages/wp-now/src/run-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SupportedPHPVersion } from '@php-wasm/universal';
import getWpNowConfig, { CliOptions } from './config';
import { spawn, SpawnOptionsWithoutStdio } from 'child_process';
import { executePHP } from './execute-php';
import { executeWPCli } from './execute-wp-cli';
import { output } from './output';
import { isGitHubCodespace } from './github-codespaces';

Expand Down Expand Up @@ -63,6 +64,7 @@ export async function runCli() {
describe: "WordPress version to use: e.g. '--wp=6.2'",
type: 'string',
});

yargs.option('port', {
describe: 'Server port',
type: 'number',
Expand Down Expand Up @@ -152,6 +154,29 @@ export async function runCli() {
}
}
)
.command(
'wp [..args]',
'run a wp-cli command',
(yargs) => {
commonParameters(yargs);
yargs.strict(false);
},
async (argv) => {
try {
// 0: node, 1: wp-now, 2: php, ...args
const args = process.argv.slice(3);
const phpArgs = args.includes('--')
? (argv._ as string[])
: args;
// 0: php, ...args
await executeWPCli(phpArgs);
process.exit(0);
} catch (error) {
console.error(error);
process.exit(error.status || -1);
}
}
)
.demandCommand(1, 'You must provide a valid command')
.help()
.alias('h', 'help')
Expand Down
46 changes: 46 additions & 0 deletions packages/wp-now/src/wp-now.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
isPluginDirectory,
isThemeDirectory,
isWpContentDirectory,
isWpPRDirectory,
isWordPressDirectory,
isWordPressDevelopDirectory,
getPluginFile,
Expand Down Expand Up @@ -95,6 +96,9 @@ export default async function startWPNow(
case WPNowMode.WP_CONTENT:
await runWpContentMode(_php, options);
break;
case WPNowMode.WP_PR:
await runWpPrMode(_php, options);
break;
case WPNowMode.WORDPRESS_DEVELOP:
await runWordPressDevelopMode(_php, options);
break;
Expand Down Expand Up @@ -175,6 +179,46 @@ async function runWpContentMode(
mountMuPlugins(php, documentRoot);
}

async function runWpPrMode(
php: NodePHP,
{
documentRoot,
wordPressVersion,
wpContentPath,
projectPath,
absoluteUrl,
}: WPNowOptions
) {
const wordPressPath = path.join(
getWordpressVersionsPath(),
wordPressVersion
);
php.mount(wordPressPath, documentRoot);
await initWordPress(php, wordPressVersion, documentRoot, absoluteUrl);
fs.ensureDirSync(wpContentPath);

php.mount(`${projectPath}/wp-content`, `${documentRoot}/wp-content`);

if (!fs.existsSync(`${projectPath}/wp-content/db.php`)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see why you put that in here. Normally mountSqlitePlugin() takes care of that, but it also overrides the database file – and in your case the database file is already there. I'd love mountSqlitePlugin() to respect any pre-existing databases so that runWpContentMode() could work with both a "regular" WordPress and a Playground export. What do you think @sejas @danielbachhuber ?

await fs.promises.copyFile(
`${getSqlitePath()}/db.copy`,
`${projectPath}/wp-content/db.php`
);
}
// need a dist folder for output
if (!fs.existsSync(`${projectPath}/dist`)) {
await fs.promises.mkdir('dist');
}
php.mount(`${projectPath}/dist`, `${documentRoot}/dist`);

mountSqliteDatabaseDirectory(
php,
documentRoot,
`${projectPath}/wp-content`
);
mountMuPlugins(php, documentRoot);
}

async function runWordPressDevelopMode(
php: NodePHP,
{ documentRoot, projectPath, absoluteUrl }: WPNowOptions
Expand Down Expand Up @@ -397,6 +441,8 @@ export function inferMode(
return WPNowMode.WORDPRESS;
} else if (isWpContentDirectory(projectPath)) {
return WPNowMode.WP_CONTENT;
} else if (isWpPRDirectory(projectPath)) {
return WPNowMode.WP_PR;
} else if (isPluginDirectory(projectPath)) {
return WPNowMode.PLUGIN;
} else if (isThemeDirectory(projectPath)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ export function isWpContentDirectory(projectPath: string): Boolean {
}
return false;
}

export function isWpPRDirectory(projectPath: string): Boolean {
const wpContentExists = fs.existsSync(path.join(projectPath, 'wp-content'));
const wpIncludeExists = fs.existsSync(path.join(projectPath, 'wp-include'));
if (wpContentExists && !wpIncludeExists) {
return true;
}
return false;
}
Loading