2022-07-27 10:50:48 -05:00
|
|
|
import { loadFixture as baseLoadFixture } from '../../../astro/test/test-utils.js';
|
2022-07-27 15:15:19 -05:00
|
|
|
import { spawn } from 'child_process';
|
|
|
|
import { fileURLToPath } from 'url';
|
2022-07-27 10:50:48 -05:00
|
|
|
|
|
|
|
export { fixLineEndings } from '../../../astro/test/test-utils.js';
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
export function runCLI(basePath, { silent }) {
|
|
|
|
const script = fileURLToPath(new URL(`${basePath}/dist/_worker.js`, import.meta.url));
|
|
|
|
const p = spawn('node', [wranglerPath, 'dev', '-l', script]);
|
|
|
|
|
|
|
|
p.stderr.setEncoding('utf-8');
|
|
|
|
p.stdout.setEncoding('utf-8');
|
|
|
|
|
|
|
|
const timeout = 10000;
|
|
|
|
|
|
|
|
const ready = new Promise(async (resolve, reject) => {
|
2022-07-27 15:17:38 -05:00
|
|
|
const failed = setTimeout(
|
|
|
|
() => 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
|
|
|
// eslint-disable-next-line
|
|
|
|
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
|
|
|
// eslint-disable-next-line
|
|
|
|
console.log(msg);
|
|
|
|
}
|
2022-07-27 15:17:38 -05:00
|
|
|
if (msg.includes(`Listening on`)) {
|
2022-07-27 15:15:19 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTimeout(failed);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
ready,
|
|
|
|
stop() {
|
|
|
|
p.kill();
|
2022-07-27 15:17:38 -05:00
|
|
|
},
|
|
|
|
};
|
2022-07-27 15:15:19 -05:00
|
|
|
}
|