diff --git a/src/utils.ts b/src/utils.ts index 26bfb01..9e1d891 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -25,7 +25,9 @@ export function formatPagePath(root: string, path: string) { export function loadPagesJson(path: string, rootPath: string): string[] { const pagesJsonRaw = readFileSync(path, 'utf-8') - const { pages = [], subPackages = [] } = jsonParse(pagesJsonRaw) + const pagesJson = jsonParse(pagesJsonRaw) + const { pages = [] } = pagesJson + const subPackages = pagesJson.subPackages ?? pagesJson.subpackages ?? [] return [ ...pages diff --git a/test/transform.test.ts b/test/transform.test.ts index e69de29..0e78514 100644 --- a/test/transform.test.ts +++ b/test/transform.test.ts @@ -0,0 +1,34 @@ +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' + +import { afterEach, describe, expect, it } from 'vitest' + +import { loadPagesJson } from '../src/utils' + +const tempDirectories: string[] = [] + +afterEach(() => { + tempDirectories.splice(0).forEach(directory => rmSync(directory, { recursive: true, force: true })) +}) + +describe('loadPagesJson', () => { + it.each(['subPackages', 'subpackages'])('loads pages from %s', (subPackagesKey) => { + const directory = mkdtempSync(join(tmpdir(), 'uni-ku-root-')) + const pagesJsonPath = join(directory, 'pages.json') + tempDirectories.push(directory) + + writeFileSync(pagesJsonPath, JSON.stringify({ + pages: [{ path: 'pages/index' }], + [subPackagesKey]: [{ + root: 'packages/example', + pages: [{ path: 'pages/detail' }], + }], + })) + + expect(loadPagesJson(pagesJsonPath, '/project/src')).toEqual([ + '/project/src/pages/index.vue', + '/project/src/packages/example/pages/detail.vue', + ]) + }) +})