1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-11-09 22:08:58 +01:00
programming-challenges/cli/utils/copyDirectory.ts

21 lines
713 B
TypeScript

import fs from 'node:fs'
import path from 'node:path'
export async function copyDirectory (
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))
}
}
}