mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2025-12-11 00:21:24 +01:00
chore: better Prettier config for easier reviews
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
import path from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import fs from 'node:fs'
|
||||
import path from "node:path"
|
||||
import { fileURLToPath } from "node:url"
|
||||
import fs from "node:fs"
|
||||
|
||||
import replaceInFileDefault from 'replace-in-file'
|
||||
import date from 'date-and-time'
|
||||
import replaceInFileDefault from "replace-in-file"
|
||||
import date from "date-and-time"
|
||||
|
||||
import { copyDirectory } from '../utils/copyDirectory.js'
|
||||
import { copyDirectory } from "../utils/copyDirectory.js"
|
||||
|
||||
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')
|
||||
const TEMPLATE_SOLUTION_BASE_PATH = path.join(TEMPLATE_SOLUTION_PATH, 'base')
|
||||
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")
|
||||
|
||||
export interface TemplateDockerOptions {
|
||||
programmingLanguage: string
|
||||
@@ -42,8 +42,8 @@ export interface ReplaceInDestinationOptions {
|
||||
|
||||
class Template {
|
||||
private getDescription(githubUser?: string): string {
|
||||
const dateString = date.format(new Date(), 'D MMMM Y', true)
|
||||
let description = 'Created'
|
||||
const dateString = date.format(new Date(), "D MMMM Y", true)
|
||||
let description = "Created"
|
||||
if (githubUser != null) {
|
||||
description += ` by [@${githubUser}](https://github.com/${githubUser})`
|
||||
}
|
||||
@@ -52,19 +52,19 @@ class Template {
|
||||
}
|
||||
|
||||
private async replaceInDestination(
|
||||
options: ReplaceInDestinationOptions
|
||||
options: ReplaceInDestinationOptions,
|
||||
): Promise<void> {
|
||||
const { name, description, destination } = options
|
||||
const readmePath = path.join(destination, 'README.md')
|
||||
const readmePath = path.join(destination, "README.md")
|
||||
await replaceInFile({
|
||||
files: [readmePath],
|
||||
from: /{{ name }}/g,
|
||||
to: name
|
||||
to: name,
|
||||
})
|
||||
await replaceInFile({
|
||||
files: [readmePath],
|
||||
from: /{{ description }}/g,
|
||||
to: description
|
||||
to: description,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -80,11 +80,11 @@ class Template {
|
||||
githubUser,
|
||||
name,
|
||||
challengeName,
|
||||
programmingLanguageName
|
||||
programmingLanguageName,
|
||||
} = options
|
||||
const templateLanguagePath = path.join(
|
||||
TEMPLATE_SOLUTION_PATH,
|
||||
programmingLanguageName
|
||||
programmingLanguageName,
|
||||
)
|
||||
await this.verifySupportedProgrammingLanguage(programmingLanguageName)
|
||||
await fs.promises.mkdir(destination, { recursive: true })
|
||||
@@ -93,7 +93,7 @@ class Template {
|
||||
await this.replaceInDestination({
|
||||
name: `${challengeName}/${programmingLanguageName}/${name}`,
|
||||
description: this.getDescription(githubUser),
|
||||
destination
|
||||
destination,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -103,23 +103,23 @@ class Template {
|
||||
await this.replaceInDestination({
|
||||
name,
|
||||
description: this.getDescription(githubUser),
|
||||
destination
|
||||
destination,
|
||||
})
|
||||
}
|
||||
|
||||
public async getProgrammingLanguages(): Promise<string[]> {
|
||||
const languages = await fs.promises.readdir(TEMPLATE_SOLUTION_PATH)
|
||||
return languages.filter((language) => {
|
||||
return language !== 'base'
|
||||
return language !== "base"
|
||||
})
|
||||
}
|
||||
|
||||
public async verifySupportedProgrammingLanguage(
|
||||
language: string
|
||||
language: string,
|
||||
): Promise<void> {
|
||||
const languages = await this.getProgrammingLanguages()
|
||||
if (!languages.includes(language)) {
|
||||
throw new Error('This programming language is not supported yet.')
|
||||
throw new Error("This programming language is not supported yet.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user