wp-studio 1.7.7-alpha1

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.
Files changed (48) hide show
  1. package/LICENSE.md +257 -0
  2. package/README.md +87 -0
  3. package/dist/cli/_events-BeOo0LuG.js +116 -0
  4. package/dist/cli/appdata-07CF2rhg.js +21090 -0
  5. package/dist/cli/archive-xDmkN4wb.js +15942 -0
  6. package/dist/cli/browser-CgWK-yoe.js +44 -0
  7. package/dist/cli/certificate-manager-DdBumKZp.js +250 -0
  8. package/dist/cli/create-BHVhkvTx.js +80 -0
  9. package/dist/cli/create-ZS29BDDi.js +40999 -0
  10. package/dist/cli/delete-BgQn-elT.js +56 -0
  11. package/dist/cli/delete-g8pgaLna.js +132 -0
  12. package/dist/cli/get-wordpress-version-BwSCJujO.js +18 -0
  13. package/dist/cli/index-7pbG_s_U.js +434 -0
  14. package/dist/cli/index-BXRYeCYG.js +1393 -0
  15. package/dist/cli/index-T3F1GwxX.js +2668 -0
  16. package/dist/cli/is-errno-exception-t38xF2pB.js +6 -0
  17. package/dist/cli/list-BE_UBjL5.js +105 -0
  18. package/dist/cli/list-DKz0XxM7.js +1032 -0
  19. package/dist/cli/logger-actions-OaIvl-ai.js +45 -0
  20. package/dist/cli/login-B4PkfKOu.js +82 -0
  21. package/dist/cli/logout-BC9gKlTj.js +48 -0
  22. package/dist/cli/main.js +5 -0
  23. package/dist/cli/mu-plugins-GEfKsl5U.js +530 -0
  24. package/dist/cli/passwords-DyzWd9Xi.js +80 -0
  25. package/dist/cli/process-manager-daemon.js +327 -0
  26. package/dist/cli/process-manager-ipc-AUZeYYDT.js +454 -0
  27. package/dist/cli/proxy-daemon.js +197 -0
  28. package/dist/cli/run-wp-cli-command-BctnMDWG.js +88 -0
  29. package/dist/cli/sequential-BQFuixXz.js +46 -0
  30. package/dist/cli/server-files-C_oy-mnI.js +26 -0
  31. package/dist/cli/set-DknhAZpw.js +327 -0
  32. package/dist/cli/site-utils-CfsabjUn.js +243 -0
  33. package/dist/cli/snapshots-6XE53y_F.js +874 -0
  34. package/dist/cli/sqlite-integration-H4OwSlwR.js +83 -0
  35. package/dist/cli/start-CRJqm09_.js +90 -0
  36. package/dist/cli/status-CWNHIOaY.js +44 -0
  37. package/dist/cli/status-CWWx9jYF.js +110 -0
  38. package/dist/cli/stop-CQosmjqA.js +117 -0
  39. package/dist/cli/update-BgL2HKHW.js +101 -0
  40. package/dist/cli/validation-error-DqLxqQuA.js +40 -0
  41. package/dist/cli/wordpress-server-child.js +514 -0
  42. package/dist/cli/wordpress-server-ipc-Dwsg9jSb.js +140 -0
  43. package/dist/cli/wordpress-server-manager-CtiuJqEb.js +566 -0
  44. package/dist/cli/wordpress-version-utils-B6UVeTh_.js +51 -0
  45. package/dist/cli/wp-UGSnlkN0.js +103 -0
  46. package/package.json +73 -0
  47. package/patches/@wp-playground+wordpress+3.1.12.patch +28 -0
  48. package/scripts/postinstall-npm.mjs +38 -0
@@ -0,0 +1,243 @@
1
+ import { d as decodePassword } from "./passwords-DyzWd9Xi.js";
2
+ import { S as SiteCommandLoggerAction } from "./logger-actions-OaIvl-ai.js";
3
+ import { __ } from "@wordpress/i18n";
4
+ import { L as LoggerError, A as getSiteUrl, r as readAppdata } from "./appdata-07CF2rhg.js";
5
+ import { o as openBrowser } from "./browser-CgWK-yoe.js";
6
+ import { g as generateSiteCertificate } from "./certificate-manager-DdBumKZp.js";
7
+ import { h as isProxyProcessRunning, i as isServerRunning, j as stopProxyProcess, l as startProxyProcess } from "./wordpress-server-manager-CtiuJqEb.js";
8
+ import fs from "fs";
9
+ import { domainToASCII } from "node:url";
10
+ import { tmpdir, platform } from "os";
11
+ import path from "path";
12
+ import { promisify } from "util";
13
+ import sudo from "@vscode/sudo-prompt";
14
+ const reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
15
+ function escapeRegex(string) {
16
+ return string ? string.replace(reRegExpChar, "\\$&") : string;
17
+ }
18
+ const sudoExec = (command, options) => {
19
+ return new Promise((resolve, reject) => {
20
+ sudo.exec(command, options, (error) => {
21
+ if (error) {
22
+ reject(error);
23
+ return;
24
+ }
25
+ resolve();
26
+ });
27
+ });
28
+ };
29
+ const readFile = promisify(fs.readFile);
30
+ const writeFile = promisify(fs.writeFile);
31
+ const HOST_FILES = {
32
+ win32: path.resolve(process.env.SystemRoot ?? "C:\\Windows", "System32\\drivers\\etc\\hosts"),
33
+ darwin: "/etc/hosts",
34
+ linux: "/etc/hosts"
35
+ };
36
+ const getHostsFilePath = () => {
37
+ const currentPlatform = platform();
38
+ const hostsPath = HOST_FILES[currentPlatform];
39
+ if (!hostsPath) {
40
+ throw new Error(`Unsupported platform: ${currentPlatform}`);
41
+ }
42
+ return hostsPath;
43
+ };
44
+ const readHostsFile = async () => {
45
+ try {
46
+ const hostsPath = getHostsFilePath();
47
+ const content = await readFile(hostsPath, "utf8");
48
+ return content;
49
+ } catch (error) {
50
+ console.error("Error reading hosts file:", error);
51
+ throw error;
52
+ }
53
+ };
54
+ const writeHostsFile = async (content) => {
55
+ const hostsPath = getHostsFilePath();
56
+ try {
57
+ const tempPath = path.join(tmpdir(), "wp-studio-hosts");
58
+ await writeFile(tempPath, content);
59
+ const command = platform() === "win32" ? `type ${tempPath} > ${hostsPath}` : `cat ${tempPath} > ${hostsPath}`;
60
+ await sudoExec(command, {
61
+ name: "WordPress Studio"
62
+ });
63
+ } catch (error) {
64
+ console.error("Error writing hosts file:", error);
65
+ throw error;
66
+ }
67
+ };
68
+ function createHostsEntryPattern(domain) {
69
+ const sanitizedDomain = domain.replace(/\\/g, "");
70
+ const escapedDomain = escapeRegex(sanitizedDomain);
71
+ return new RegExp(`127\\.0\\.0\\.1\\s+${escapedDomain}(\\s|$)`, "i");
72
+ }
73
+ const addDomainToHosts = async (domain, port) => {
74
+ try {
75
+ const hostsContent = await readHostsFile();
76
+ const encodedDomain = domainToASCII(domain);
77
+ const newContent = updateStudioBlock(hostsContent, (entries) => {
78
+ const pattern = createHostsEntryPattern(encodedDomain);
79
+ if (entries.some((entry) => entry.match(pattern))) {
80
+ return entries;
81
+ }
82
+ return [...entries, `127.0.0.1 ${encodedDomain} # Port ${port}`];
83
+ });
84
+ if (newContent !== hostsContent) {
85
+ await writeHostsFile(newContent);
86
+ }
87
+ } catch (error) {
88
+ console.error(`Error adding domain ${domain} to hosts file:`, error);
89
+ throw error;
90
+ }
91
+ };
92
+ const removeDomainFromHosts = async (domain) => {
93
+ try {
94
+ const hostsContent = await readHostsFile();
95
+ const encodedDomain = domainToASCII(domain);
96
+ const pattern = createHostsEntryPattern(encodedDomain);
97
+ const newContent = updateStudioBlock(
98
+ hostsContent,
99
+ (entries) => entries.filter((entry) => !entry.match(pattern))
100
+ );
101
+ if (newContent !== hostsContent) {
102
+ await writeHostsFile(newContent);
103
+ }
104
+ } catch (error) {
105
+ console.error(`Error removing domain ${domain} from hosts file:`, error);
106
+ throw error;
107
+ }
108
+ };
109
+ const updateDomainInHosts = async (oldDomain, newDomain, port) => {
110
+ if (oldDomain === newDomain) {
111
+ return;
112
+ }
113
+ if (!oldDomain && newDomain) {
114
+ await addDomainToHosts(newDomain, port);
115
+ return;
116
+ }
117
+ if (oldDomain && !newDomain) {
118
+ await removeDomainFromHosts(oldDomain);
119
+ return;
120
+ }
121
+ try {
122
+ const hostsContent = await readHostsFile();
123
+ const encodedOldDomain = domainToASCII(oldDomain);
124
+ const encodedNewDomain = domainToASCII(newDomain);
125
+ const oldPattern = createHostsEntryPattern(encodedOldDomain);
126
+ const newContent = updateStudioBlock(hostsContent, (entries) => {
127
+ const filtered = entries.filter((entry) => !entry.match(oldPattern));
128
+ return [...filtered, `127.0.0.1 ${encodedNewDomain} # Port ${port}`];
129
+ });
130
+ if (newContent !== hostsContent) {
131
+ await writeHostsFile(newContent);
132
+ }
133
+ } catch (error) {
134
+ console.error(
135
+ `Error replacing domain ${oldDomain} with ${newDomain} in hosts file:`,
136
+ error
137
+ );
138
+ throw error;
139
+ }
140
+ };
141
+ function updateStudioBlock(content, updateFn) {
142
+ const STUDIO_BLOCK_PATTERN = /(^|\n)(# BEGIN WordPress Studio)([\s\S]*?)\n(# END WordPress Studio)/;
143
+ const match = content.match(STUDIO_BLOCK_PATTERN);
144
+ if (match) {
145
+ const [_, space, begin, block, end] = match;
146
+ const before = content.slice(0, match.index);
147
+ const after = content.slice((match.index ?? 0) + match[0].length);
148
+ const entries = block.split("\n").filter(Boolean);
149
+ const newEntries = updateFn(entries);
150
+ if (!newEntries.length) {
151
+ return before + after;
152
+ }
153
+ const newLines = [begin, ...newEntries, end];
154
+ return before + space + newLines.join("\n") + after;
155
+ } else {
156
+ const newEntries = updateFn([]);
157
+ if (newEntries.length) {
158
+ return content + ["\n", "# BEGIN WordPress Studio", ...newEntries, "# END WordPress Studio"].join("\n");
159
+ }
160
+ }
161
+ return content;
162
+ }
163
+ async function startProxyIfNeeded(logger) {
164
+ const proxyProcess = await isProxyProcessRunning();
165
+ if (!proxyProcess) {
166
+ logger.reportStart(SiteCommandLoggerAction.START_PROXY, __("Starting HTTP proxy server…"));
167
+ await startProxyProcess();
168
+ logger.reportSuccess(__("HTTP proxy server started"));
169
+ } else {
170
+ logger.reportSuccess(__("HTTP proxy already running"));
171
+ }
172
+ }
173
+ async function openSiteInBrowser(site) {
174
+ const siteUrl = getSiteUrl(site);
175
+ try {
176
+ const autoLoginUrl = `${siteUrl}/studio-auto-login?redirect_to=${encodeURIComponent(
177
+ `${siteUrl}/wp-admin/`
178
+ )}`;
179
+ await openBrowser(autoLoginUrl);
180
+ } catch (error) {
181
+ }
182
+ }
183
+ function logSiteDetails(site) {
184
+ const siteUrl = getSiteUrl(site);
185
+ console.log(__("Site URL: "), siteUrl);
186
+ console.log(__("Username: "), site.adminUsername || "admin");
187
+ if (site.adminPassword) {
188
+ console.log(__("Password: "), decodePassword(site.adminPassword));
189
+ }
190
+ }
191
+ async function setupCustomDomain(site, logger, options) {
192
+ if (!site.customDomain) {
193
+ return;
194
+ }
195
+ await startProxyIfNeeded(logger);
196
+ if (site.enableHttps && !site.tlsKey && !site.tlsCert) {
197
+ logger.reportStart(SiteCommandLoggerAction.GENERATE_CERT, __("Generating SSL certificates…"));
198
+ await generateSiteCertificate(site.customDomain);
199
+ logger.reportSuccess(__("SSL certificates generated"));
200
+ }
201
+ if (!options?.skipHostsUpdate) {
202
+ logger.reportStart(SiteCommandLoggerAction.ADD_DOMAIN_TO_HOSTS, __("Adding domain to hosts file…"));
203
+ try {
204
+ await addDomainToHosts(site.customDomain, site.port);
205
+ logger.reportSuccess(__("Domain added to hosts file"));
206
+ } catch (error) {
207
+ throw new LoggerError(__("Failed to add domain to hosts file"), error);
208
+ }
209
+ }
210
+ }
211
+ async function stopProxyIfNoSitesNeedIt(stoppedSiteIds, logger) {
212
+ const stoppedSiteIdsArray = Array.isArray(stoppedSiteIds) ? stoppedSiteIds : [stoppedSiteIds];
213
+ const proxyProcess = await isProxyProcessRunning();
214
+ if (!proxyProcess) {
215
+ return;
216
+ }
217
+ const appdata = await readAppdata();
218
+ const remainingSitesWithCustomDomains = appdata.sites.filter(
219
+ (site) => !stoppedSiteIdsArray.includes(site.id) && site.customDomain
220
+ );
221
+ const sitesStillRunning = await Promise.all(
222
+ remainingSitesWithCustomDomains.map((site) => isServerRunning(site.id))
223
+ );
224
+ if (sitesStillRunning.some((isRunning) => isRunning)) {
225
+ return;
226
+ }
227
+ logger.reportStart(SiteCommandLoggerAction.STOP_PROXY, __("Stopping HTTP proxy server…"));
228
+ await stopProxyProcess();
229
+ logger.reportSuccess(__("HTTP proxy server stopped"));
230
+ }
231
+ const isSiteRunning = async (site) => {
232
+ const processInfo = await isServerRunning(site.id);
233
+ return !!(processInfo && processInfo.status === "online" && site.latestCliPid !== void 0 && processInfo.pid === site.latestCliPid);
234
+ };
235
+ export {
236
+ stopProxyIfNoSitesNeedIt as a,
237
+ isSiteRunning as i,
238
+ logSiteDetails as l,
239
+ openSiteInBrowser as o,
240
+ removeDomainFromHosts as r,
241
+ setupCustomDomain as s,
242
+ updateDomainInHosts as u
243
+ };