1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2025-05-18 12:02:53 +02: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,5 +1,5 @@
import path from 'node:path'
import fs from 'node:fs'
import { fileURLToPath } from 'node:url'
import validateProjectName from 'validate-npm-package-name'
@ -21,7 +21,7 @@ export class Challenge implements ChallengeOptions {
constructor (options: ChallengeOptions) {
const { name } = options
this.name = name
this.path = path.join(__dirname, '..', '..', 'challenges', name)
this.path = fileURLToPath(new URL(`../../challenges/${name}`, import.meta.url))
}
static async generate (options: GenerateChallengeOptions): Promise<Challenge> {

View File

@ -1,4 +1,4 @@
import execa from 'execa'
import { execaCommand } from 'execa'
import ora from 'ora'
import ms from 'ms'
@ -11,7 +11,7 @@ export class Docker {
public async build (): Promise<void> {
const loader = ora('Building the Docker image').start()
try {
await execa.command(`docker build --tag=${Docker.CONTAINER_TAG} ./`)
await execaCommand(`docker build --tag=${Docker.CONTAINER_TAG} ./`)
loader.stop()
} catch (error) {
loader.fail()
@ -20,7 +20,7 @@ export class Docker {
}
public async run (input: string): Promise<string> {
const subprocess = execa.command(
const subprocess = execaCommand(
`docker run --interactive --rm ${Docker.CONTAINER_TAG}`,
{
input

View File

@ -1,4 +1,4 @@
import execa from 'execa'
import { execaCommand } from 'execa'
import { Challenge } from './Challenge.js'
import { Solution } from './Solution.js'
@ -35,7 +35,7 @@ export class GitAffected implements GitAffectedOptions {
head: string
): Promise<string[]> {
try {
const { stdout } = await execa.command(
const { stdout } = await execaCommand(
`git diff --name-only --relative ${base} ${head}`
)
return this.parseGitOutput(stdout)
@ -50,7 +50,7 @@ export class GitAffected implements GitAffectedOptions {
public async getLatestPushedCommit (): Promise<string> {
const latestCommit = this.isContinuousIntegration ? '~1' : ''
const { stdout } = await execa.command(`git rev-parse origin/master${latestCommit}`)
const { stdout } = await execaCommand(`git rev-parse origin/master${latestCommit}`)
return stdout
}

View File

@ -1,3 +1,4 @@
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import fs from 'node:fs'
@ -122,12 +123,7 @@ export class Solution implements SolutionOptions {
static async getManyByProgrammingLanguages (programmingLanguages?: string[]): Promise<Solution[]> {
const languages = programmingLanguages ?? await template.getProgrammingLanguages()
const challengesPath = path.join(
__dirname,
'..',
'..',
'challenges'
)
const challengesPath = fileURLToPath(new URL('../../challenges', import.meta.url))
const challenges = await fs.promises.readdir(challengesPath)
const paths: string[] = []
for (const challenge of challenges) {

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.')

View File

@ -1,104 +1,125 @@
import tap from 'tap'
import { Challenge } from '../Challenge.js'
import { GitAffected } from '../GitAffected.js'
import { Solution } from '../Solution.js'
const gitAffected = new GitAffected({ isContinuousIntegration: false })
describe('services/GitAffected - parseGitOutput', () => {
it('returns the right output array', () => {
expect(gitAffected.parseGitOutput('1.txt\n 2.txt ')).toEqual(['1.txt', '2.txt'])
})
})
describe('services/GitAffected - getAffectedSolutionsFromFiles', () => {
it('returns the affected solutions', async () => {
const files = [
'challenges/hello-world/solutions/javascript/function/solution.js',
'challenges/is-palindrome/solutions/c/function/input.c'
]
const solutions = await gitAffected.getAffectedSolutionsFromFiles(files)
expect(solutions).toEqual([
new Solution({
challenge: new Challenge({ name: 'hello-world' }),
name: 'function',
programmingLanguageName: 'javascript'
}),
new Solution({
challenge: new Challenge({ name: 'is-palindrome' }),
name: 'function',
programmingLanguageName: 'c'
})
])
await tap.test('services/GitAffected', async (t) => {
await t.test('parseGitOutput', async (t) => {
await t.test('returns the right output array', async (t) => {
t.same(gitAffected.parseGitOutput('1.txt\n 2.txt '), ['1.txt', '2.txt'])
})
})
it('returns the affected solutions from Dockerfile changes', async () => {
const files = ['templates/docker/javascript/Dockerfile']
const solutions = await gitAffected.getAffectedSolutionsFromFiles(files)
expect(solutions[0]).toEqual(
new Solution({
challenge: new Challenge({ name: 'camel-case' }),
name: 'function',
programmingLanguageName: 'javascript'
})
)
expect(solutions[1]).toEqual(
new Solution({
challenge: new Challenge({ name: 'first-non-repeating-character' }),
name: 'function',
programmingLanguageName: 'javascript'
})
)
})
await t.test('getAffectedSolutionsFromFiles', async (t) => {
await t.test('returns the affected solutions', async (t) => {
const files = [
'challenges/hello-world/solutions/javascript/function/solution.js',
'challenges/is-palindrome/solutions/c/function/input.c'
]
const solutions = await gitAffected.getAffectedSolutionsFromFiles(files)
t.same(solutions, [
new Solution({
challenge: new Challenge({ name: 'hello-world' }),
name: 'function',
programmingLanguageName: 'javascript'
}),
new Solution({
challenge: new Challenge({ name: 'is-palindrome' }),
name: 'function',
programmingLanguageName: 'c'
})
])
})
it('returns the affected solutions from Docker template changes', async () => {
const files = ['templates/docker/javascript/package.json']
const solutions = await gitAffected.getAffectedSolutionsFromFiles(files)
expect(solutions[0]).toEqual(
new Solution({
challenge: new Challenge({ name: 'camel-case' }),
name: 'function',
programmingLanguageName: 'javascript'
})
await t.test(
'returns the affected solutions from Dockerfile changes',
async (t) => {
const files = ['templates/docker/javascript/Dockerfile']
const solutions = await gitAffected.getAffectedSolutionsFromFiles(files)
t.same(
solutions[0],
new Solution({
challenge: new Challenge({ name: 'camel-case' }),
name: 'function',
programmingLanguageName: 'javascript'
})
)
t.same(
solutions[1],
new Solution({
challenge: new Challenge({ name: 'first-non-repeating-character' }),
name: 'function',
programmingLanguageName: 'javascript'
})
)
}
)
expect(solutions[1]).toEqual(
new Solution({
challenge: new Challenge({ name: 'first-non-repeating-character' }),
name: 'function',
programmingLanguageName: 'javascript'
})
)
})
it('returns the affected solutions from input/output files', async () => {
const files = ['challenges/hello-world/test/1/input.txt']
const solutions = await gitAffected.getAffectedSolutionsFromFiles(files)
expect(solutions[0]).toEqual(
new Solution({
challenge: new Challenge({ name: 'hello-world' }),
name: 'function',
programmingLanguageName: 'c'
})
await t.test(
'returns the affected solutions from Docker template changes',
async (t) => {
const files = ['templates/docker/javascript/package.json']
const solutions = await gitAffected.getAffectedSolutionsFromFiles(files)
t.same(
solutions[0],
new Solution({
challenge: new Challenge({ name: 'camel-case' }),
name: 'function',
programmingLanguageName: 'javascript'
})
)
t.same(
solutions[1],
new Solution({
challenge: new Challenge({ name: 'first-non-repeating-character' }),
name: 'function',
programmingLanguageName: 'javascript'
})
)
}
)
expect(solutions[1]).toEqual(
new Solution({
challenge: new Challenge({ name: 'hello-world' }),
name: 'function',
programmingLanguageName: 'cpp'
})
)
expect(solutions[2]).toEqual(
new Solution({
challenge: new Challenge({ name: 'hello-world' }),
name: 'function',
programmingLanguageName: 'cs'
})
)
expect(solutions[3]).toEqual(
new Solution({
challenge: new Challenge({ name: 'hello-world' }),
name: 'function',
programmingLanguageName: 'dart'
})
await t.test(
'returns the affected solutions from input/output files',
async (t) => {
const files = ['challenges/hello-world/test/1/input.txt']
const solutions = await gitAffected.getAffectedSolutionsFromFiles(files)
t.same(
solutions[0],
new Solution({
challenge: new Challenge({ name: 'hello-world' }),
name: 'function',
programmingLanguageName: 'c'
})
)
t.same(
solutions[1],
new Solution({
challenge: new Challenge({ name: 'hello-world' }),
name: 'function',
programmingLanguageName: 'cpp'
})
)
t.same(
solutions[2],
new Solution({
challenge: new Challenge({ name: 'hello-world' }),
name: 'function',
programmingLanguageName: 'cs'
})
)
t.same(
solutions[3],
new Solution({
challenge: new Challenge({ name: 'hello-world' }),
name: 'function',
programmingLanguageName: 'dart'
})
)
}
)
})
})