1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00
programming-challenges/cli/services/Challenge.ts

56 lines
1.6 KiB
TypeScript
Raw Normal View History

import fs from "node:fs"
import { fileURLToPath } from "node:url"
import validateProjectName from "validate-npm-package-name"
import { isExistingPath } from "../utils/isExistingPath.js"
import { template } from "./Template.js"
export interface ChallengeOptions {
name: string
}
export interface GenerateChallengeOptions extends ChallengeOptions {
githubUser: string
}
export class Challenge implements ChallengeOptions {
public static BASE_URL = new URL("../../challenges/", import.meta.url)
public name: string
public path: string
constructor(options: ChallengeOptions) {
const { name } = options
this.name = name
2023-01-10 23:15:36 +01:00
this.path = fileURLToPath(new URL(`./${name}`, Challenge.BASE_URL))
}
2023-01-05 18:29:07 +01:00
public static async getChallenges(): Promise<Challenge[]> {
2023-01-10 23:15:36 +01:00
const challengeNames = await fs.promises.readdir(Challenge.BASE_URL)
2023-01-05 18:29:07 +01:00
return challengeNames.map((challengeName) => {
return new Challenge({ name: challengeName })
})
}
2023-01-10 23:15:36 +01:00
public static async generate(
options: GenerateChallengeOptions,
2023-01-10 23:15:36 +01:00
): Promise<Challenge> {
const { name, githubUser } = options
const challenge = new Challenge({ name })
if (await isExistingPath(challenge.path)) {
throw new Error(`The challenge already exists: ${name}.`)
}
const isValidName = validateProjectName(name).validForNewPackages
if (!isValidName) {
throw new Error("Invalid challenge name.")
}
await fs.promises.mkdir(challenge.path)
await template.challenge({
destination: challenge.path,
githubUser,
name,
})
return challenge
}
}