@pixelbyte-software/pixcode 1.40.2 → 1.40.4

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.
@@ -0,0 +1,333 @@
1
+ import { spawn } from 'node:child_process';
2
+ import { promises as fs } from 'node:fs';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+
6
+ import { buildCliSpawnEnv, findExecutableOnPath } from './install-jobs.js';
7
+
8
+ const DEFAULT_RUNTIMES_HOME = path.join(os.homedir(), '.pixcode', 'runtimes');
9
+ const MANIFEST_FILE = 'pixcode-runtime.json';
10
+ const installLocks = new Map();
11
+
12
+ function runtimesHome(env = process.env) {
13
+ return env.PIXCODE_MANAGED_RUNTIMES_HOME || DEFAULT_RUNTIMES_HOME;
14
+ }
15
+
16
+ function runtimeDir(id, env = process.env) {
17
+ return path.join(runtimesHome(env), id);
18
+ }
19
+
20
+ function manifestPath(id, env = process.env) {
21
+ return path.join(runtimeDir(id, env), MANIFEST_FILE);
22
+ }
23
+
24
+ async function fileExists(filePath) {
25
+ try {
26
+ const stats = await fs.stat(filePath);
27
+ return stats.isFile();
28
+ } catch {
29
+ return false;
30
+ }
31
+ }
32
+
33
+ async function readManifest(id, env = process.env) {
34
+ try {
35
+ const content = await fs.readFile(manifestPath(id, env), 'utf8');
36
+ const manifest = JSON.parse(content);
37
+ if (manifest?.executablePath && await fileExists(manifest.executablePath)) {
38
+ return manifest;
39
+ }
40
+ } catch {
41
+ // Missing or malformed manifests are treated as not installed.
42
+ }
43
+ return null;
44
+ }
45
+
46
+ function platformTokens() {
47
+ if (process.platform === 'win32') return ['windows'];
48
+ if (process.platform === 'darwin') return ['mac', 'darwin'];
49
+ if (process.platform === 'linux') return ['linux'];
50
+ return [process.platform];
51
+ }
52
+
53
+ function archTokens() {
54
+ if (process.arch === 'x64') return ['x86_64', 'amd64', 'x64'];
55
+ if (process.arch === 'arm64') return ['aarch64', 'arm64'];
56
+ return [process.arch];
57
+ }
58
+
59
+ function scoreFrankenPhpAsset(assetName) {
60
+ const name = assetName.toLowerCase();
61
+ if (!name.includes('frankenphp')) return -1;
62
+ if (name.includes('debug')) return -1;
63
+ if (!platformTokens().some((token) => name.includes(token))) return -1;
64
+ if (!archTokens().some((token) => name.includes(token))) return -1;
65
+
66
+ let score = 10;
67
+ if (process.platform === 'win32' && name.endsWith('.zip')) score += 10;
68
+ if (process.platform !== 'win32' && !name.endsWith('.zip')) score += 10;
69
+ if (!name.includes('gnu')) score += 2;
70
+ if (name.endsWith('.tar.gz') || name.endsWith('.tgz')) score += 1;
71
+ return score;
72
+ }
73
+
74
+ function selectFrankenPhpAsset(release) {
75
+ const assets = Array.isArray(release?.assets) ? release.assets : [];
76
+ const candidates = assets
77
+ .map((asset) => ({ asset, score: scoreFrankenPhpAsset(asset.name || '') }))
78
+ .filter((entry) => entry.score >= 0)
79
+ .sort((a, b) => b.score - a.score);
80
+
81
+ return candidates[0]?.asset || null;
82
+ }
83
+
84
+ async function fetchJson(url, env = process.env) {
85
+ const headers = {
86
+ Accept: 'application/vnd.github+json',
87
+ 'User-Agent': 'Pixcode Live View',
88
+ };
89
+ if (env.GITHUB_TOKEN) headers.Authorization = `Bearer ${env.GITHUB_TOKEN}`;
90
+
91
+ const response = await fetch(url, { headers });
92
+ if (!response.ok) {
93
+ throw new Error(`Runtime metadata request failed with HTTP ${response.status}`);
94
+ }
95
+ return response.json();
96
+ }
97
+
98
+ async function downloadFile(url, targetFile, env = process.env) {
99
+ const headers = { 'User-Agent': 'Pixcode Live View' };
100
+ if (env.GITHUB_TOKEN && url.includes('github.com')) headers.Authorization = `Bearer ${env.GITHUB_TOKEN}`;
101
+
102
+ const response = await fetch(url, { headers });
103
+ if (!response.ok) {
104
+ throw new Error(`Runtime download failed with HTTP ${response.status}`);
105
+ }
106
+ const buffer = Buffer.from(await response.arrayBuffer());
107
+ await fs.writeFile(targetFile, buffer);
108
+ }
109
+
110
+ function runProcess(command, args, options = {}) {
111
+ return new Promise((resolve, reject) => {
112
+ let stderr = '';
113
+ const child = spawn(command, args, {
114
+ ...options,
115
+ stdio: ['ignore', 'ignore', 'pipe'],
116
+ windowsHide: true,
117
+ });
118
+ child.stderr.on('data', (chunk) => {
119
+ stderr += chunk.toString();
120
+ });
121
+ child.on('error', reject);
122
+ child.on('close', (code) => {
123
+ if (code === 0) {
124
+ resolve();
125
+ return;
126
+ }
127
+ reject(new Error(`${command} exited with code ${code}${stderr ? `: ${stderr.trim()}` : ''}`));
128
+ });
129
+ });
130
+ }
131
+
132
+ async function extractZip(archivePath, targetDir, env = process.env) {
133
+ if (process.platform === 'win32') {
134
+ const shell = env.ComSpec || process.env.ComSpec || 'powershell.exe';
135
+ const isCmd = shell.toLowerCase().endsWith('cmd.exe');
136
+ if (isCmd) {
137
+ await runProcess('powershell.exe', [
138
+ '-NoProfile',
139
+ '-ExecutionPolicy',
140
+ 'Bypass',
141
+ '-Command',
142
+ 'Expand-Archive -Force -LiteralPath $args[0] -DestinationPath $args[1]',
143
+ archivePath,
144
+ targetDir,
145
+ ], { env });
146
+ return;
147
+ }
148
+ await runProcess(shell, [
149
+ '-NoProfile',
150
+ '-ExecutionPolicy',
151
+ 'Bypass',
152
+ '-Command',
153
+ 'Expand-Archive -Force -LiteralPath $args[0] -DestinationPath $args[1]',
154
+ archivePath,
155
+ targetDir,
156
+ ], { env });
157
+ return;
158
+ }
159
+
160
+ await runProcess('unzip', ['-q', archivePath, '-d', targetDir], { env });
161
+ }
162
+
163
+ async function extractTarGz(archivePath, targetDir, env = process.env) {
164
+ await runProcess('tar', ['-xzf', archivePath, '-C', targetDir], { env });
165
+ }
166
+
167
+ async function findRuntimeExecutable(searchRoot, binaryName) {
168
+ const expectedNames = process.platform === 'win32'
169
+ ? [`${binaryName}.exe`, binaryName]
170
+ : [binaryName];
171
+ const stack = [searchRoot];
172
+
173
+ while (stack.length > 0) {
174
+ const current = stack.pop();
175
+ const entries = await fs.readdir(current, { withFileTypes: true }).catch(() => []);
176
+ for (const entry of entries) {
177
+ const full = path.join(current, entry.name);
178
+ if (entry.isDirectory()) {
179
+ stack.push(full);
180
+ continue;
181
+ }
182
+ if (entry.isFile() && expectedNames.includes(entry.name)) {
183
+ return full;
184
+ }
185
+ }
186
+ }
187
+
188
+ return null;
189
+ }
190
+
191
+ async function installFrankenPhp(env = process.env) {
192
+ const releaseApiUrl = env.PIXCODE_FRANKENPHP_RELEASE_API
193
+ || 'https://api.github.com/repos/php/frankenphp/releases/latest';
194
+ const release = env.PIXCODE_FRANKENPHP_URL ? null : await fetchJson(releaseApiUrl, env);
195
+ const asset = env.PIXCODE_FRANKENPHP_URL
196
+ ? {
197
+ name: path.basename(new URL(env.PIXCODE_FRANKENPHP_URL).pathname),
198
+ browser_download_url: env.PIXCODE_FRANKENPHP_URL,
199
+ }
200
+ : selectFrankenPhpAsset(release);
201
+
202
+ if (!asset?.browser_download_url) {
203
+ throw new Error('No FrankenPHP binary is available for this operating system and CPU architecture.');
204
+ }
205
+
206
+ const baseDir = runtimeDir('frankenphp', env);
207
+ const stagingDir = path.join(baseDir, `.staging-${Date.now()}`);
208
+ const currentDir = path.join(baseDir, 'current');
209
+ await fs.mkdir(stagingDir, { recursive: true });
210
+
211
+ const archivePath = path.join(stagingDir, asset.name || 'frankenphp');
212
+ await downloadFile(asset.browser_download_url, archivePath, env);
213
+
214
+ let executablePath = archivePath;
215
+ const assetName = (asset.name || '').toLowerCase();
216
+ if (assetName.endsWith('.zip')) {
217
+ await extractZip(archivePath, stagingDir, env);
218
+ executablePath = await findRuntimeExecutable(stagingDir, 'frankenphp');
219
+ if (!executablePath) {
220
+ throw new Error('Downloaded FrankenPHP archive did not contain a frankenphp executable.');
221
+ }
222
+ } else if (assetName.endsWith('.tar.gz') || assetName.endsWith('.tgz')) {
223
+ await extractTarGz(archivePath, stagingDir, env);
224
+ executablePath = await findRuntimeExecutable(stagingDir, 'frankenphp');
225
+ if (!executablePath) {
226
+ throw new Error('Downloaded FrankenPHP archive did not contain a frankenphp executable.');
227
+ }
228
+ }
229
+
230
+ if (process.platform !== 'win32') {
231
+ await fs.chmod(executablePath, 0o755).catch(() => undefined);
232
+ }
233
+
234
+ await fs.rm(currentDir, { recursive: true, force: true });
235
+ await fs.mkdir(currentDir, { recursive: true });
236
+
237
+ const finalName = process.platform === 'win32' ? 'frankenphp.exe' : 'frankenphp';
238
+ const finalExecutable = path.join(currentDir, finalName);
239
+ await fs.copyFile(executablePath, finalExecutable);
240
+ if (process.platform !== 'win32') {
241
+ await fs.chmod(finalExecutable, 0o755).catch(() => undefined);
242
+ }
243
+
244
+ const manifest = {
245
+ id: 'frankenphp',
246
+ label: 'Pixcode PHP runtime',
247
+ provider: 'FrankenPHP',
248
+ version: release?.tag_name || 'custom',
249
+ executablePath: finalExecutable,
250
+ sourceUrl: asset.browser_download_url,
251
+ installedAt: new Date().toISOString(),
252
+ };
253
+ await fs.writeFile(manifestPath('frankenphp', env), JSON.stringify(manifest, null, 2));
254
+ await fs.rm(stagingDir, { recursive: true, force: true }).catch(() => undefined);
255
+ return manifest;
256
+ }
257
+
258
+ export async function getManagedRuntimeStatus(id, options = {}) {
259
+ const env = options.env || process.env;
260
+ if (id !== 'frankenphp') {
261
+ return {
262
+ id,
263
+ status: 'unsupported',
264
+ installable: false,
265
+ reason: 'Pixcode does not have a managed runtime for this stack yet.',
266
+ };
267
+ }
268
+
269
+ const spawnEnv = buildCliSpawnEnv(env);
270
+ const systemExecutable = findExecutableOnPath('frankenphp', spawnEnv);
271
+ if (systemExecutable) {
272
+ return {
273
+ id,
274
+ label: 'FrankenPHP',
275
+ status: 'system',
276
+ installable: true,
277
+ executablePath: systemExecutable,
278
+ };
279
+ }
280
+
281
+ const manifest = await readManifest(id, env);
282
+ if (manifest) {
283
+ return {
284
+ id,
285
+ label: manifest.label || 'Pixcode PHP runtime',
286
+ status: 'installed',
287
+ installable: true,
288
+ executablePath: manifest.executablePath,
289
+ version: manifest.version,
290
+ };
291
+ }
292
+
293
+ return {
294
+ id,
295
+ label: 'Pixcode PHP runtime',
296
+ provider: 'FrankenPHP',
297
+ status: 'missing',
298
+ installable: true,
299
+ reason: 'Pixcode will prepare a local PHP runtime automatically before starting this project.',
300
+ };
301
+ }
302
+
303
+ export async function ensureManagedRuntime(id, options = {}) {
304
+ const env = options.env || process.env;
305
+ const status = await getManagedRuntimeStatus(id, { env });
306
+ if (status.executablePath) return status;
307
+ if (!status.installable) {
308
+ throw new Error(status.reason || 'This runtime cannot be prepared automatically.');
309
+ }
310
+
311
+ const lockKey = `${runtimesHome(env)}:${id}`;
312
+ if (installLocks.has(lockKey)) return installLocks.get(lockKey);
313
+
314
+ const installPromise = (async () => {
315
+ if (id === 'frankenphp') {
316
+ const manifest = await installFrankenPhp(env);
317
+ return {
318
+ id,
319
+ label: manifest.label,
320
+ status: 'installed',
321
+ installable: true,
322
+ executablePath: manifest.executablePath,
323
+ version: manifest.version,
324
+ };
325
+ }
326
+ throw new Error(`Unsupported managed runtime: ${id}`);
327
+ })().finally(() => {
328
+ installLocks.delete(lockKey);
329
+ });
330
+
331
+ installLocks.set(lockKey, installPromise);
332
+ return installPromise;
333
+ }