-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
Copy pathprepare-build.mjs
57 lines (50 loc) · 1.55 KB
/
prepare-build.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//@ts-check
import nodeFs from 'node:fs/promises';
import nodePath from 'node:path';
await collectAndCopyDirToAssets('./pages');
await collectAndCopyDirToAssets('./snippets');
/**
* @param {string} path
* @returns {Promise<void>}
*/
async function collectAndCopyDirToAssets(path) {
await nodeFs.cp(path, nodePath.join('./.open-next/assets', path), {
recursive: true,
force: true,
});
const pagesChildren = await collectDirChildren(path);
await nodeFs.mkdir('./.cloudflare/.asset-manifests/', { recursive: true });
await nodeFs.writeFile(
`./.cloudflare/.asset-manifests/${nodePath.basename(path)}.mjs`,
`export default ${JSON.stringify(pagesChildren)}`
);
}
/**
* @param {string} path
* @returns {Promise<DirentLike[]>}
*/
async function collectDirChildren(path) {
const dirContent = await nodeFs.readdir(path, { withFileTypes: true });
return Promise.all(
dirContent.map(async item => {
const base = {
name: item.name,
parentPath: item.parentPath,
};
if (item.isFile()) {
return { ...base, type: 'file' };
} else {
const dirInfo = await collectDirChildren(
`${item.parentPath}/${item.name}`
);
return { ...base, type: 'directory', children: dirInfo };
}
})
);
}
/**
* @typedef {{ name: string, parentPath: string } } DirentLikeBase
* @typedef {DirentLikeBase & { type: 'file' }} DirentLikeFile
* @typedef {DirentLikeBase & { type: 'directory', children: DirentLike[] }} DirentLikeDir
* @typedef {DirentLikeFile|DirentLikeDir} DirentLike
*/