1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-07-18 02:20:12 +02:00
programming-challenges/cli/services/Solution.ts

105 lines
2.8 KiB
TypeScript
Raw Normal View History

import path from 'node:path'
import {
createTemporaryEmptyFolder,
TEMPORARY_PATH
} from '../utils/createTemporaryEmptyFolder'
import { isExistingPath } from '../utils/isExistingPath'
import { Challenge } from './Challenge'
import { copyDirectory } from '../utils/copyDirectory'
import { template } from './Template'
import { docker } from './Docker'
import { Test } from './Test'
export interface GetSolutionOptions {
programmingLanguageName: string
challengeName: string
name: string
}
export interface GenerateSolutionOptions extends GetSolutionOptions {
githubUser: string
}
export interface SolutionOptions {
programmingLanguageName: string
challenge: Challenge
name: string
}
export class Solution implements SolutionOptions {
public programmingLanguageName: string
public challenge: Challenge
public name: string
public path: string
constructor (options: SolutionOptions) {
const { programmingLanguageName, challenge, name } = options
this.programmingLanguageName = programmingLanguageName
this.challenge = challenge
this.name = name
this.path = path.join(
challenge.path,
'solutions',
programmingLanguageName,
name
)
}
private async prepareTemporaryFolder (): Promise<void> {
await createTemporaryEmptyFolder()
await copyDirectory(this.path, TEMPORARY_PATH)
await template.docker({
programmingLanguage: this.programmingLanguageName,
destination: TEMPORARY_PATH
})
process.chdir(TEMPORARY_PATH)
}
public async test (): Promise<void> {
await this.prepareTemporaryFolder()
await docker.build()
await Test.runAll(this)
}
static async generate (options: GenerateSolutionOptions): Promise<Solution> {
const { name, challengeName, programmingLanguageName, githubUser } = options
const challenge = new Challenge({ name: challengeName })
if (!(await isExistingPath(challenge.path))) {
throw new Error(`The challenge doesn't exist yet: ${name}.`)
}
const solution = new Solution({
name,
challenge,
programmingLanguageName
})
if (await isExistingPath(solution.path)) {
throw new Error('The solution already exists.')
}
await template.solution({
challengeName: challenge.name,
destination: solution.path,
githubUser,
programmingLanguageName: solution.programmingLanguageName,
name: solution.name
})
return solution
}
static async get (options: GetSolutionOptions): Promise<Solution> {
const { name, challengeName, programmingLanguageName } = options
const challenge = new Challenge({
name: challengeName
})
const solution = new Solution({
name,
challenge,
programmingLanguageName
})
if (!(await isExistingPath(solution.path))) {
throw new Error('The solution was not found.')
}
return solution
}
}