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:
26
scripts/utils/copyDirPromise.ts
Normal file
26
scripts/utils/copyDirPromise.ts
Normal 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
|
18
scripts/utils/deleteAllFilesExceptOne.ts
Normal file
18
scripts/utils/deleteAllFilesExceptOne.ts
Normal 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
|
Reference in New Issue
Block a user