2023-10-23 23:16:24 +02:00
|
|
|
import path from "node:path"
|
|
|
|
import fs from "node:fs"
|
2022-08-30 15:48:07 +02:00
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
import { Command, Option } from "clipanion"
|
|
|
|
import * as typanion from "typanion"
|
|
|
|
import chalk from "chalk"
|
2022-08-30 15:48:07 +02:00
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
import { isExistingPath } from "../../utils/isExistingPath.js"
|
|
|
|
import { template } from "../../services/Template.js"
|
|
|
|
import { Solution } from "../../services/Solution.js"
|
|
|
|
import { TemporaryFolder } from "../../services/TemporaryFolder.js"
|
2022-08-30 15:48:07 +02:00
|
|
|
|
|
|
|
export class RunSolutionCommand extends Command {
|
2023-10-23 23:16:24 +02:00
|
|
|
public static override paths = [["run", "solution"]]
|
2022-08-30 15:48:07 +02:00
|
|
|
|
2023-01-10 23:15:36 +01:00
|
|
|
public static override usage = {
|
2023-10-23 23:16:24 +02:00
|
|
|
description: "Run the solution with the given `input.txt` file.",
|
2022-08-30 15:48:07 +02:00
|
|
|
}
|
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
public programmingLanguage = Option.String("--language", {
|
|
|
|
description: "The programming language used to solve the challenge.",
|
2022-08-30 15:48:07 +02:00
|
|
|
required: true,
|
2023-10-23 23:16:24 +02:00
|
|
|
validator: typanion.isString(),
|
2022-08-30 15:48:07 +02:00
|
|
|
})
|
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
public challenge = Option.String("--challenge", {
|
|
|
|
description: "The challenge name where you want to run your solution.",
|
2022-08-30 15:48:07 +02:00
|
|
|
required: true,
|
2023-10-23 23:16:24 +02:00
|
|
|
validator: typanion.isString(),
|
2022-08-30 15:48:07 +02:00
|
|
|
})
|
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
public solutionName = Option.String("--solution", {
|
|
|
|
description: "The solution name to run.",
|
2022-08-30 15:48:07 +02:00
|
|
|
required: true,
|
2023-10-23 23:16:24 +02:00
|
|
|
validator: typanion.isString(),
|
2022-08-30 15:48:07 +02:00
|
|
|
})
|
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
public inputPathUser = Option.String("--input-path", {
|
|
|
|
description: "The input file path to use.",
|
2022-08-30 15:48:07 +02:00
|
|
|
required: true,
|
2023-10-23 23:16:24 +02:00
|
|
|
validator: typanion.isString(),
|
2022-08-30 15:48:07 +02:00
|
|
|
})
|
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
public output = Option.Boolean("--output", false, {
|
|
|
|
description: "Display the output of the solution.",
|
2022-08-30 15:48:07 +02:00
|
|
|
})
|
|
|
|
|
2023-01-10 23:15:36 +01:00
|
|
|
public async execute(): Promise<number> {
|
2022-08-30 15:48:07 +02:00
|
|
|
console.log()
|
|
|
|
try {
|
2022-09-22 16:16:21 +02:00
|
|
|
await TemporaryFolder.cleanAll()
|
2022-08-30 15:48:07 +02:00
|
|
|
await template.verifySupportedProgrammingLanguage(
|
2023-10-23 23:16:24 +02:00
|
|
|
this.programmingLanguage,
|
2022-08-30 15:48:07 +02:00
|
|
|
)
|
|
|
|
const solution = await Solution.get({
|
|
|
|
name: this.solutionName,
|
|
|
|
challengeName: this.challenge,
|
2023-10-23 23:16:24 +02:00
|
|
|
programmingLanguageName: this.programmingLanguage,
|
2022-08-30 15:48:07 +02:00
|
|
|
})
|
|
|
|
const inputPath = path.resolve(process.cwd(), this.inputPathUser)
|
|
|
|
if (!(await isExistingPath(inputPath))) {
|
|
|
|
throw new Error(`The \`input-path\` doesn't exist: ${inputPath}.`)
|
|
|
|
}
|
2023-10-23 23:16:24 +02:00
|
|
|
const input = await fs.promises.readFile(inputPath, { encoding: "utf-8" })
|
2022-08-30 15:48:07 +02:00
|
|
|
await solution.run(input, this.output)
|
2022-09-22 16:16:21 +02:00
|
|
|
await TemporaryFolder.cleanAll()
|
2022-08-30 15:48:07 +02:00
|
|
|
return 0
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof Error) {
|
2023-10-23 23:16:24 +02:00
|
|
|
console.error(`${chalk.bold.red("Error:")} ${error.message}`)
|
2022-08-30 15:48:07 +02:00
|
|
|
}
|
2022-09-22 16:16:21 +02:00
|
|
|
await TemporaryFolder.cleanAll()
|
2022-08-30 15:48:07 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|