1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2025-05-18 12:02:53 +02:00

🎉 Initial commit

This commit is contained in:
Divlo
2020-07-05 15:48:51 +02:00
commit 395ae0eff3
32 changed files with 1752 additions and 0 deletions

View File

@ -0,0 +1,26 @@
import fs from 'fs'
import path from 'path'
function copyDirPromise (source: string, destination: string) {
return new Promise(next => {
const filesToCreate = fs.readdirSync(source)
filesToCreate.forEach(async file => {
const originalFilePath = path.join(source, file)
const stats = fs.statSync(originalFilePath)
if (stats.isFile()) {
if (file === '.npmignore') file = '.gitignore'
const writePath = path.join(destination, file)
fs.copyFileSync(originalFilePath, writePath)
} else if (stats.isDirectory()) {
fs.mkdirSync(path.join(destination, file))
await copyDirPromise(
path.join(source, file),
path.join(destination, file)
)
}
})
next()
})
}
export default copyDirPromise

View File

@ -0,0 +1,18 @@
import path from 'path'
import * as fsWithCallbacks from 'fs'
const fs = fsWithCallbacks.promises
async function deleteAllFilesExceptOne (directoryPath: string, fileNameToNotDelete: string) {
const fileNames = await fs.readdir(path.resolve(directoryPath))
for (const name of fileNames) {
const fileNamePath = path.resolve(directoryPath, name)
const stats = await fs.stat(fileNamePath)
if (stats.isDirectory()) {
await fs.rmdir(fileNamePath, { recursive: true })
} else if (name !== fileNameToNotDelete) {
await fs.unlink(fileNamePath)
}
}
}
export default deleteAllFilesExceptOne