Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Fix text exporter
Browse files- app/scripts/export-txt.mjs +36 -12
app/scripts/export-txt.mjs
CHANGED
|
@@ -35,16 +35,23 @@ async function run(command, args = [], options = {}) {
|
|
| 35 |
});
|
| 36 |
}
|
| 37 |
|
| 38 |
-
async function waitForServer(
|
|
|
|
| 39 |
const start = Date.now();
|
| 40 |
while (Date.now() - start < timeoutMs) {
|
| 41 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
const res = await fetch(url);
|
| 43 |
if (res.ok) return;
|
| 44 |
} catch { }
|
| 45 |
await delay(500);
|
| 46 |
}
|
| 47 |
-
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
function parseArgs(argv) {
|
|
@@ -418,6 +425,22 @@ async function main() {
|
|
| 418 |
console.log('> Starting Astro preview…');
|
| 419 |
// Capture stdout to detect the actual port used
|
| 420 |
let capturedPort = 8080;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 421 |
const preview = spawn('npm', ['run', 'preview'], {
|
| 422 |
cwd,
|
| 423 |
stdio: ['ignore', 'pipe', 'pipe'],
|
|
@@ -428,27 +451,28 @@ async function main() {
|
|
| 428 |
preview.stdout.on('data', (data) => {
|
| 429 |
const output = data.toString();
|
| 430 |
process.stdout.write(output);
|
| 431 |
-
|
| 432 |
-
if (match) {
|
| 433 |
-
capturedPort = parseInt(match[1]);
|
| 434 |
-
}
|
| 435 |
});
|
| 436 |
|
| 437 |
preview.stderr.on('data', (data) => {
|
| 438 |
-
|
|
|
|
|
|
|
| 439 |
});
|
| 440 |
|
| 441 |
const previewExit = new Promise((resolvePreview) => {
|
| 442 |
preview.on('close', (code, signal) => resolvePreview({ code, signal }));
|
| 443 |
});
|
| 444 |
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
|
|
|
| 448 |
|
| 449 |
try {
|
| 450 |
-
await waitForServer(
|
| 451 |
-
|
|
|
|
| 452 |
|
| 453 |
const browser = await chromium.launch({ headless: true });
|
| 454 |
try {
|
|
|
|
| 35 |
});
|
| 36 |
}
|
| 37 |
|
| 38 |
+
async function waitForServer(urlOrFn, timeoutMs = 60000) {
|
| 39 |
+
const getUrl = typeof urlOrFn === 'function' ? urlOrFn : () => urlOrFn;
|
| 40 |
const start = Date.now();
|
| 41 |
while (Date.now() - start < timeoutMs) {
|
| 42 |
try {
|
| 43 |
+
const url = getUrl();
|
| 44 |
+
if (!url) {
|
| 45 |
+
await delay(200);
|
| 46 |
+
continue;
|
| 47 |
+
}
|
| 48 |
const res = await fetch(url);
|
| 49 |
if (res.ok) return;
|
| 50 |
} catch { }
|
| 51 |
await delay(500);
|
| 52 |
}
|
| 53 |
+
const lastUrl = getUrl();
|
| 54 |
+
throw new Error(`Server did not start in time: ${lastUrl || '(unknown url)'}`);
|
| 55 |
}
|
| 56 |
|
| 57 |
function parseArgs(argv) {
|
|
|
|
| 425 |
console.log('> Starting Astro preview…');
|
| 426 |
// Capture stdout to detect the actual port used
|
| 427 |
let capturedPort = 8080;
|
| 428 |
+
let sawPreviewUrl = false;
|
| 429 |
+
|
| 430 |
+
const maybeCapturePort = (output) => {
|
| 431 |
+
const match = output.match(/http:\/\/localhost:(\d+)\//);
|
| 432 |
+
if (match) {
|
| 433 |
+
capturedPort = parseInt(match[1]);
|
| 434 |
+
sawPreviewUrl = true;
|
| 435 |
+
}
|
| 436 |
+
};
|
| 437 |
+
|
| 438 |
+
const previewPortEnv = process.env.PREVIEW_PORT ? Number(process.env.PREVIEW_PORT) : null;
|
| 439 |
+
if (previewPortEnv) {
|
| 440 |
+
capturedPort = previewPortEnv;
|
| 441 |
+
sawPreviewUrl = true;
|
| 442 |
+
}
|
| 443 |
+
|
| 444 |
const preview = spawn('npm', ['run', 'preview'], {
|
| 445 |
cwd,
|
| 446 |
stdio: ['ignore', 'pipe', 'pipe'],
|
|
|
|
| 451 |
preview.stdout.on('data', (data) => {
|
| 452 |
const output = data.toString();
|
| 453 |
process.stdout.write(output);
|
| 454 |
+
maybeCapturePort(output);
|
|
|
|
|
|
|
|
|
|
| 455 |
});
|
| 456 |
|
| 457 |
preview.stderr.on('data', (data) => {
|
| 458 |
+
const output = data.toString();
|
| 459 |
+
process.stderr.write(output);
|
| 460 |
+
maybeCapturePort(output);
|
| 461 |
});
|
| 462 |
|
| 463 |
const previewExit = new Promise((resolvePreview) => {
|
| 464 |
preview.on('close', (code, signal) => resolvePreview({ code, signal }));
|
| 465 |
});
|
| 466 |
|
| 467 |
+
const getBaseUrl = () => {
|
| 468 |
+
if (!sawPreviewUrl) return null;
|
| 469 |
+
return `http://localhost:${capturedPort}/`;
|
| 470 |
+
};
|
| 471 |
|
| 472 |
try {
|
| 473 |
+
await waitForServer(getBaseUrl, 60000);
|
| 474 |
+
const baseUrl = getBaseUrl();
|
| 475 |
+
console.log(`> Server ready (${baseUrl}), extracting content…`);
|
| 476 |
|
| 477 |
const browser = await chromium.launch({ headless: true });
|
| 478 |
try {
|