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
703 B
TypeScript
Raw Normal View History

import fs from 'fs'
import path from '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))
}
}
}