From da7dfc3601d48e542772e05a38f362c2ebe37f46 Mon Sep 17 00:00:00 2001 From: "jyc.dev" Date: Sat, 26 Apr 2025 12:07:40 +0200 Subject: [PATCH] Bin kit routes v5 (#988) * :construction: WIP: tweak message & add tests * tweaks logs & configs * update tests --- .changeset/bitter-friends-exist.md | 5 ++ .../src/lib/bin.spec.ts | 88 +++++++++++++++++++ .../vite-plugin-kit-routes/src/lib/bin.ts | 53 +++++++---- .../src/lib/plugins.spec.ts | 1 + .../src/test/vite.config.noexport.ts | 17 ++++ .../src/test/vite.config.noexportDefault.ts | 2 + 6 files changed, 150 insertions(+), 16 deletions(-) create mode 100644 .changeset/bitter-friends-exist.md create mode 100644 packages/vite-plugin-kit-routes/src/lib/bin.spec.ts create mode 100644 packages/vite-plugin-kit-routes/src/test/vite.config.noexport.ts create mode 100644 packages/vite-plugin-kit-routes/src/test/vite.config.noexportDefault.ts diff --git a/.changeset/bitter-friends-exist.md b/.changeset/bitter-friends-exist.md new file mode 100644 index 00000000..bc712ce2 --- /dev/null +++ b/.changeset/bitter-friends-exist.md @@ -0,0 +1,5 @@ +--- +'vite-plugin-kit-routes': patch +--- + +update logs on wrong config diff --git a/packages/vite-plugin-kit-routes/src/lib/bin.spec.ts b/packages/vite-plugin-kit-routes/src/lib/bin.spec.ts new file mode 100644 index 00000000..249c18bf --- /dev/null +++ b/packages/vite-plugin-kit-routes/src/lib/bin.spec.ts @@ -0,0 +1,88 @@ +import { execSync } from 'child_process' +import path from 'node:path' +import { describe, expect, it, suite } from 'vitest' + +import { read } from '@kitql/internals' + +const binPath = path.resolve(__dirname, 'bin.ts') + +describe('bin', () => { + suite('Working Happy Paths', () => { + it('should show help when no command is provided', () => { + try { + execSync(`tsx --tsconfig .svelte-kit/tsconfig.json ${binPath}`, {}) + } catch (error) { + if (error instanceof Error) { + expect(error.message).toContain('Command failed') + expect(error.message).toContain('Usage: kit-routes [options] [command]') + expect(error.message).toContain('CLI for kit-routes plugin') + } else { + expect('To never').toBe('be here') + } + } + }) + + it('should generate ROUTES.ts with sync command', () => { + execSync(`tsx --tsconfig .svelte-kit/tsconfig.json ${binPath} sync`, {}) + const output = read(path.resolve(__dirname, 'ROUTES.ts')) + expect(output).toContain(`This file was generated by 'vite-plugin-kit-routes'`) + }) + + it('should generate ROUTES.ts with sync command and full config options', () => { + execSync( + `tsx --tsconfig .svelte-kit/tsconfig.json ${binPath} sync --config vite.config.ts#_kitRoutesConfig`, + {}, + ) + const output = read(path.resolve(__dirname, 'ROUTES.ts')) + expect(output).toContain(`This file was generated by 'vite-plugin-kit-routes'`) + }) + }) + + suite('Working Error Paths', () => { + it('no export default', () => { + try { + execSync( + `tsx --tsconfig .svelte-kit/tsconfig.json ${binPath} sync --config ./src/test/vite.config.noexportDefault.ts`, + {}, + ) + } catch (error) { + if (error instanceof Error) { + expect(error.message).toContain('Missing') + expect(error.message).toContain('export default { ... }') + expect(error.message).toContain("or it's not a valid kit-routes config object") + } else { + expect('To never').toBe('be here') + } + } + }) + + it('no good default export', () => { + try { + execSync(`tsx --tsconfig .svelte-kit/tsconfig.json ${binPath} sync --config vite.config.ts`, {}) + } catch (error) { + if (error instanceof Error) { + expect(error.message).toContain('Missing') + expect(error.message).toContain("or it's not a valid kit-routes config object") + } else { + expect('To never').toBe('be here') + } + } + }) + + it('no _plop export', () => { + try { + execSync( + `tsx --tsconfig .svelte-kit/tsconfig.json ${binPath} sync --config ./src/test/vite.config.noexport.ts#_plop`, + {}, + ) + } catch (error) { + if (error instanceof Error) { + expect(error.message).toContain('Missing') + expect(error.message).toContain('export const _plop') + } else { + expect('To never').toBe('be here') + } + } + }) + }) +}) diff --git a/packages/vite-plugin-kit-routes/src/lib/bin.ts b/packages/vite-plugin-kit-routes/src/lib/bin.ts index 0163acce..712ef05a 100644 --- a/packages/vite-plugin-kit-routes/src/lib/bin.ts +++ b/packages/vite-plugin-kit-routes/src/lib/bin.ts @@ -2,7 +2,7 @@ import path from 'node:path' import { Command } from 'commander' -import { green, Log } from '@kitql/helpers' +import { cyan, gray, green, Log, red } from '@kitql/helpers' import { getRelativePackagePath, read } from '@kitql/internals' import { evaluateNode, getExportsFromFile } from './ast.js' @@ -11,28 +11,49 @@ import { run } from './plugin.js' const program = new Command() const log = new Log('kit-routes') -async function loadConfigFromFile(filePath: string, exportName?: string) { +async function loadConfigFromFile( + filePath: string, + exportName?: string, +): Promise<{ status: 'NoFile' | 'NoExport' | 'Invalid' | 'InvalidObject' | 'Valid'; result: any }> { try { const resolvedPath = path.resolve(process.cwd(), filePath) + + const logError = () => { + if (exportName) { + log.error(`Missing "${red(`export const ${exportName}`)}" in '${cyan(resolvedPath)}'`) + } else { + log.error(`Missing "${red(`export default { ... }`)}" in '${cyan(resolvedPath)}' +${gray("(or it's not a valid kit-routes config object)")}`) + } + } + const code = read(resolvedPath) if (!code) { log.error(`Could not read file: ${resolvedPath}`) - return null + return { status: 'NoFile', result: null } } - const result = evaluateNode(getExportsFromFile(code, exportName)) - if (!result) { - if (exportName) { - log.error(`There is no 'export const ${exportName}' in '${filePath}'`) - } else { - log.error(`There is no default export in '${resolvedPath}'`) - } - return null + const exported = getExportsFromFile(code, exportName) + if (!exported) { + logError() + return { status: 'NoExport', result: null } + } + + const result = evaluateNode(exported) + + let isValidResult = true + if (result['callee']) { + isValidResult = false } - return result + if (!result || !isValidResult) { + logError() + return { status: 'InvalidObject', result: null } + } + + return { status: 'Valid', result } } catch (error) { - return null + return { status: 'Invalid', result: null } } } @@ -43,14 +64,14 @@ async function loadConfig(configPath?: string) { const [filePath, local_exportName] = configPath.split('#') const userConfig = await loadConfigFromFile(filePath, local_exportName) exportName = local_exportName - if (userConfig) return userConfig - // If config set, but not found, return null + if (userConfig.status === 'Valid') return userConfig.result return null } // Try vite.config.ts with _kitRoutesConfig const tsConfig = await loadConfigFromFile('vite.config.ts', exportName) - if (tsConfig) return tsConfig + if (tsConfig.status === 'Valid') return tsConfig.result + if (tsConfig.status === 'NoExport') return null // Try vite.config.js with _kitRoutesConfig const jsConfig = await loadConfigFromFile('vite.config.js', exportName) 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 07c1fa90..4fbcb6e3 100644 --- a/packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts +++ b/packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts @@ -309,6 +309,7 @@ describe('getFilesUnder', () => { "ROUTES.ts", "ast.spec.ts", "ast.ts", + "bin.spec.ts", "bin.ts", "format.ts", "index.ts", diff --git a/packages/vite-plugin-kit-routes/src/test/vite.config.noexport.ts b/packages/vite-plugin-kit-routes/src/test/vite.config.noexport.ts new file mode 100644 index 00000000..e84f3e60 --- /dev/null +++ b/packages/vite-plugin-kit-routes/src/test/vite.config.noexport.ts @@ -0,0 +1,17 @@ +import { sveltekit } from '@sveltejs/kit/vite' +import { defineConfig } from 'vitest/config' + +import type { KIT_ROUTES } from '$lib/ROUTES.js' + +import { kitRoutes } from '../../src/lib/index.js' + +export default defineConfig({ + plugins: [ + sveltekit(), + // demo + kitRoutes(), + ], + test: { + include: ['src/**/*.spec.ts'], + }, +}) diff --git a/packages/vite-plugin-kit-routes/src/test/vite.config.noexportDefault.ts b/packages/vite-plugin-kit-routes/src/test/vite.config.noexportDefault.ts new file mode 100644 index 00000000..c4227696 --- /dev/null +++ b/packages/vite-plugin-kit-routes/src/test/vite.config.noexportDefault.ts @@ -0,0 +1,2 @@ +// Needed for tests ;) +console.info('hello') -- 2.51.2