Skip to content

Commit

Permalink
fix: fixes generation issue with the Astro srcDir option
Browse files Browse the repository at this point in the history
Co-authored-by: HiDeoo <[email protected]>
  • Loading branch information
charlesLoder and HiDeoo authored Jul 16, 2024
1 parent 0923e6d commit d8c4eaa
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: actions/checkout@v3

- name: Install pnpm
uses: pnpm/action-setup@v2.2.4
uses: pnpm/action-setup@v4
with:
version: 8.6.1

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
fetch-depth: 0

- name: Install pnpm
uses: pnpm/action-setup@v2.2.4
uses: pnpm/action-setup@v4
with:
version: 8.6.1

Expand Down
2 changes: 1 addition & 1 deletion packages/starlight-typedoc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function makeStarlightTypeDocPlugin(sidebarGroup: SidebarGroup): (options: Starl
name: 'starlight-typedoc-plugin',
hooks: {
async setup({ astroConfig, config, logger, updateConfig }) {
const { outputDirectory, reflections } = await generateTypeDoc(options, astroConfig.base, logger)
const { outputDirectory, reflections } = await generateTypeDoc(options, astroConfig, logger)
const sidebar = getSidebarFromReflections(
config.sidebar,
sidebarGroup,
Expand Down
13 changes: 9 additions & 4 deletions packages/starlight-typedoc/libs/typedoc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import fs from 'node:fs'
import path from 'node:path'
import url from 'node:url'

import type { AstroIntegrationLogger } from 'astro'
import type { AstroConfig, AstroIntegrationLogger } from 'astro'
import {
Application,
PageEvent,
Expand Down Expand Up @@ -35,7 +36,11 @@ const markdownPluginConfig: TypeDocConfig = {
hidePageTitle: true,
}

export async function generateTypeDoc(options: StarlightTypeDocOptions, base: string, logger: AstroIntegrationLogger) {
export async function generateTypeDoc(
options: StarlightTypeDocOptions,
config: AstroConfig,
logger: AstroIntegrationLogger,
) {
const outputDirectory = options.output ?? 'api'

const app = await bootstrapApp(
Expand All @@ -44,7 +49,7 @@ export async function generateTypeDoc(options: StarlightTypeDocOptions, base: st
options.typeDoc,
outputDirectory,
options.pagination ?? false,
base,
config.base,
logger,
)
const reflections = await app.convert()
Expand All @@ -56,7 +61,7 @@ export async function generateTypeDoc(options: StarlightTypeDocOptions, base: st
throw new Error('Failed to generate TypeDoc documentation.')
}

const outputPath = path.join('src/content/docs', outputDirectory)
const outputPath = path.join(url.fileURLToPath(config.srcDir), 'content/docs', outputDirectory)

if (options.watch) {
app.convertAndWatch(async (reflections) => {
Expand Down
29 changes: 26 additions & 3 deletions packages/starlight-typedoc/tests/unit/typedoc.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'node:fs'

import type { AstroIntegrationLogger } from 'astro'
import type { AstroIntegrationLogger, AstroConfig } from 'astro'
import { afterAll, afterEach, beforeAll, expect, test, vi } from 'vitest'

import type { StarlightTypeDocOptions } from '../..'
Expand All @@ -13,6 +13,11 @@ const starlightTypeDocOptions = {
},
} satisfies Partial<StarlightTypeDocOptions>

const starlightTypeDocAstroConfig: Partial<AstroConfig> = {
// './src' is the default value supplied by Astro — https://docs.astro.build/en/reference/configuration-reference/#srcdir
srcDir: new URL('src', import.meta.url),
}

beforeAll(() => {
vi.spyOn(fs, 'mkdirSync').mockReturnValue(undefined)
vi.spyOn(fs, 'rmSync').mockReturnValue(undefined)
Expand Down Expand Up @@ -67,6 +72,21 @@ test('should generate the doc in `src/content/docs/api` by default', async () =>
expect(mkdirSyncSpy.mock.calls[0]?.[0].toString()).toMatch(/src[/\\]content[/\\]docs[/\\]api$/)
})

test('should generate the doc in `/content/docs/api` of the srcDir via the AstroConfig', async () => {
await generateTestTypeDoc(
{
...starlightTypeDocOptions,
entryPoints: ['../../fixtures/basics/src/functions.ts'],
},
{ srcDir: new URL('www/src', import.meta.url) },
)

const mkdirSyncSpy = vi.mocked(fs.mkdirSync)

expect(mkdirSyncSpy).toHaveBeenCalled()
expect(mkdirSyncSpy.mock.calls[0]?.[0].toString()).toMatch(/www[/\\]src[/\\]content[/\\]docs[/\\]api$/)
})

test('should generate the doc in a custom output directory relative to `src/content/docs/`', async () => {
const output = 'dist-api'

Expand Down Expand Up @@ -165,13 +185,16 @@ test('should output index with correct module path', async () => {
).toBe(true)
})

function generateTestTypeDoc(options: Parameters<typeof generateTypeDoc>[0]) {
function generateTestTypeDoc(
options: Parameters<typeof generateTypeDoc>[0],
config: Partial<AstroConfig> = starlightTypeDocAstroConfig,
) {
return generateTypeDoc(
{
...starlightTypeDocOptions,
...options,
},
'/',
config as AstroConfig,
{
info() {
// noop
Expand Down

0 comments on commit d8c4eaa

Please sign in to comment.