| 12 | const identity = (x: string) => x |
| 13 | |
| 14 | export const copy = async ( |
| 15 | src: string | string[], |
| 16 | dest: string, |
| 17 | { cwd, rename = identity, parents = true }: CopyOption = {} |
| 18 | ) => { |
| 19 | const source = typeof src === 'string' ? [src] : src |
| 20 | |
| 21 | if (source.length === 0 || !dest) { |
| 22 | throw new TypeError('`src` and `dest` are required') |
| 23 | } |
| 24 | |
| 25 | const sourceFiles = await glob(source, { |
| 26 | cwd, |
| 27 | dot: true, |
| 28 | absolute: false, |
| 29 | stats: false, |
| 30 | }) |
| 31 | |
| 32 | const destRelativeToCwd = cwd ? resolve(cwd, dest) : dest |
| 33 | |
| 34 | return Promise.all( |
| 35 | sourceFiles.map(async (p) => { |
| 36 | const dirName = dirname(p) |
| 37 | const baseName = rename(basename(p)) |
| 38 | |
| 39 | const from = cwd ? resolve(cwd, p) : p |
| 40 | const to = parents |
| 41 | ? join(destRelativeToCwd, dirName, baseName) |
| 42 | : join(destRelativeToCwd, baseName) |
| 43 | |
| 44 | // Ensure the destination directory exists |
| 45 | await mkdir(dirname(to), { recursive: true }) |
| 46 | |
| 47 | return copyFile(from, to) |
| 48 | }) |
| 49 | ) |
| 50 | } |