import { existsSync, readdirSync, readFileSync } from 'node:fs' import path from 'node:path' import { describe, expect, it } from 'vitest' const ROOT = path.resolve(import.meta.dirname, '../..') const DOCS_ORIGIN = 'https://ecosystem.vuejs.press/' /** The workspace directories that hold published packages */ const PACKAGE_DIRS = ['plugins', 'themes', 'tools'] /** Directories that never hold a workspace package */ const IGNORED_DIRS = new Set(['dist', 'node_modules']) interface HomepageEntry { /** The package name, falling back to the directory when the manifest has none */ name: string /** The package directory relative to the repository root */ dir: string homepage: string } /** * How a homepage relates to the documentation site * * - `external`: another origin, there is no local page to verify. * - `mapped`: the matching pages, at least one of which must exist. * - `unmappable`: a documentation URL whose shape is not a known page route. */ type HomepageTarget = | { kind: 'external' } | { kind: 'mapped'; pages: string[] } | { kind: 'unmappable'; route: string } /** * Turn a package homepage into the documentation pages that it must resolve to * * A page route ends either with `.html`, as written in the built site, or with * a trailing slash, and both layouts are used by the pages of this repository, * so a slash route accepts either `README.md` or a sibling `.md`. * * @param homepage - The homepage field of a package manifest / 包清单的首页字段 * @returns The pages to check, or why they cannot be checked / * 要检查的页面,或无法检查的原因 */ const resolveHomepageTarget = (homepage: string): HomepageTarget => { if (!homepage.startsWith(DOCS_ORIGIN)) return { kind: 'external' } const route = homepage.slice(DOCS_ORIGIN.length) if (route.endsWith('.html')) { return { kind: 'mapped', pages: [`docs/${route.slice(0, -'.html'.length)}.md`], } } if (route.endsWith('/')) { return { kind: 'mapped', pages: [`docs/${route}README.md`, `docs/${route.slice(0, -1)}.md`], } } return { kind: 'unmappable', route } } /** * Collect the `package.json` files of the workspace packages * * @param dir - The directory to scan, relative to the repository root / * 要扫描的目录(相对于仓库根目录) * @returns The manifest paths, relative to the repository root / * 清单路径(相对于仓库根目录) */ const findManifests = (dir: string): string[] => { const entries = readdirSync(path.join(ROOT, dir), { withFileTypes: true }) return entries.flatMap((entry) => { if (IGNORED_DIRS.has(entry.name)) return [] const file = `${dir}/${entry.name}` if (entry.isDirectory()) return findManifests(file) return entry.name === 'package.json' ? [file] : [] }) } /** * Read the homepages of every workspace package that declares one * * @returns The homepages along with the package that declares them / * 首页字段及其所属包 */ const collectHomepages = (): HomepageEntry[] => PACKAGE_DIRS.flatMap((dir) => findManifests(dir)) .flatMap((file) => { const manifest = JSON.parse( readFileSync(path.join(ROOT, file), 'utf8'), ) as { name?: string homepage?: string } if (!manifest.homepage) return [] const dir = path.dirname(file) return [{ name: manifest.name ?? dir, dir, homepage: manifest.homepage }] }) .sort((a, b) => a.dir.localeCompare(b.dir)) /** * Describe why a homepage does not resolve to a documentation page * * @param entry - The package whose homepage is checked / 要检查首页的包 * @returns The failure messages, empty when the homepage is valid / * 失败信息,首页有效时为空数组 */ const findHomepageFailures = (entry: HomepageEntry): string[] => { const { name, dir, homepage } = entry const target = resolveHomepageTarget(homepage) if (target.kind === 'external') return [] if (target.kind === 'unmappable') { return [ `${name} (${dir}) → ${homepage} → cannot be mapped to a page: "${target.route}" ends with neither ".html" nor "/"`, ] } return target.pages.some((page) => existsSync(path.join(ROOT, page))) ? [] : [`${name} (${dir}) → ${homepage} → ${target.pages.join(' or ')}`] } describe('package homepages', () => { describe('resolveHomepageTarget()', () => { it('maps an .html route to the Markdown page', () => { expect( resolveHomepageTarget( 'https://ecosystem.vuejs.press/plugins/blog/blog.html', ), ).toStrictEqual({ kind: 'mapped', pages: ['docs/plugins/blog/blog.md'], }) }) it('accepts both layouts of a slash route', () => { expect( resolveHomepageTarget('https://ecosystem.vuejs.press/themes/default/'), ).toStrictEqual({ kind: 'mapped', pages: ['docs/themes/default/README.md', 'docs/themes/default.md'], }) }) it('skips every other origin', () => { expect( resolveHomepageTarget('https://github.com/vuepress'), ).toStrictEqual({ kind: 'external', }) }) it('reports a documentation URL that cannot be mapped', () => { expect( resolveHomepageTarget('https://ecosystem.vuejs.press/plugins/blog'), ).toStrictEqual({ kind: 'unmappable', route: 'plugins/blog' }) }) }) describe('findHomepageFailures()', () => { it('accepts a homepage whose page exists', () => { expect( findHomepageFailures({ name: '@vuepress/plugin-blog', dir: 'plugins/blog/plugin-blog', homepage: 'https://ecosystem.vuejs.press/plugins/blog/blog.html', }), ).toStrictEqual([]) }) it('names the package, the homepage and the expected page', () => { expect( findHomepageFailures({ name: '@vuepress/plugin-blog', dir: 'plugins/blog/plugin-blog', homepage: 'https://ecosystem.vuejs.press/blog/plugins/blog.html', }), ).toStrictEqual([ '@vuepress/plugin-blog (plugins/blog/plugin-blog) → https://ecosystem.vuejs.press/blog/plugins/blog.html → docs/blog/plugins/blog.md', ]) }) it('skips a homepage that points at another origin', () => { expect( findHomepageFailures({ name: 'vp-update', dir: 'tools/vp-update', homepage: 'https://github.com/vuepress', }), ).toStrictEqual([]) }) }) it('scans the real package directories', () => { const entries = collectHomepages() expect(entries.length).toBeGreaterThan(60) expect(entries.map(({ name }) => name)).toContain('@vuepress/plugin-blog') // A fixture manifest must never be picked up as a package expect(entries.map(({ dir }) => dir)).not.toContain( 'plugins/markdown/plugin-markdown-file-tree/tests/__fixtures__/embed', ) }) it('points every homepage at an existing documentation page', () => { const failures = collectHomepages().flatMap((entry) => findHomepageFailures(entry), ) expect( failures, 'The homepage of these packages does not resolve to a documentation page.', ).toStrictEqual([]) }) })