|
| 1 | +import { copy } from "fs-extra/esm"; |
| 2 | +import { glob } from "tinyglobby"; |
| 3 | +import { logger } from "../../utils/logger.js"; |
| 4 | +import { toArray } from "../../utils/string.js"; |
| 5 | + |
| 6 | +// We should ignore node_modules/ by default, glob this folder will be very slow |
| 7 | +const DEFAULT_IGNORE = ["node_modules", ".git"]; |
| 8 | + |
| 9 | +// Copys files in `Config.build.assets` to `Config.dist` |
| 10 | +export default async function copyAssets( |
| 11 | + source: string | string[], |
| 12 | + dist: string, |
| 13 | + assets: string | string[], |
| 14 | +) { |
| 15 | + const sourceArr = toArray(source); |
| 16 | + const paths = await glob(assets, { |
| 17 | + ignore: [...DEFAULT_IGNORE, dist], |
| 18 | + }); |
| 19 | + |
| 20 | + for (const file of paths) { |
| 21 | + const newPath = getNewPath(sourceArr, dist, file); |
| 22 | + await copy(file, newPath); |
| 23 | + logger.debug(`Copy ${file} to ${newPath}`); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +export function getNewPath(sources: string[], dist: string, path: string): string { |
| 28 | + const pattern = new RegExp(sources.join("|")); |
| 29 | + const relativePath = path.replace(pattern, ""); |
| 30 | + return `${dist}/addon/${relativePath}`; |
| 31 | +} |
0 commit comments