Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions utils/docs-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,17 @@ describe('docs api', () => {
expect(item).toEqual({ path: item.path, type: 'blob', sha: blobSha(raw) })
}
})

it('bundles every source with the sha git would give it', async () => {
const outDir = await mkdtemp(path.join(tmpdir(), 'docs-api-'))
outDirs.push(outDir)
await publishSources('.', outDir, ['index.md', 'about/index.md'])

const bundle = JSON.parse(await readFile(path.join(outDir, 'docs-api/bundle.json'), 'utf8'))
expect(bundle.files.map((file: { path: string }) => file.path)).toEqual(['about/index.md', 'index.md'])
for (const file of bundle.files) {
const expected = execFileSync('git', ['hash-object', file.path], { encoding: 'utf8' }).trim()
expect(file).toEqual({ path: file.path, sha: expected, content: await readFile(file.path, 'utf8') })
}
})
})
6 changes: 4 additions & 2 deletions utils/docs-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ export function blobSha(bytes: Uint8Array): string {
// Same shape as GitHub's recursive tree response, so @nbtca/docs reads both with one parser.
export async function publishSources(srcDir: string, outDir: string, pages: string[]): Promise<void> {
const root = path.join(outDir, 'docs-api')
const tree = await Promise.all([...pages].sort().map(async (page) => {
const files = await Promise.all([...pages].sort().map(async (page) => {
const bytes = await readFile(path.join(srcDir, page))
const target = path.join(root, 'raw', page)
await mkdir(path.dirname(target), { recursive: true })
await writeFile(target, bytes)
return { path: page, type: 'blob', sha: blobSha(bytes) }
return { path: page, sha: blobSha(bytes), content: bytes.toString('utf8') }
}))
const tree = files.map(file => ({ path: file.path, type: 'blob', sha: file.sha }))
await writeFile(path.join(root, 'index.json'), JSON.stringify({ tree, truncated: false }))
await writeFile(path.join(root, 'bundle.json'), JSON.stringify({ files }))
}
Loading