1
0
mirror of https://github.com/theoludwig/programming-challenges.git synced 2025-12-11 00:21:24 +01:00

feat: usage of ESM modules imports (instead of CommonJS) (#14)

This commit is contained in:
Divlo
2022-04-23 18:41:14 +02:00
committed by GitHub
parent c502c17e7c
commit 104c61935a
44 changed files with 6623 additions and 5171 deletions

View File

@@ -1,12 +1,15 @@
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import fs from 'node:fs'
import { replaceInFile } from 'replace-in-file'
import replaceInFileDefault from 'replace-in-file'
import date from 'date-and-time'
import { copyDirectory } from '../utils/copyDirectory.js'
const TEMPLATE_PATH = path.join(__dirname, '..', '..', 'templates')
const { replaceInFile } = replaceInFileDefault
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')
@@ -38,7 +41,7 @@ export interface ReplaceInDestinationOptions {
}
class Template {
private getDescription (githubUser?: string): string {
private getDescription(githubUser?: string): string {
const dateString = date.format(new Date(), 'D MMMM Y', true)
let description = 'Created'
if (githubUser != null) {
@@ -48,7 +51,9 @@ class Template {
return description
}
private async replaceInDestination (options: ReplaceInDestinationOptions): Promise<void> {
private async replaceInDestination(
options: ReplaceInDestinationOptions
): Promise<void> {
const { name, description, destination } = options
const readmePath = path.join(destination, 'README.md')
await replaceInFile({
@@ -63,15 +68,24 @@ class Template {
})
}
public async docker (options: TemplateDockerOptions): Promise<void> {
public async docker(options: TemplateDockerOptions): Promise<void> {
const { programmingLanguage, destination } = options
const sourcePath = path.join(TEMPLATE_DOCKER_PATH, programmingLanguage)
await copyDirectory(sourcePath, destination)
}
public async solution (options: TemplateSolutionOptions): Promise<void> {
const { destination, githubUser, name, challengeName, programmingLanguageName } = options
const templateLanguagePath = path.join(TEMPLATE_SOLUTION_PATH, programmingLanguageName)
public async solution(options: TemplateSolutionOptions): Promise<void> {
const {
destination,
githubUser,
name,
challengeName,
programmingLanguageName
} = options
const templateLanguagePath = path.join(
TEMPLATE_SOLUTION_PATH,
programmingLanguageName
)
await this.verifySupportedProgrammingLanguage(programmingLanguageName)
await fs.promises.mkdir(destination, { recursive: true })
await copyDirectory(templateLanguagePath, destination)
@@ -83,7 +97,7 @@ class Template {
})
}
public async challenge (options: TemplateChallengeOptions): Promise<void> {
public async challenge(options: TemplateChallengeOptions): Promise<void> {
const { destination, githubUser, name } = options
await copyDirectory(TEMPLATE_CHALLENGE_PATH, destination)
await this.replaceInDestination({
@@ -93,12 +107,14 @@ class Template {
})
}
public async getProgrammingLanguages (): Promise<string[]> {
public async getProgrammingLanguages(): Promise<string[]> {
const languages = await fs.promises.readdir(TEMPLATE_SOLUTION_PATH)
return languages.filter(language => language !== 'base')
return languages.filter((language) => language !== 'base')
}
public async verifySupportedProgrammingLanguage (language: string): Promise<void> {
public async verifySupportedProgrammingLanguage(
language: string
): Promise<void> {
const languages = await this.getProgrammingLanguages()
if (!languages.includes(language)) {
throw new Error('This programming language is not supported yet.')