Skip to content

Commit

Permalink
feat: add support for packages entry point strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
HiDeoo committed Jan 3, 2024
1 parent 037b219 commit 89552ec
Show file tree
Hide file tree
Showing 27 changed files with 311 additions and 30 deletions.
29 changes: 29 additions & 0 deletions example/astro.packages-entrypoints.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import starlight from '@astrojs/starlight'
import { defineConfig } from 'astro/config'
import starlightTypeDoc, { typeDocSidebarGroup } from 'starlight-typedoc'

export default defineConfig({
base: '/packages-entrypoints/',
integrations: [
starlight({
plugins: [
starlightTypeDoc({
entryPoints: ['../fixtures/packages/packages/*'],
output: 'api-packages-entrypoints',
tsconfig: '../fixtures/packages/tsconfig.json',
typeDoc: {
entryPointStrategy: 'packages',
},
}),
],
sidebar: [
{
label: 'Guides',
items: [{ label: 'Example Guide', link: '/guides/example/' }],
},
typeDocSidebarGroup,
],
title: 'Starlight TypeDoc Packages Entry Points Example',
}),
],
})
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"dev": "astro dev",
"dev:single-entrypoints": "astro dev --config astro.config.ts",
"dev:multiple-entrypoints": "astro dev --config astro.multiple-entrypoints.config.ts",
"dev:packages-entrypoints": "pnpm -C ../fixtures/packages run build && astro dev --config astro.packages-entrypoints.config.ts",
"start": "astro dev",
"build": "astro build",
"preview": "astro preview",
Expand Down
1 change: 1 addition & 0 deletions example/src/content/docs/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Autogenerated by https://github.com/HiDeoo/starlight-typedoc
api/
api-multiple-entrypoints/
api-packages-entrypoints/
34 changes: 34 additions & 0 deletions fixtures/packages/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@starlight-typedoc/fixtures-packages",
"version": "0.0.1",
"license": "MIT",
"description": "Starlight plugin to generate documentation from TypeScript using TypeDoc.",
"author": "HiDeoo <github@hideoo.dev> (https://hideoo.dev)",
"type": "module",
"scripts": {
"build": "tsc --build"
},
"dependencies": {
"typescript": "5.1.6"
},
"engines": {
"node": ">=18.14.1"
},
"packageManager": "pnpm@8.6.1",
"private": true,
"sideEffects": false,
"keywords": [
"starlight",
"plugin",
"typedoc",
"typescript",
"documentation",
"astro"
],
"homepage": "https://github.com/HiDeoo/starlight-typedoc",
"repository": {
"type": "git",
"url": "https://github.com/HiDeoo/starlight-typedoc.git"
},
"bugs": "https://github.com/HiDeoo/starlight-typedoc/issues"
}
25 changes: 25 additions & 0 deletions fixtures/packages/packages/bar/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "bar",
"version": "0.0.1",
"license": "MIT",
"description": "Starlight plugin to generate documentation from TypeScript using TypeDoc.",
"author": "HiDeoo <github@hideoo.dev> (https://hideoo.dev)",
"type": "module",
"packageManager": "pnpm@8.6.1",
"private": true,
"sideEffects": false,
"keywords": [
"starlight",
"plugin",
"typedoc",
"typescript",
"documentation",
"astro"
],
"homepage": "https://github.com/HiDeoo/starlight-typedoc",
"repository": {
"type": "git",
"url": "https://github.com/HiDeoo/starlight-typedoc.git"
},
"bugs": "https://github.com/HiDeoo/starlight-typedoc/issues"
}
6 changes: 6 additions & 0 deletions fixtures/packages/packages/bar/src/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* A function that does a bar thing.
*/
export function doBar() {
return 'doBar'
}
1 change: 1 addition & 0 deletions fixtures/packages/packages/bar/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './functions.js'
8 changes: 8 additions & 0 deletions fixtures/packages/packages/bar/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}
3 changes: 3 additions & 0 deletions fixtures/packages/packages/bar/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"entryPoints": ["src/index.ts"]
}
25 changes: 25 additions & 0 deletions fixtures/packages/packages/foo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "foo",
"version": "0.0.1",
"license": "MIT",
"description": "Starlight plugin to generate documentation from TypeScript using TypeDoc.",
"author": "HiDeoo <github@hideoo.dev> (https://hideoo.dev)",
"type": "module",
"packageManager": "pnpm@8.6.1",
"private": true,
"sideEffects": false,
"keywords": [
"starlight",
"plugin",
"typedoc",
"typescript",
"documentation",
"astro"
],
"homepage": "https://github.com/HiDeoo/starlight-typedoc",
"repository": {
"type": "git",
"url": "https://github.com/HiDeoo/starlight-typedoc.git"
},
"bugs": "https://github.com/HiDeoo/starlight-typedoc/issues"
}
16 changes: 16 additions & 0 deletions fixtures/packages/packages/foo/src/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* A function that does a foo thing.
* @deprecated Use the new {@link doFooFaster} function instead.
*/
export function doFoo() {
return 'doFoo'
}

/**
* A function that does another foo thing but faster.
*
* This is a faster alternative to {@link doFoo}.
*/
export function doFooFaster() {
return 'doFoo'
}
1 change: 1 addition & 0 deletions fixtures/packages/packages/foo/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './functions.js'
8 changes: 8 additions & 0 deletions fixtures/packages/packages/foo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}
3 changes: 3 additions & 0 deletions fixtures/packages/packages/foo/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"entryPoints": ["src/index.ts"]
}
9 changes: 9 additions & 0 deletions fixtures/packages/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"composite": true,
"declaration": true,
"declarationMap": true,
"module": "NodeNext",
"strict": true
}
}
11 changes: 11 additions & 0 deletions fixtures/packages/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"files": [],
"references": [
{
"path": "./packages/bar"
},
{
"path": "./packages/foo"
}
]
}
34 changes: 31 additions & 3 deletions packages/starlight-typedoc/libs/starlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'node:path'

import type { StarlightPlugin } from '@astrojs/starlight/types'
import { slug } from 'github-slugger'
import type { DeclarationReflection, ProjectReflection } from 'typedoc'
import { type DeclarationReflection, type ProjectReflection, ReflectionKind } from 'typedoc'

import type { StarlightTypeDocSidebarOptions } from '..'

Expand Down Expand Up @@ -54,15 +54,42 @@ export function getSidebarFromReflections(
})
}

function getSidebarGroupFromPackageReflections(
options: StarlightTypeDocSidebarOptions,
reflections: ProjectReflection | DeclarationReflection,
outputDirectory: string,
): SidebarGroup {
const groups = (reflections.children ?? []).map((child) => {
if (!child.url) {
return undefined
}

const url = path.parse(child.url)

return getSidebarGroupFromReflections(options, child, `${outputDirectory}/${url.dir}`, child.name)
})

return {
label: options.label ?? sidebarDefaultOptions.label,
collapsed: options.collapsed ?? sidebarDefaultOptions.collapsed,
items: groups.filter((item): item is SidebarGroup => item !== undefined),
}
}

function getSidebarGroupFromReflections(
options: StarlightTypeDocSidebarOptions,
reflections: ProjectReflection | DeclarationReflection,
outputDirectory: string,
label?: string,
): SidebarGroup {
if ((!reflections.groups || reflections.groups.length === 0) && reflections.children) {
return getSidebarGroupFromPackageReflections(options, reflections, outputDirectory)
}

const groups = reflections.groups ?? []

return {
label: options.label ?? sidebarDefaultOptions.label,
label: label ?? options.label ?? sidebarDefaultOptions.label,
collapsed: options.collapsed ?? sidebarDefaultOptions.collapsed,
items: groups
.flatMap((group) => {
Expand All @@ -73,11 +100,12 @@ function getSidebarGroupFromReflections(
}

const url = path.parse(child.url)
const isParentKindModule = child.parent?.kind === ReflectionKind.Module

return getSidebarGroupFromReflections(
{ collapsed: true, label: child.name },
child,
`${outputDirectory}/${url.dir}`,
`${outputDirectory}/${isParentKindModule ? url.dir.split('/').slice(1).join('/') : url.dir}`,
)
})
}
Expand Down
5 changes: 4 additions & 1 deletion packages/starlight-typedoc/libs/typedoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export async function generateTypeDoc(options: StarlightTypeDocOptions, base: st
)
const reflections = await app.convert()

if (!reflections?.groups || reflections.groups.length === 0) {
if (
(!reflections?.groups || reflections.groups.length === 0) &&
!reflections?.children?.some((child) => (child.groups ?? []).length > 0)
) {
throw new Error('Failed to generate TypeDoc documentation.')
}

Expand Down
5 changes: 4 additions & 1 deletion packages/starlight-typedoc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
"scripts": {
"test": "pnpm test:unit && pnpm test:e2e",
"test:unit": "vitest",
"test:e2e": "playwright install --with-deps chromium && playwright test",
"test:e2e": "pnpm run test:e2e:basics && pnpm run test:e2e:packages",
"test:e2e:basics": "TEST_TYPE=basics pnpm run playwright",
"test:e2e:packages": "TEST_TYPE=packages pnpm run playwright",
"playwright": "playwright install --with-deps chromium && playwright test",
"lint": "prettier -c --cache . && eslint . --cache --max-warnings=0"
},
"dependencies": {
Expand Down
40 changes: 25 additions & 15 deletions packages/starlight-typedoc/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,29 @@ export default defineConfig({
use: { ...devices['Desktop Chrome'] },
},
],
testDir: 'tests/e2e',
webServer: [
{
command: 'pnpm run dev:single-entrypoints',
cwd: '../../example',
reuseExistingServer: !process.env['CI'],
url: 'http://localhost:4321',
},
{
command: 'pnpm run dev:multiple-entrypoints',
cwd: '../../example',
reuseExistingServer: !process.env['CI'],
url: 'http://localhost:4322/multiple-entrypoints/',
},
],
testDir: `tests/e2e/${process.env['TEST_TYPE']}`,
webServer:
process.env['TEST_TYPE'] === 'basics'
? [
{
command: 'pnpm run dev:single-entrypoints',
cwd: '../../example',
reuseExistingServer: !process.env['CI'],
url: 'http://localhost:4321',
},
{
command: 'pnpm run dev:multiple-entrypoints',
cwd: '../../example',
reuseExistingServer: !process.env['CI'],
url: 'http://localhost:4322/multiple-entrypoints/',
},
]
: [
{
command: 'pnpm run dev:packages-entrypoints',
cwd: '../../example',
reuseExistingServer: !process.env['CI'],
url: 'http://localhost:4321/packages-entrypoints/',
},
],
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { DocPage } from './fixtures/DocPage'
import { expect, test } from './test'
import type { DocPage } from '../fixtures/DocPage'
import { expect, test } from '../test'

test('should use an aside for the deprecated tag with no content', async ({ docPage }) => {
await docPage.goto('functions/dothingb')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, test } from './test'
import { expect, test } from '../test'

test('should add titles to the frontmatter', async ({ docPage }) => {
await docPage.goto('classes/foo')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { DocPage } from './fixtures/DocPage'
import { expect, test } from './test'
import type { DocPage } from '../fixtures/DocPage'
import { expect, test } from '../test'

test('should not include pagination links by default', async ({ docPage }) => {
await docPage.goto('classes/foo')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, test } from './test'
import { expect, test } from '../test'

const singleEntrypointUrl = 'classes/foo'
const multipleEntrypointsUrl = 'bar/classes/bar'
Expand Down
Loading

0 comments on commit 89552ec

Please sign in to comment.