1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00
programming-challenges/cli/utils/copyDirectory.ts

21 lines
716 B
TypeScript

import fs from "node:fs"
import path from "node:path"
export const copyDirectory = async (
source: string,
destination: string,
): Promise<void> => {
const filesToCreate = await fs.promises.readdir(source)
for (const file of filesToCreate) {
const originalFilePath = path.join(source, file)
const stats = await fs.promises.stat(originalFilePath)
if (stats.isFile()) {
const writePath = path.join(destination, file)
await fs.promises.copyFile(originalFilePath, writePath)
} else if (stats.isDirectory()) {
await fs.promises.mkdir(path.join(destination, file), { recursive: true })
await copyDirectory(path.join(source, file), path.join(destination, file))
}
}
}