From df8ceffe389b55980db1061a1ec5a7f83775e0da Mon Sep 17 00:00:00 2001 From: Andreas Fehn Date: Wed, 3 Jan 2024 10:42:22 +0100 Subject: [PATCH 1/4] call post update sync --- .../vite-plugin-kit-routes/src/lib/plugin.ts | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/vite-plugin-kit-routes/src/lib/plugin.ts b/packages/vite-plugin-kit-routes/src/lib/plugin.ts index 4127dc60d..7e6963813 100644 --- a/packages/vite-plugin-kit-routes/src/lib/plugin.ts +++ b/packages/vite-plugin-kit-routes/src/lib/plugin.ts @@ -1,6 +1,6 @@ import { cyan, gray, green, italic, Log, red, stry0, yellow } from '@kitql/helpers' import { getFilesUnder, read, write, relative, dirname } from '@kitql/internals' -import { spawn } from 'child_process' +import { spawnSync } from 'child_process' import type { Plugin } from 'vite' import { watchAndRun } from 'vite-plugin-watch-and-run' @@ -942,28 +942,26 @@ ${objTypes } // do the stuff - const child = spawn(options.post_update_run, { shell: true }) + const child = spawnSync(options.post_update_run, { shell: true }) // report things if (shouldLog('post_update_run', options)) { - child.stdout.on('data', data => { - if (data.toString()) { - log.info(data.toString()) - } - }) + const stdout = child.stdout.toString() + if (stdout) { + log.info(stdout) + } } // report errors if (shouldLog('errors', options)) { - child.stderr.on('data', data => { - log.error(data.toString()) - }) + const stderr = child.stderr.toString() + if (stderr) { + log.error(stderr) + } } if (shouldLog('update', options)) { - child.on('close', () => { - theEnd(atStart, result, objTypes, options) - }) + theEnd(atStart, result, objTypes, options) } } else { theEnd(atStart, result, objTypes, options) From 30972c40d4718a9cd72de828c08a610558f21fac Mon Sep 17 00:00:00 2001 From: Andreas Fehn Date: Wed, 3 Jan 2024 10:42:50 +0100 Subject: [PATCH 2/4] generate routes during build and dev --- packages/vite-plugin-kit-routes/src/lib/plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite-plugin-kit-routes/src/lib/plugin.ts b/packages/vite-plugin-kit-routes/src/lib/plugin.ts index 7e6963813..ab32fe154 100644 --- a/packages/vite-plugin-kit-routes/src/lib/plugin.ts +++ b/packages/vite-plugin-kit-routes/src/lib/plugin.ts @@ -1067,7 +1067,7 @@ export function kitRoutes(options?: Options // Run the thing at startup { name: 'kit-routes', - configureServer() { + buildStart() { run(true, options) }, }, From 8df0a01624132fc799f4680d5d1736af0e903ff9 Mon Sep 17 00:00:00 2001 From: Andreas Fehn Date: Wed, 3 Jan 2024 10:43:55 +0100 Subject: [PATCH 3/4] changeset --- .changeset/short-vans-camp.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/short-vans-camp.md diff --git a/.changeset/short-vans-camp.md b/.changeset/short-vans-camp.md new file mode 100644 index 000000000..5874fc3eb --- /dev/null +++ b/.changeset/short-vans-camp.md @@ -0,0 +1,5 @@ +--- +'vite-plugin-kit-routes': minor +--- + +also generate routes during build From 351e1240f8b34f7b1f81ce8e7bbcd0455741320c Mon Sep 17 00:00:00 2001 From: Andreas Fehn Date: Wed, 3 Jan 2024 11:15:19 +0100 Subject: [PATCH 4/4] await child process close --- .../vite-plugin-kit-routes/src/lib/plugin.ts | 36 +++++++++++-------- .../src/lib/plugins.spec.ts | 12 +++---- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/packages/vite-plugin-kit-routes/src/lib/plugin.ts b/packages/vite-plugin-kit-routes/src/lib/plugin.ts index ab32fe154..705b3aab9 100644 --- a/packages/vite-plugin-kit-routes/src/lib/plugin.ts +++ b/packages/vite-plugin-kit-routes/src/lib/plugin.ts @@ -1,6 +1,6 @@ import { cyan, gray, green, italic, Log, red, stry0, yellow } from '@kitql/helpers' import { getFilesUnder, read, write, relative, dirname } from '@kitql/internals' -import { spawnSync } from 'child_process' +import { spawn } from 'child_process' import type { Plugin } from 'vite' import { watchAndRun } from 'vite-plugin-watch-and-run' @@ -775,7 +775,7 @@ const arrayToRecord = (arr?: string[]) => { return `: Record` } -export const run = (atStart: boolean, o?: Options) => { +export const run = async (atStart: boolean, o?: Options) => { const options = getDefaultOption(o) const files = getFilesUnder(routes_path()) @@ -942,24 +942,30 @@ ${objTypes } // do the stuff - const child = spawnSync(options.post_update_run, { shell: true }) + const child = spawn(options.post_update_run, { shell: true }) // report things if (shouldLog('post_update_run', options)) { - const stdout = child.stdout.toString() - if (stdout) { - log.info(stdout) - } + child.stdout.on('data', data => { + if (data.toString()) { + log.info(data.toString()) + } + }) } // report errors if (shouldLog('errors', options)) { - const stderr = child.stderr.toString() - if (stderr) { - log.error(stderr) - } + child.stderr.on('data', data => { + log.error(data.toString()) + }) } + const exitPromise = new Promise(resolve => { + child.on('close', () => resolve()) + }) + + await exitPromise + if (shouldLog('update', options)) { theEnd(atStart, result, objTypes, options) } @@ -1067,8 +1073,8 @@ export function kitRoutes(options?: Options // Run the thing at startup { name: 'kit-routes', - buildStart() { - run(true, options) + async buildStart() { + await run(true, options) }, }, @@ -1078,8 +1084,8 @@ export function kitRoutes(options?: Options name: 'kit-routes-watch', logs: [], watch: ['**/+page.svelte', '**/+page.server.ts', '**/+server.ts'], - run: () => { - run(false, options) + run: async () => { + await run(false, options) }, }, ]), diff --git a/packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts b/packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts index 9ef245457..9170862e0 100644 --- a/packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts +++ b/packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts @@ -460,7 +460,7 @@ describe('run()', async () => { for (let i = 0; i < runs.length; i++) { const toRun = runs[i] it(`run ${toRun.pathFile}`, async () => { - const ret = run(false, { + const ret = await run(false, { format: toRun.format, generated_file_path: getPathROUTES(toRun.pathFile), ...toRun.extra, @@ -474,7 +474,7 @@ describe('run()', async () => { const toRun = getToRunShortened(runs[i]) it(`run ${toRun.pathFile}`, async () => { - const ret = run(false, { + const ret = await run(false, { format: toRun.format, generated_file_path: getPathROUTES(toRun.pathFile), ...toRun.extra, @@ -769,9 +769,9 @@ describe('run()', async () => { }) } - it('post_update_run', () => { + it('post_update_run', async () => { const generated_file_path = 'src/test/ROUTES_post-update.ts' - run(false, { + await run(false, { generated_file_path, post_update_run: 'echo done', }) @@ -779,9 +779,9 @@ describe('run()', async () => { expect(true).toBe(true) }) - it('with path base', () => { + it('with path base', async () => { const generated_file_path = 'src/test/ROUTES_base.ts' - run(false, { + await run(false, { generated_file_path, path_base: true, })