2021-07-03 18:40:17 +02:00
|
|
|
import path from 'node:path'
|
2022-04-23 18:41:14 +02:00
|
|
|
import { fileURLToPath } from 'node:url'
|
2021-07-03 18:40:17 +02:00
|
|
|
import fs from 'node:fs'
|
2021-06-09 20:31:45 +02:00
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
import replaceInFileDefault from 'replace-in-file'
|
2021-06-09 20:31:45 +02:00
|
|
|
import date from 'date-and-time'
|
|
|
|
|
2022-02-19 18:30:29 +01:00
|
|
|
import { copyDirectory } from '../utils/copyDirectory.js'
|
2021-06-09 20:31:45 +02:00
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
const { replaceInFile } = replaceInFileDefault
|
|
|
|
|
|
|
|
const TEMPLATE_PATH = fileURLToPath(new URL('../../templates', import.meta.url))
|
2021-06-09 20:31:45 +02:00
|
|
|
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')
|
|
|
|
|
|
|
|
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 {
|
2021-06-09 20:31:45 +02:00
|
|
|
const dateString = date.format(new Date(), 'D MMMM Y', true)
|
|
|
|
let description = 'Created'
|
|
|
|
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(
|
|
|
|
options: ReplaceInDestinationOptions
|
|
|
|
): Promise<void> {
|
2021-06-09 20:31:45 +02:00
|
|
|
const { name, description, destination } = options
|
|
|
|
const readmePath = path.join(destination, 'README.md')
|
|
|
|
await replaceInFile({
|
|
|
|
files: [readmePath],
|
|
|
|
from: /{{ name }}/g,
|
|
|
|
to: name
|
|
|
|
})
|
|
|
|
await replaceInFile({
|
|
|
|
files: [readmePath],
|
|
|
|
from: /{{ description }}/g,
|
|
|
|
to: description
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
programmingLanguageName
|
|
|
|
} = options
|
|
|
|
const templateLanguagePath = path.join(
|
|
|
|
TEMPLATE_SOLUTION_PATH,
|
|
|
|
programmingLanguageName
|
|
|
|
)
|
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),
|
|
|
|
destination
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
destination
|
|
|
|
})
|
|
|
|
}
|
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)
|
2022-04-23 18:41:14 +02:00
|
|
|
return languages.filter((language) => language !== 'base')
|
2021-11-09 16:45:42 +01:00
|
|
|
}
|
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
public async verifySupportedProgrammingLanguage(
|
|
|
|
language: string
|
|
|
|
): Promise<void> {
|
2021-11-09 16:45:42 +01:00
|
|
|
const languages = await this.getProgrammingLanguages()
|
|
|
|
if (!languages.includes(language)) {
|
2021-10-13 21:43:45 +02:00
|
|
|
throw new Error('This programming language is not supported yet.')
|
|
|
|
}
|
|
|
|
}
|
2021-06-09 20:31:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const template = new Template()
|