2023-10-23 23:16:24 +02:00
|
|
|
import path from "node:path"
|
|
|
|
import { fileURLToPath } from "node:url"
|
|
|
|
import fs from "node:fs"
|
2021-06-09 20:31:45 +02:00
|
|
|
|
2024-11-18 01:04:51 +01:00
|
|
|
import { replaceInFile } from "replace-in-file"
|
2023-10-23 23:16:24 +02:00
|
|
|
import date from "date-and-time"
|
2021-06-09 20:31:45 +02:00
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
import { copyDirectory } from "../utils/copyDirectory.js"
|
2021-06-09 20:31:45 +02:00
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
const TEMPLATE_PATH = fileURLToPath(new URL("../../templates", import.meta.url))
|
|
|
|
const TEMPLATE_DOCKER_PATH = path.join(TEMPLATE_PATH, "docker")
|
|
|
|
const TEMPLATE_CHALLENGE_PATH = path.join(TEMPLATE_PATH, "challenge")
|
|
|
|
const TEMPLATE_SOLUTION_PATH = path.join(TEMPLATE_PATH, "solution")
|
|
|
|
const TEMPLATE_SOLUTION_BASE_PATH = path.join(TEMPLATE_SOLUTION_PATH, "base")
|
2021-06-09 20:31:45 +02:00
|
|
|
|
|
|
|
export interface TemplateDockerOptions {
|
|
|
|
programmingLanguage: string
|
|
|
|
destination: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface TemplateChallengeOptions {
|
|
|
|
name: string
|
|
|
|
githubUser?: string
|
|
|
|
destination: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface TemplateSolutionOptions {
|
|
|
|
challengeName: string
|
|
|
|
programmingLanguageName: string
|
|
|
|
name: string
|
|
|
|
githubUser?: string
|
|
|
|
destination: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ReplaceInDestinationOptions {
|
|
|
|
destination: string
|
|
|
|
name: string
|
|
|
|
description: string
|
|
|
|
}
|
|
|
|
|
|
|
|
class Template {
|
2022-04-23 18:41:14 +02:00
|
|
|
private getDescription(githubUser?: string): string {
|
2023-10-23 23:16:24 +02:00
|
|
|
const dateString = date.format(new Date(), "D MMMM Y", true)
|
|
|
|
let description = "Created"
|
2021-06-09 20:31:45 +02:00
|
|
|
if (githubUser != null) {
|
|
|
|
description += ` by [@${githubUser}](https://github.com/${githubUser})`
|
|
|
|
}
|
|
|
|
description += ` on ${dateString}.`
|
|
|
|
return description
|
|
|
|
}
|
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
private async replaceInDestination(
|
2024-11-18 01:37:42 +01:00
|
|
|
options: ReplaceInDestinationOptions,
|
2022-04-23 18:41:14 +02:00
|
|
|
): Promise<void> {
|
2021-06-09 20:31:45 +02:00
|
|
|
const { name, description, destination } = options
|
2023-10-23 23:16:24 +02:00
|
|
|
const readmePath = path.join(destination, "README.md")
|
2021-06-09 20:31:45 +02:00
|
|
|
await replaceInFile({
|
|
|
|
files: [readmePath],
|
|
|
|
from: /{{ name }}/g,
|
2023-10-23 23:16:24 +02:00
|
|
|
to: name,
|
2021-06-09 20:31:45 +02:00
|
|
|
})
|
|
|
|
await replaceInFile({
|
|
|
|
files: [readmePath],
|
|
|
|
from: /{{ description }}/g,
|
2023-10-23 23:16:24 +02:00
|
|
|
to: description,
|
2021-06-09 20:31:45 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
public async docker(options: TemplateDockerOptions): Promise<void> {
|
2021-06-09 20:31:45 +02:00
|
|
|
const { programmingLanguage, destination } = options
|
|
|
|
const sourcePath = path.join(TEMPLATE_DOCKER_PATH, programmingLanguage)
|
|
|
|
await copyDirectory(sourcePath, destination)
|
|
|
|
}
|
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
public async solution(options: TemplateSolutionOptions): Promise<void> {
|
|
|
|
const {
|
|
|
|
destination,
|
|
|
|
githubUser,
|
|
|
|
name,
|
|
|
|
challengeName,
|
2023-10-23 23:16:24 +02:00
|
|
|
programmingLanguageName,
|
2022-04-23 18:41:14 +02:00
|
|
|
} = options
|
|
|
|
const templateLanguagePath = path.join(
|
|
|
|
TEMPLATE_SOLUTION_PATH,
|
2024-11-18 01:37:42 +01:00
|
|
|
programmingLanguageName,
|
2022-04-23 18:41:14 +02:00
|
|
|
)
|
2021-10-13 21:43:45 +02:00
|
|
|
await this.verifySupportedProgrammingLanguage(programmingLanguageName)
|
2021-06-09 20:31:45 +02:00
|
|
|
await fs.promises.mkdir(destination, { recursive: true })
|
|
|
|
await copyDirectory(templateLanguagePath, destination)
|
|
|
|
await copyDirectory(TEMPLATE_SOLUTION_BASE_PATH, destination)
|
|
|
|
await this.replaceInDestination({
|
|
|
|
name: `${challengeName}/${programmingLanguageName}/${name}`,
|
|
|
|
description: this.getDescription(githubUser),
|
2023-10-23 23:16:24 +02:00
|
|
|
destination,
|
2021-06-09 20:31:45 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
public async challenge(options: TemplateChallengeOptions): Promise<void> {
|
2021-06-09 20:31:45 +02:00
|
|
|
const { destination, githubUser, name } = options
|
|
|
|
await copyDirectory(TEMPLATE_CHALLENGE_PATH, destination)
|
|
|
|
await this.replaceInDestination({
|
2022-02-19 18:30:29 +01:00
|
|
|
name,
|
2021-06-09 20:31:45 +02:00
|
|
|
description: this.getDescription(githubUser),
|
2023-10-23 23:16:24 +02:00
|
|
|
destination,
|
2021-06-09 20:31:45 +02:00
|
|
|
})
|
|
|
|
}
|
2021-10-13 21:43:45 +02:00
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
public async getProgrammingLanguages(): Promise<string[]> {
|
2021-11-09 16:45:42 +01:00
|
|
|
const languages = await fs.promises.readdir(TEMPLATE_SOLUTION_PATH)
|
2023-01-10 23:15:36 +01:00
|
|
|
return languages.filter((language) => {
|
2023-10-23 23:16:24 +02:00
|
|
|
return language !== "base"
|
2023-01-10 23:15:36 +01:00
|
|
|
})
|
2021-11-09 16:45:42 +01:00
|
|
|
}
|
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
public async verifySupportedProgrammingLanguage(
|
2024-11-18 01:37:42 +01:00
|
|
|
language: string,
|
2022-04-23 18:41:14 +02:00
|
|
|
): Promise<void> {
|
2021-11-09 16:45:42 +01:00
|
|
|
const languages = await this.getProgrammingLanguages()
|
|
|
|
if (!languages.includes(language)) {
|
2023-10-23 23:16:24 +02:00
|
|
|
throw new Error("This programming language is not supported yet.")
|
2021-10-13 21:43:45 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-09 20:31:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const template = new Template()
|