Skip to content
Merged
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
2 changes: 2 additions & 0 deletions packages/playground/blueprints/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export type {
VFSResource,
} from './lib/resources';

export { wpContentFilesExcludedFromExport } from './lib/utils/wp-content-files-excluded-from-exports';

/**
* @deprecated This function is a no-op. Playground no longer uses a proxy to download plugins and themes.
* To be removed in v0.3.0
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/blueprints/src/lib/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from '@php-wasm/progress';
import { UniversalPHP } from '@php-wasm/universal';
import { Semaphore } from '@php-wasm/util';
import { zipNameToHumanName } from './steps/common';
import { zipNameToHumanName } from './utils/zip-name-to-human-name';

export const ResourceTypes = [
'vfs',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { UniversalPHP } from '@php-wasm/universal';
import { StepHandler } from '..';
import { updateFile } from '../common';
import { defineWpConfigConsts } from '../define-wp-config-consts';

/** @ts-ignore */
Expand All @@ -9,6 +8,7 @@ import transportFetch from './wp-content/mu-plugins/playground-includes/wp_http_
import transportDummy from './wp-content/mu-plugins/playground-includes/requests_transport_dummy.php?raw';
/** @ts-ignore */
import playgroundMuPlugin from './wp-content/mu-plugins/0-playground.php?raw';
import { updateFile } from '../../utils/update-file';

/**
* @private
Expand Down
76 changes: 0 additions & 76 deletions packages/playground/blueprints/src/lib/steps/common.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { StepHandler } from '.';
import { unzip } from './unzip';
import { dirname, joinPaths, phpVar } from '@php-wasm/util';
import { wpContentFilesExcludedFromExport } from './common';
import { UniversalPHP } from '@php-wasm/universal';
import { wpContentFilesExcludedFromExport } from '../utils/wp-content-files-excluded-from-exports';

/**
* @inheritDoc importWordPressFiles
Expand Down
2 changes: 0 additions & 2 deletions packages/playground/blueprints/src/lib/steps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ export type StepDefinition = Step & {
};
};

export { wpContentFilesExcludedFromExport } from './common';

/**
* If you add a step here, make sure to also
* add it to the exports below.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { UniversalPHP } from '@php-wasm/universal';
import { StepHandler } from '.';
import { zipNameToHumanName } from './common';
import { installAsset } from './install-asset';
import { activatePlugin } from './activate-plugin';
import { makeEditorFrameControlled } from './apply-wordpress-patches';
import { zipNameToHumanName } from '../utils/zip-name-to-human-name';

/**
* @inheritDoc installPlugin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { StepHandler } from '.';
import { zipNameToHumanName } from './common';
import { installAsset } from './install-asset';
import { activateTheme } from './activate-theme';
import { zipNameToHumanName } from '../utils/zip-name-to-human-name';

/**
* @inheritDoc installTheme
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/blueprints/src/lib/steps/unzip.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { phpVars } from '@php-wasm/util';
import { StepHandler } from '.';
import { runPhpWithZipFunctions } from './common';
import { runPhpWithZipFunctions } from '../utils/run-php-with-zip-functions';

/**
* @inheritDoc unzip
Expand Down
3 changes: 1 addition & 2 deletions packages/playground/blueprints/src/lib/steps/write-file.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { StepHandler } from '.';
import { fileToUint8Array } from './common';

/**
* @inheritDoc writeFile
Expand Down Expand Up @@ -31,7 +30,7 @@ export const writeFile: StepHandler<WriteFileStep<File>> = async (
{ path, data }
) => {
if (data instanceof File) {
data = await fileToUint8Array(data);
data = new Uint8Array(await data.arrayBuffer());
}
await playground.writeFile(path, data);
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { joinPaths, phpVars } from '@php-wasm/util';
import { runPhpWithZipFunctions } from './common';
import { wpContentFilesExcludedFromExport } from './common';
import { UniversalPHP } from '@php-wasm/universal';
import { wpContentFilesExcludedFromExport } from '../utils/wp-content-files-excluded-from-exports';
import { runPhpWithZipFunctions } from '../utils/run-php-with-zip-functions';

interface ZipWpContentOptions {
/**
Expand Down
36 changes: 36 additions & 0 deletions packages/playground/blueprints/src/lib/utils/flatten-directory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { UniversalPHP } from '@php-wasm/universal';
import { dirname, joinPaths } from '@php-wasm/util';

/**
* Flattens a directory.
* If the directory contains only one file, it will be moved to the parent directory.
* Otherwise, the directory will be renamed to the default name.
*
* @param php Playground client.
* @param directoryPath The directory to flatten.
* @param defaultName The name to use if the directory contains only one file.
* @returns The final path of the directory.
*/
export async function flattenDirectory(
php: UniversalPHP,
directoryPath: string,
defaultName: string
) {
const parentPath = dirname(directoryPath);

const filesInside = await php.listFiles(directoryPath);
if (filesInside.length === 1) {
const onlyFilePath = joinPaths(directoryPath, filesInside[0]);
const isDir = await php.isDir(onlyFilePath);
if (isDir) {
const finalPath = joinPaths(parentPath, filesInside[0]);
await php.mv(onlyFilePath, finalPath);
await php.rmdir(directoryPath, { recursive: true });
return finalPath;
}
}

const finalPath = joinPaths(parentPath, defaultName);
await php.mv(directoryPath, finalPath);
return finalPath;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { UniversalPHP } from '@php-wasm/universal';
// @ts-ignore
import zipFunctions from './zip-functions.php?raw';
export async function runPhpWithZipFunctions(
playground: UniversalPHP,
code: string
) {
const result = await playground.run({
code: zipFunctions + code,
});
if (result.exitCode !== 0) {
console.log(zipFunctions + code);
console.log(code + '');
console.log(result.errors);
throw result.errors;
}
return result;
}
23 changes: 23 additions & 0 deletions packages/playground/blueprints/src/lib/utils/update-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { UniversalPHP } from '@php-wasm/universal';

type PatchFileCallback = (contents: string) => string | Uint8Array;

/**
* Updates a file in the PHP filesystem.
* If the file does not exist, it will be created.
*
* @param php The PHP instance.
* @param path The path to the file.
* @param callback A function that maps the old file contents to the new file contents.
*/
export async function updateFile(
php: UniversalPHP,
path: string,
callback: PatchFileCallback
) {
let contents = '';
if (await php.fileExists(path)) {
contents = await php.readFileAsText(path);
}
await php.writeFile(path, callback(contents));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Used by the export step to exclude the Playground-specific files
* from the zip file. Keep it in sync with the list of files created
* by WordPressPatcher.
*/
export const wpContentFilesExcludedFromExport = [
'db.php',
'plugins/akismet',
'plugins/hello.php',
'plugins/wordpress-importer',
'plugins/sqlite-database-integration',
'mu-plugins/playground-includes',
'mu-plugins/export-wxz.php',
'mu-plugins/0-playground.php',

/*
* Listing core themes like that here isn't ideal, especially since
* developers may actually want to use one of them.
* @TODO Let's give the user a choice whether or not to include them.
*/
'themes/twentytwenty',
'themes/twentytwentyone',
'themes/twentytwentytwo',
'themes/twentytwentythree',
'themes/twentytwentyfour',
'themes/twentytwentyfive',
'themes/twentytwentysix',
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Converts a zip file name to a nice, human-readable name.
*
* @example `hello-dolly.zip` -> `Hello dolly`
*
* @param zipName A zip filename.
* @returns A nice, human-readable name.
*/
export function zipNameToHumanName(zipName: string) {
const mixedCaseName = zipName.split('.').shift()!.replace(/-/g, ' ');
return (
mixedCaseName.charAt(0).toUpperCase() +
mixedCaseName.slice(1).toLowerCase()
);
}