Skip to content

Commit c7bea21

Browse files
committed
feat: add project asset copying to web generator
1 parent 01f99f0 commit c7bea21

1 file changed

Lines changed: 37 additions & 1 deletion

File tree

‎src/generators/web/generate.mjs‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,46 @@
11
'use strict';
22

3-
import { readFile } from 'node:fs/promises';
3+
import { readFile, cp, mkdir } from 'node:fs/promises';
44
import { join } from 'node:path';
55

66
import { processJSXEntries } from './utils/processing.mjs';
77
import getConfig from '../../utils/configuration/index.mjs';
88
import { writeFile } from '../../utils/file.mjs';
99

10+
/**
11+
* Aggregates and copies local assets referenced in the markdown ASTs to the output directory.
12+
*
13+
* @param {Array<import('./types').JSXContent>} input - The processed entries containing asset maps
14+
* @param {string} outputDir - The absolute path to the generation output directory
15+
* @returns {Promise<void>}
16+
*/
17+
async function copyProjectAssets(input, outputDir) {
18+
const allAssets = new Map();
19+
20+
for (const entry of input) {
21+
if (entry.assetsMap) {
22+
for (const [source, name] of Object.entries(entry.assetsMap)) {
23+
allAssets.set(source, join(outputDir, 'assets', name));
24+
}
25+
}
26+
}
27+
28+
if (allAssets.size === 0) {
29+
return;
30+
}
31+
32+
const assetsOutputDir = join(outputDir, 'assets');
33+
await mkdir(assetsOutputDir, { recursive: true });
34+
35+
for (const [source, dest] of allAssets.entries()) {
36+
try {
37+
await cp(source, dest, { force: true });
38+
} catch (err) {
39+
console.error(`[doc-kit] Error copying asset: ${source}`, err);
40+
}
41+
}
42+
}
43+
1044
/**
1145
* Main generation function that processes JSX AST entries into web bundles.
1246
*
@@ -34,6 +68,8 @@ export async function generate(input) {
3468

3569
// Write CSS bundle
3670
await writeFile(join(config.output, 'styles.css'), css, 'utf-8');
71+
72+
await copyProjectAssets(input, config.output);
3773
}
3874

3975
return results.map(({ html }) => ({ html: html.toString(), css }));

0 commit comments

Comments
 (0)