diff --git a/utils/docs-api.test.ts b/utils/docs-api.test.ts index 283ae4c..d1b278c 100644 --- a/utils/docs-api.test.ts +++ b/utils/docs-api.test.ts @@ -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') }) + } + }) }) diff --git a/utils/docs-api.ts b/utils/docs-api.ts index cff4e08..319cfe5 100644 --- a/utils/docs-api.ts +++ b/utils/docs-api.ts @@ -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 { 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 })) }