Open History to see the last commit
1import { existsSync } from 'node:fs'
2import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
3import { tmpdir } from 'node:os'
4import { join } from 'node:path'
5import { afterEach, describe, expect, it } from 'vitest'
6import { publishStagedDocs } from './publish-staged-docs'
7
8const roots: Array<string> = []
9
10afterEach(async () => {
11 await Promise.all(
12 roots.splice(0).map((root) => rm(root, { recursive: true, force: true })),
13 )
14})
15
16async function makeDirs() {
17 const root = await mkdtemp(join(tmpdir(), 'docs-gen-'))
18 roots.push(root)
19 const stagingDir = join(root, 'staging')
20 const outputDir = join(root, 'output')
21 await mkdir(stagingDir)
22 await mkdir(outputDir)
23 return { stagingDir, outputDir }
24}
25
26describe('publishStagedDocs', () => {
27 it('keeps existing docs when staging has no index.md', async () => {
28 const { stagingDir, outputDir } = await makeDirs()
29 await writeFile(join(outputDir, 'index.md'), 'keep me')
30
31 await expect(publishStagedDocs(stagingDir, outputDir)).rejects.toThrow(
32 /index\.md/,
33 )
34
35 expect(await readFile(join(outputDir, 'index.md'), 'utf8')).toBe('keep me')
36 })
37
38 it('replaces existing docs when staging has index.md', async () => {
39 const { stagingDir, outputDir } = await makeDirs()
40 await writeFile(join(outputDir, 'index.md'), 'old')
41 await writeFile(join(outputDir, 'stale.md'), 'gone')
42 await writeFile(join(stagingDir, 'index.md'), 'new')
43 await writeFile(join(stagingDir, 'chat.md'), 'chat')
44
45 await publishStagedDocs(stagingDir, outputDir)
46
47 expect(await readFile(join(outputDir, 'index.md'), 'utf8')).toBe('new')
48 expect(await readFile(join(outputDir, 'chat.md'), 'utf8')).toBe('chat')
49 await expect(
50 readFile(join(outputDir, 'stale.md'), 'utf8'),
51 ).rejects.toThrow()
52 expect(existsSync(`${outputDir}.next`)).toBe(false)
53 expect(existsSync(stagingDir)).toBe(false)
54 })
55})