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:
38
cli/utils/__test__/copyDirectory.test.ts
Normal file
38
cli/utils/__test__/copyDirectory.test.ts
Normal 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'])
|
||||
)
|
||||
})
|
||||
})
|
33
cli/utils/__test__/createTemporaryEmptyFolder.test.ts
Normal file
33
cli/utils/__test__/createTemporaryEmptyFolder.test.ts
Normal 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)
|
||||
})
|
||||
})
|
23
cli/utils/__test__/isExistingPath.test.ts
Normal file
23
cli/utils/__test__/isExistingPath.test.ts
Normal 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()
|
||||
})
|
||||
})
|
20
cli/utils/copyDirectory.ts
Normal file
20
cli/utils/copyDirectory.ts
Normal 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))
|
||||
}
|
||||
}
|
||||
}
|
13
cli/utils/createTemporaryEmptyFolder.ts
Normal file
13
cli/utils/createTemporaryEmptyFolder.ts
Normal 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)
|
||||
}
|
10
cli/utils/isExistingPath.ts
Normal file
10
cli/utils/isExistingPath.ts
Normal 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
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user