mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2025-05-18 12:02:53 +02:00
feat: add Defibrillators challenge and TypeScript solution
This commit is contained in:
@ -8,12 +8,12 @@ const inputPath = path.join(__dirname, 'input.json')
|
||||
const outputPath = path.join(__dirname, 'output.json')
|
||||
|
||||
const main = async () => {
|
||||
const inputFile = await fs.readFile(inputPath)
|
||||
const inputFile = await fs.readFile(inputPath, { encoding: 'utf-8' })
|
||||
const inputJSON = JSON.parse(inputFile.toString())
|
||||
|
||||
try {
|
||||
const result = solution.apply(null, inputJSON)
|
||||
await fs.writeFile(outputPath, JSON.stringify(result))
|
||||
await fs.writeFile(outputPath, JSON.stringify(result), { encoding: 'utf-8' })
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
|
@ -1,26 +1,21 @@
|
||||
import fs from 'fs'
|
||||
import * as fsWithCallbacks 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()
|
||||
})
|
||||
const fs = fsWithCallbacks.promises
|
||||
|
||||
async function copyDirectory (source: string, destination: string): Promise<void> {
|
||||
const filesToCreate = await fs.readdir(source)
|
||||
for (let file of filesToCreate) {
|
||||
const originalFilePath = path.join(source, file)
|
||||
const stats = await fs.stat(originalFilePath)
|
||||
if (stats.isFile()) {
|
||||
const writePath = path.join(destination, file)
|
||||
await fs.copyFile(originalFilePath, writePath)
|
||||
} else if (stats.isDirectory()) {
|
||||
await fs.mkdir(path.join(destination, file))
|
||||
await copyDirectory(path.join(source, file), path.join(destination, file))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default copyDirPromise
|
||||
export default copyDirectory
|
||||
|
@ -2,17 +2,20 @@ 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)
|
||||
}
|
||||
async function deleteAllFilesExceptOne(
|
||||
directoryPath: string,
|
||||
fileNameToNotDelete: string
|
||||
): Promise<void> {
|
||||
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