Skip to content

Commit

Permalink
Wait for vue dev server
Browse files Browse the repository at this point in the history
Allow dev server fetch to fail

Signed-off-by: Phillip Rak <rak.phillip@gmail.com>
  • Loading branch information
rak-phillip committed Aug 7, 2023
1 parent 5bedfa1 commit c59a29a
Showing 1 changed file with 48 additions and 22 deletions.
70 changes: 48 additions & 22 deletions scripts/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,7 @@ class DevRunner extends events.EventEmitter {
async startMainProcess() {
try {
await buildUtils.buildMain();
const rendererEnv = this.rendererEnv();

// Wait for the renderer to finish, so that the output from nuxt doesn't
// clobber debugging output.
while (true) {
if ((await fetch(rendererEnv.home, { agent: rendererEnv.agent })).ok) {
break;
}
await util.promisify(setTimeout)(1000);
}
this.#mainProcess = this.spawn(
'Main process',
'node',
Expand Down Expand Up @@ -112,18 +103,51 @@ class DevRunner extends events.EventEmitter {
*/
async startRendererProcess(): Promise<void> {
await buildUtils.buildPreload();
this.#rendererProcess = this.spawn(
'Renderer process',
'yarn',
'run',
'dev:ui',
'--hostname',
'localhost',
'--port',
this.rendererPort.toString(),
);

return Promise.resolve();

return new Promise((resolve, reject) => {
this.#rendererProcess = this.spawn(
'Renderer process',
'yarn',
'run',
'dev:ui',
'--hostname',
'localhost',
'--port',
this.rendererPort.toString(),
);

// Listen for the 'exit' event of the child process and resolve or reject the Promise accordingly.
this.#rendererProcess.on('exit', (code, _signal) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`Renderer build failed with code ${ code }`));
}
});

// Wait for the renderer to finish, so that vue-cli output doesn't
// clobber debugging output.
const rendererEnv = this.rendererEnv();
const maxRetries = 10;
let retryCount = 0;

const serverCheckInterval = setInterval(async() => {
try {
const response = await fetch(rendererEnv.home, { agent: rendererEnv.agent });

if (response.ok) {
clearInterval(serverCheckInterval);
resolve();
}
} catch (error) {
retryCount++;
if (retryCount >= maxRetries) {
clearInterval(serverCheckInterval);
reject(new Error(`Renderer build failed to connect after ${ maxRetries } attempts`));
}
}
}, 1000);
});
}

exit() {
Expand All @@ -134,10 +158,12 @@ class DevRunner extends events.EventEmitter {
async run() {
process.env.NODE_ENV = 'development';
try {
await this.startRendererProcess();

await buildUtils.wait(
() => this.startRendererProcess(),
() => this.startMainProcess(),
);

await new Promise((resolve, reject) => {
this.on('error', reject);
});
Expand Down

0 comments on commit c59a29a

Please sign in to comment.