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

CLI: Source WordPress zip from a local cache file before looking for it online #1523

Open
wants to merge 2 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
26 changes: 5 additions & 21 deletions packages/playground/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ import { createNodeFsMountHandler, loadNodeRuntime } from '@php-wasm/node';
import { RecommendedPHPVersion, zipDirectory } from '@wp-playground/common';
import { bootWordPress } from '@wp-playground/wordpress';
import { rootCertificates } from 'tls';
import {
CACHE_FOLDER,
fetchSqliteIntegration,
fetchWordPress,
readAsFile,
resolveWPRelease,
} from './download';
import { resolveWordPressZip, fetchSqliteIntegration } from './download';

export interface Mount {
hostPath: string;
Expand Down Expand Up @@ -272,24 +266,14 @@ async function run() {
}
}) as any);

wpDetails = await resolveWPRelease(args.wp);
wpDetails = await resolveWordPressZip(args.wp, monitor);
}

const preinstalledWpContentPath = path.join(
CACHE_FOLDER,
`prebuilt-wp-content-for-wp-${wpDetails.version}.zip`
);
const wordPressZip = !wpDetails
? undefined
: fs.existsSync(preinstalledWpContentPath)
? readAsFile(preinstalledWpContentPath)
: fetchWordPress(wpDetails.url, monitor);

requestHandler = await bootWordPress({
siteUrl: absoluteUrl,
createPhpRuntime: async () =>
await loadNodeRuntime(compiledBlueprint.versions.php),
wordPressZip,
wordPressZip: wpDetails.zipFile,
sqliteIntegrationPluginZip: fetchSqliteIntegration(monitor),
sapiName: 'cli',
createFiles: {
Expand All @@ -313,8 +297,8 @@ async function run() {
const php = await requestHandler.getPrimaryPhp();
if (wpDetails && !args.mountBeforeInstall) {
fs.writeFileSync(
preinstalledWpContentPath,
await zipDirectory(php, '/wordpress')
wpDetails.localZipPath,
await zipDirectory(php, requestHandler.documentRoot)
);
}

Expand Down
78 changes: 78 additions & 0 deletions packages/playground/cli/src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EmscriptenDownloadMonitor } from '@php-wasm/progress';
import { createHash } from 'crypto';
import fs from 'fs-extra';
import os from 'os';
import { globSync } from 'glob';
import path, { basename } from 'path';

export const CACHE_FOLDER = path.join(os.homedir(), '.wordpress-playground');
Expand Down Expand Up @@ -84,6 +85,83 @@ export function readAsFile(path: string, fileName?: string): File {
return new File([fs.readFileSync(path)], fileName ?? basename(path));
}

/**
* Offline-first WordPress zip resolution.
* It will first look for a pre-installed WordPress zip file in the local cache,
* and will only do a network lookup if it doesn't find one.
*
* @param preferredVersion
* @param monitor
* @returns
*/
export async function resolveWordPressZip(
preferredVersion: string,
monitor: EmscriptenDownloadMonitor
): Promise<{ zipFile: File | Promise<File>; localZipPath: string }> {
const filenamePrefix = `prebuilt-wp-`;
const filenameSuffix = `.zip`;
let wpDetails;

// First, let's grab a pre-installed WordPress zip file we may have
// already prepared earlier.
const localVersions = globSync(
path.join(CACHE_FOLDER, `${filenamePrefix}*${filenameSuffix}`)
)
.sort()
.map((path) => path.split('/').pop()!)
.reverse();
let resolvedFilename: string | undefined;
if (preferredVersion === 'latest') {
resolvedFilename = localVersions.filter(
(filename) =>
!filename.includes('beta') && !filename.includes('nightly')
)[0];
} else if (preferredVersion === 'beta') {
resolvedFilename = localVersions.filter((filename) =>
filename.includes('beta')
)[0];
} else if (preferredVersion === 'nightly') {
resolvedFilename = localVersions.filter((filename) =>
filename.includes('nightly')
)[0];
} else {
resolvedFilename = localVersions.filter((filename) =>
filename.startsWith(`${filenamePrefix}${preferredVersion}`)
)[0];
}
if (resolvedFilename) {
const localZipPath = path.join(CACHE_FOLDER, resolvedFilename);
const zipFile = readAsFile(localZipPath);
return {
zipFile,
localZipPath,
};
}

// We don't have this WordPress version on the device. Let's
// do a network lookup.
try {
wpDetails = await resolveWPRelease(preferredVersion);
const localZipPath = path.join(
CACHE_FOLDER,
`${filenamePrefix}${wpDetails.version}${filenameSuffix}`
);
return {
zipFile: fs.existsSync(localZipPath)
? readAsFile(localZipPath)
: fetchWordPress(wpDetails.url, monitor),
localZipPath,
};
} catch (e) {
throw new Error(
`Could not resolve WordPress ${resolvedFilename} from local cache (you're offline)`,
{
cause: e,
}
);
}
}

export async function resolveWPRelease(version = 'latest') {
// Support custom URLs
if (version.startsWith('https://') || version.startsWith('http://')) {
Expand Down
Loading