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

feat: rewrite programming-challenges CLI (#3)

This commit is contained in:
Divlo
2021-06-09 20:31:45 +02:00
committed by GitHub
parent 7aa12f313e
commit 677a55a9d8
256 changed files with 16829 additions and 1881 deletions

View File

@ -0,0 +1,38 @@
import fsMock from 'mock-fs'
import fs from 'fs'
import { copyDirectory } from '../copyDirectory'
describe('utils/copyDirectory', () => {
afterEach(async () => {
fsMock.restore()
})
it('copy the files', async () => {
fsMock({
'/source': {
'default.png': '',
'index.ts': '',
'.npmignore': ''
},
'/destination': {}
})
let destinationDirectoryContent = await fs.promises.readdir('/destination')
let sourceDirectoryContent = await fs.promises.readdir('/source')
expect(destinationDirectoryContent.length).toEqual(0)
expect(sourceDirectoryContent.length).toEqual(3)
await copyDirectory('/source', '/destination')
destinationDirectoryContent = await fs.promises.readdir('/destination')
sourceDirectoryContent = await fs.promises.readdir('/source')
expect(destinationDirectoryContent.length).toEqual(3)
expect(sourceDirectoryContent.length).toEqual(3)
expect(destinationDirectoryContent).toEqual(
expect.arrayContaining(['default.png', 'index.ts', '.npmignore'])
)
expect(sourceDirectoryContent).toEqual(
expect.arrayContaining(['default.png', 'index.ts', '.npmignore'])
)
})
})

View File

@ -0,0 +1,33 @@
import fsMock from 'mock-fs'
import fs from 'fs'
import {
TEMPORARY_PATH,
createTemporaryEmptyFolder
} from '../createTemporaryEmptyFolder'
import { isExistingPath } from '../isExistingPath'
describe('utils/createTemporaryEmptyFolder', () => {
afterEach(async () => {
fsMock.restore()
})
it('should create the temporary folder', async () => {
fsMock({})
expect(await isExistingPath(TEMPORARY_PATH)).toBeFalsy()
await createTemporaryEmptyFolder()
expect(await isExistingPath(TEMPORARY_PATH)).toBeTruthy()
})
it('should remove and create again the temporary folder', async () => {
fsMock({
[TEMPORARY_PATH]: {
'file.txt': ''
}
})
expect(await isExistingPath(TEMPORARY_PATH)).toBeTruthy()
expect((await fs.promises.readdir(TEMPORARY_PATH)).length).toEqual(1)
await createTemporaryEmptyFolder()
expect((await fs.promises.readdir(TEMPORARY_PATH)).length).toEqual(0)
})
})

View File

@ -0,0 +1,23 @@
import fsMock from 'mock-fs'
import { isExistingPath } from '../isExistingPath'
describe('utils/isExistingFile', () => {
afterEach(async () => {
fsMock.restore()
})
it('should return true if the file exists', async () => {
fsMock({
'/file.txt': ''
})
expect(await isExistingPath('/file.txt')).toBeTruthy()
})
it("should return false if the file doesn't exists", async () => {
fsMock({
'/file.txt': ''
})
expect(await isExistingPath('/randomfile.txt')).toBeFalsy()
})
})

View File

@ -0,0 +1,20 @@
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))
}
}
}

View File

@ -0,0 +1,13 @@
import path from 'path'
import fs from 'fs'
import { isExistingPath } from '../utils/isExistingPath'
export const TEMPORARY_PATH = path.join(__dirname, '..', '..', 'temp')
export const createTemporaryEmptyFolder = async (): Promise<void> => {
if (await isExistingPath(TEMPORARY_PATH)) {
await fs.promises.rm(TEMPORARY_PATH, { recursive: true, force: true })
}
await fs.promises.mkdir(TEMPORARY_PATH)
}

View File

@ -0,0 +1,10 @@
import fs from 'fs'
export const isExistingPath = async (path: string): Promise<boolean> => {
try {
await fs.promises.access(path, fs.constants.F_OK)
return true
} catch {
return false
}
}