-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathcommon.ts
33 lines (28 loc) · 978 Bytes
/
common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import type { PHPResponse, UniversalPHP } from '@php-wasm/universal';
export function asDOM(response: PHPResponse) {
return new DOMParser().parseFromString(response.text, 'text/html')!;
}
export function zipNameToHumanName(zipName: string) {
const mixedCaseName = zipName.split('.').shift()!.replace('-', ' ');
return (
mixedCaseName.charAt(0).toUpperCase() +
mixedCaseName.slice(1).toLowerCase()
);
}
type PatchFileCallback = (contents: string) => string | Uint8Array;
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));
}
export async function fileToUint8Array(file: File) {
return new Uint8Array(await file.arrayBuffer());
}
export const VFS_CONFIG_FILE_BASENAME = '/vfs-blueprints';
export const VFS_CONFIG_FILE_PATH = '/vfs-blueprints/wp-config-consts.php';