@wp-playground/cli 1.0.8 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli.js +4 -4
- package/package.json +9 -9
package/cli.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import g from"fs";import
|
|
1
|
+
import g from"fs";import P,{basename as B}from"path";import E from"yargs";import F from"express";import{SupportedPHPVersions as z,PHPResponse as I}from"@php-wasm/universal";import{logger as c,errorLogPath as D}from"@php-wasm/logger";import{runBlueprintSteps as x,compileBlueprint as C}from"@wp-playground/blueprints";import{EmscriptenDownloadMonitor as k,ProgressTracker as L}from"@php-wasm/progress";import{loadNodeRuntime as _,createNodeFsMountHandler as U}from"@php-wasm/node";import{RecommendedPHPVersion as Z,zipDirectory as q}from"@wp-playground/common";import{bootWordPress as A}from"@wp-playground/wordpress";import{rootCertificates as M}from"tls";import{createHash as O}from"crypto";import y from"fs-extra";import V from"os";async function j(e){const t=F(),r=await new Promise((a,u)=>{const s=t.listen(e.port,()=>{const d=s.address();d===null||typeof d=="string"?u(new Error("Server address is not available")):a(s)})});t.use("/",async(a,u)=>{const s=await e.handleRequest({url:a.url,headers:N(a),method:a.method,body:await T(a)});u.statusCode=s.httpStatusCode;for(const d in s.headers)u.setHeader(d,s.headers[d]);u.end(s.bytes)});const w=r.address().port;await e.onBind(w)}const T=async e=>await new Promise(t=>{const r=[];e.on("data",p=>{r.push(p)}),e.on("end",()=>{t(Buffer.concat(r))})}),N=e=>{const t={};if(e.rawHeaders&&e.rawHeaders.length)for(let r=0;r<e.rawHeaders.length;r+=2)t[e.rawHeaders[r].toLowerCase()]=e.rawHeaders[r+1];return t};function G(e){return/^latest$|^trunk$|^nightly$|^(?:(\d+)\.(\d+)(?:\.(\d+))?)((?:-beta(?:\d+)?)|(?:-RC(?:\d+)?))?$/.test(e)}const W=P.join(V.homedir(),".wordpress-playground");async function Y(e="latest",t){const r=await $(e);return await S(r.url,`${r.version}.zip`,t)}async function J(e){return await S("https://github.com/WordPress/sqlite-database-integration/archive/refs/heads/main.zip","sqlite.zip",e)}async function S(e,t,r){const p=P.join(W,t);return y.existsSync(p)||(y.ensureDirSync(W),await K(e,p,r)),H(p)}async function K(e,t,r){const w=(await r.monitorFetch(fetch(e))).body.getReader(),a=y.createWriteStream(t);for(;;){const{done:u,value:s}=await w.read();if(s&&a.write(s),u)break}a.close(),a.closed||await new Promise((u,s)=>{a.on("finish",d=>{d?s(d):u(null)})})}function H(e,t){return new File([y.readFileSync(e)],t??B(e))}async function $(e="latest"){if(e.startsWith("https://")||e.startsWith("http://")){const r=O("sha1");r.update(e);const p=r.digest("hex");return{url:e,version:"custom-"+p.substring(0,8)}}if(e==="trunk"||e==="nightly")return{url:"https://wordpress.org/nightly-builds/wordpress-latest.zip",version:"nightly-"+new Date().toISOString().split("T")[0]};let t=await fetch("https://api.wordpress.org/core/version-check/1.7/?channel=beta").then(r=>r.json());t=t.offers.filter(r=>r.response==="autoupdate");for(const r of t){if(e==="beta"&&r.version.includes("beta"))return{url:r.download,version:r.version};if(e==="latest")return{url:r.download,version:r.version};if(r.version.substring(0,e.length)===e)return{url:r.download,version:r.version}}return{url:`https://wordpress.org/wordpress-${e}.zip`,version:e}}async function Q(){const e=await E(process.argv.slice(2)).usage("Usage: wp-playground <command> [options]").positional("command",{describe:"Command to run",type:"string",choices:["server","run-blueprint","build-snapshot"]}).option("outfile",{describe:"When building, write to this output file.",type:"string",default:"wordpress.zip"}).option("port",{describe:"Port to listen on when serving.",type:"number",default:9400}).option("php",{describe:"PHP version to use.",type:"string",default:Z,choices:z}).option("wp",{describe:"WordPress version to use.",type:"string",default:"latest"}).option("mount",{describe:"Mount a directory to the PHP runtime. You can provide --mount multiple times. Format: /host/path:/vfs/path",type:"array",string:!0}).option("mountBeforeInstall",{describe:"Mount a directory to the PHP runtime before installing WordPress. You can provide --mount-before-install multiple times. Format: /host/path:/vfs/path",type:"array",string:!0}).option("login",{describe:"Should log the user in",type:"boolean",default:!1}).option("blueprint",{describe:"Blueprint to execute.",type:"string"}).option("skipWordPressSetup",{describe:"Do not download, unzip, and install WordPress. Useful for mounting a pre-configured WordPress directory at /wordpress.",type:"boolean",default:!1}).option("quiet",{describe:"Do not output logs and progress messages.",type:"boolean",default:!1}).option("debug",{describe:"Print PHP error log content if an error occurs during Playground boot.",type:"boolean",default:!1}).showHelpOnFail(!1).check(o=>{if(o.wp!==void 0&&!G(o.wp))try{new URL(o.wp)}catch{throw new Error('Unrecognized WordPress version. Please use "latest", a URL, or a numeric version such as "6.2", "6.0.1", "6.2-beta1", or "6.2-RC1"')}if(o.blueprint!==void 0){const l=P.resolve(process.cwd(),o.blueprint);if(!g.existsSync(l))throw new Error("Blueprint file does not exist");const n=g.readFileSync(l,"utf-8");try{o.blueprint=JSON.parse(n)}catch{throw new Error("Blueprint file is not a valid JSON file")}}return!0});e.wrap(e.terminalWidth());const t=await e.argv;t.quiet&&(c.handlers=[]);async function r(o){const{php:l,reap:n}=await s.processManager.acquirePHPInstance();try{await l.run({code:`<?php
|
|
2
2
|
$zip = new ZipArchive();
|
|
3
3
|
if(false === $zip->open('/tmp/build.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
|
|
4
4
|
throw new Exception('Failed to create ZIP');
|
|
@@ -15,6 +15,6 @@ import g from"fs";import y,{basename as W}from"path";import B from"yargs";import
|
|
|
15
15
|
}
|
|
16
16
|
$zip->close();
|
|
17
17
|
|
|
18
|
-
`});const i=l.readFileAsBuffer("/tmp/build.zip");g.writeFileSync(o,i)}finally{n()}}function p(o,l){const n=l.map(i=>{const[f,b]=i.split(":");return{hostPath:
|
|
19
|
-
`))}),C(o,{progress:l})}const a=t._[0];["run-blueprint","server","build-snapshot"].includes(a)||(e.showHelp(),process.exit(1));const u=w();let s,d=!1;c.log("Starting a PHP server..."),j({port:t.port,onBind:async o=>{const l=`http://127.0.0.1:${o}`;c.log(`Setting up WordPress ${t.wp}`);let n;const i=new k;t.skipWordPressSetup||(i.addEventListener("progress",h=>{const m=Math.round(Math.min(100,100*h.detail.loaded/h.detail.total));t.quiet||c.log(`\rDownloading WordPress ${m}%... `)}),n=await
|
|
20
|
-
`)},phpIniEntries:{"openssl.cafile":"/internal/shared/ca-bundle.crt",allow_url_fopen:"1",disable_functions:""},hooks:{async beforeWordPressFiles(h){t.mountBeforeInstall&&p(h,t.mountBeforeInstall)}}});const v=await s.getPrimaryPhp();try{if(n&&!t.mountBeforeInstall&&g.writeFileSync(f,await
|
|
18
|
+
`});const i=l.readFileAsBuffer("/tmp/build.zip");g.writeFileSync(o,i)}finally{n()}}function p(o,l){const n=l.map(i=>{const[f,b]=i.split(":");return{hostPath:P.resolve(process.cwd(),f),vfsPath:b}});for(const i of n)o.mkdir(i.vfsPath),o.mount(i.vfsPath,U(i.hostPath))}function w(){let o;t.blueprint?o=t.blueprint:o={preferredVersions:{php:t.php,wp:t.wp},login:t.login};const l=new L;let n="",i=!1;return l.addEventListener("progress",f=>{i||(f.detail.progress===100&&(i=!0),n=f.detail.caption||n||"Running the Blueprint",c.log(`\r\x1B[K${n.trim()} – ${f.detail.progress}%`),i&&c.log(`
|
|
19
|
+
`))}),C(o,{progress:l})}const a=t._[0];["run-blueprint","server","build-snapshot"].includes(a)||(e.showHelp(),process.exit(1));const u=w();let s,d=!1;c.log("Starting a PHP server..."),j({port:t.port,onBind:async o=>{const l=`http://127.0.0.1:${o}`;c.log(`Setting up WordPress ${t.wp}`);let n;const i=new k;t.skipWordPressSetup||(i.addEventListener("progress",h=>{const m=Math.round(Math.min(100,100*h.detail.loaded/h.detail.total));t.quiet||c.log(`\rDownloading WordPress ${m}%... `)}),n=await $(t.wp));const f=n&&P.join(W,`prebuilt-wp-content-for-wp-${n.version}.zip`),b=n?g.existsSync(f)?H(f):Y(n.url,i):void 0,R={WP_DEBUG:!0,WP_DEBUG_LOG:!0,WP_DEBUG_DISPLAY:!1};s=await A({siteUrl:l,createPhpRuntime:async()=>await _(u.versions.php),wordPressZip:b,sqliteIntegrationPluginZip:J(i),sapiName:"cli",createFiles:{"/internal/shared/ca-bundle.crt":M.join(`
|
|
20
|
+
`)},constants:R,phpIniEntries:{"openssl.cafile":"/internal/shared/ca-bundle.crt",allow_url_fopen:"1",disable_functions:""},hooks:{async beforeWordPressFiles(h){t.mountBeforeInstall&&p(h,t.mountBeforeInstall)}}});const v=await s.getPrimaryPhp();try{if(n&&!t.mountBeforeInstall&&g.writeFileSync(f,await q(v,"/wordpress")),t.mount&&p(v,t.mount),d=!0,u){const{php:h,reap:m}=await s.processManager.acquirePHPInstance();try{c.log("Running the Blueprint..."),await x(u,h),c.log("Finished running the blueprint")}finally{m()}}a==="build-snapshot"?(await r(t.outfile),c.log(`WordPress exported to ${t.outfile}`),process.exit(0)):a==="run-blueprint"?(c.log("Blueprint executed"),process.exit(0)):c.log(`WordPress is running on ${l}`)}catch(h){if(!t.debug)throw h;const m=v.readFileAsText(D);throw new Error(m,{cause:h})}},async handleRequest(o){return d?await s.request(o):I.forHttpCode(502,"WordPress is not ready yet")}})}Q();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wp-playground/cli",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.10",
|
|
4
4
|
"description": "WordPress Playground CLI",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"type": "module",
|
|
24
24
|
"main": "main.js",
|
|
25
25
|
"bin": "wp-playground.js",
|
|
26
|
-
"gitHead": "
|
|
26
|
+
"gitHead": "3fe05683f97e56f4dd14c5947163faec895c2452",
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"ajv": "8.12.0",
|
|
29
29
|
"async-lock": "1.4.1",
|
|
@@ -47,12 +47,12 @@
|
|
|
47
47
|
"wasm-feature-detect": "1.8.0",
|
|
48
48
|
"ws": "8.18.0",
|
|
49
49
|
"yargs": "17.7.2",
|
|
50
|
-
"@php-wasm/universal": "1.0.
|
|
51
|
-
"@php-wasm/logger": "1.0.
|
|
52
|
-
"@wp-playground/blueprints": "1.0.
|
|
53
|
-
"@php-wasm/progress": "1.0.
|
|
54
|
-
"@php-wasm/node": "1.0.
|
|
55
|
-
"@wp-playground/common": "1.0.
|
|
56
|
-
"@wp-playground/wordpress": "1.0.
|
|
50
|
+
"@php-wasm/universal": "1.0.10",
|
|
51
|
+
"@php-wasm/logger": "1.0.10",
|
|
52
|
+
"@wp-playground/blueprints": "1.0.10",
|
|
53
|
+
"@php-wasm/progress": "1.0.10",
|
|
54
|
+
"@php-wasm/node": "1.0.10",
|
|
55
|
+
"@wp-playground/common": "1.0.10",
|
|
56
|
+
"@wp-playground/wordpress": "1.0.10"
|
|
57
57
|
}
|
|
58
58
|
}
|