2023-07-17 19:17:59 -05:00
|
|
|
import { spawn } from 'node:child_process';
|
|
|
|
import { fileURLToPath } from 'node:url';
|
2023-08-15 12:10:51 -05:00
|
|
|
import kill from 'kill-port';
|
2022-07-27 10:50:48 -05:00
|
|
|
import { loadFixture as baseLoadFixture } from '../../../astro/test/test-utils.js';
|
|
|
|
|
|
|
|
export { fixLineEndings } from '../../../astro/test/test-utils.js';
|
|
|
|
|
2023-05-17 08:23:20 -05:00
|
|
|
/**
|
2023-07-17 07:30:02 -05:00
|
|
|
* @typedef {{ ready: Promise<void>, stop: Promise<void> }} WranglerCLI
|
2023-05-17 08:23:20 -05:00
|
|
|
* @typedef {import('../../../astro/test/test-utils').Fixture} Fixture
|
|
|
|
*/
|
|
|
|
|
2022-07-27 10:50:48 -05:00
|
|
|
export function loadFixture(config) {
|
|
|
|
if (config?.root) {
|
|
|
|
config.root = new URL(config.root, import.meta.url);
|
|
|
|
}
|
|
|
|
return baseLoadFixture(config);
|
|
|
|
}
|
2022-07-27 15:15:19 -05:00
|
|
|
|
2022-07-27 15:17:38 -05:00
|
|
|
const wranglerPath = fileURLToPath(
|
|
|
|
new URL('../node_modules/wrangler/bin/wrangler.js', import.meta.url)
|
|
|
|
);
|
2022-07-27 15:15:19 -05:00
|
|
|
|
2023-07-17 07:30:02 -05:00
|
|
|
/**
|
2023-08-15 12:10:51 -05:00
|
|
|
* @returns {Promise<WranglerCLI>}
|
2023-07-17 07:30:02 -05:00
|
|
|
*/
|
2023-08-15 12:10:51 -05:00
|
|
|
export async function runCLI(basePath, { silent, port }) {
|
|
|
|
// Hack: force existing process on port to be killed
|
|
|
|
try {
|
|
|
|
await kill(port, 'tcp');
|
|
|
|
} catch {
|
|
|
|
// Will throw if port is not in use, but that's fine
|
|
|
|
}
|
|
|
|
|
2022-07-27 15:15:19 -05:00
|
|
|
const script = fileURLToPath(new URL(`${basePath}/dist/_worker.js`, import.meta.url));
|
2023-08-15 12:13:43 -05:00
|
|
|
const p = spawn('node', [
|
|
|
|
wranglerPath,
|
|
|
|
'dev',
|
|
|
|
script,
|
|
|
|
'--port',
|
|
|
|
port,
|
|
|
|
'--log-level',
|
|
|
|
'info',
|
|
|
|
'--persist-to',
|
|
|
|
`${basePath}/.wrangler/state`,
|
|
|
|
]);
|
2022-07-27 15:15:19 -05:00
|
|
|
|
|
|
|
p.stderr.setEncoding('utf-8');
|
|
|
|
p.stdout.setEncoding('utf-8');
|
|
|
|
|
2023-08-16 13:28:57 -05:00
|
|
|
const timeout = 20_000;
|
2022-07-27 15:15:19 -05:00
|
|
|
|
|
|
|
const ready = new Promise(async (resolve, reject) => {
|
2023-08-15 12:13:43 -05:00
|
|
|
const failed = setTimeout(() => {
|
|
|
|
p.kill();
|
|
|
|
reject(new Error(`Timed out starting the wrangler CLI`));
|
|
|
|
}, timeout);
|
2022-07-27 15:15:19 -05:00
|
|
|
|
|
|
|
(async function () {
|
2022-07-27 15:17:38 -05:00
|
|
|
for (const msg of p.stderr) {
|
|
|
|
if (!silent) {
|
2022-07-27 15:15:19 -05:00
|
|
|
console.error(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2022-07-27 15:17:38 -05:00
|
|
|
for await (const msg of p.stdout) {
|
|
|
|
if (!silent) {
|
2022-07-27 15:15:19 -05:00
|
|
|
console.log(msg);
|
|
|
|
}
|
2023-08-15 12:10:51 -05:00
|
|
|
if (msg.includes(`[mf:inf] Ready on`)) {
|
2022-07-27 15:15:19 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTimeout(failed);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
ready,
|
|
|
|
stop() {
|
2023-06-21 08:09:49 -05:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
p.on('close', () => resolve());
|
|
|
|
p.on('error', (err) => reject(err));
|
|
|
|
p.kill();
|
|
|
|
});
|
2022-07-27 15:17:38 -05:00
|
|
|
},
|
|
|
|
};
|
2022-07-27 15:15:19 -05:00
|
|
|
}
|