2023-10-23 23:16:24 +02:00
|
|
|
import { Command, Option } from "clipanion"
|
|
|
|
import * as typanion from "typanion"
|
|
|
|
import chalk from "chalk"
|
2021-06-09 20:31:45 +02:00
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
import { Solution } from "../../services/Solution.js"
|
2021-06-09 20:31:45 +02:00
|
|
|
|
|
|
|
export class GenerateSolutionCommand extends Command {
|
2023-10-23 23:16:24 +02:00
|
|
|
public static override paths = [["generate", "solution"]]
|
2021-06-09 20:31:45 +02:00
|
|
|
|
2023-01-10 23:15:36 +01:00
|
|
|
public static override usage = {
|
2023-10-23 23:16:24 +02:00
|
|
|
description: "Create the basic files needed for a new solution.",
|
2021-06-09 20:31:45 +02:00
|
|
|
}
|
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
public challenge = Option.String("--challenge", {
|
|
|
|
description: "The challenge name you want to generate a solution for.",
|
2021-06-09 20:31:45 +02:00
|
|
|
required: true,
|
2023-10-23 23:16:24 +02:00
|
|
|
validator: typanion.isString(),
|
2021-06-09 20:31:45 +02:00
|
|
|
})
|
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
public githubUser = Option.String("--github-user", {
|
|
|
|
description: "Your GitHub user.",
|
2021-06-09 20:31:45 +02:00
|
|
|
required: true,
|
2023-10-23 23:16:24 +02:00
|
|
|
validator: typanion.isString(),
|
2021-06-09 20:31:45 +02:00
|
|
|
})
|
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
public solutionName = Option.String("--solution", {
|
|
|
|
description: "The new solution name to generate.",
|
2021-06-09 20:31:45 +02:00
|
|
|
required: true,
|
2023-10-23 23:16:24 +02:00
|
|
|
validator: typanion.isString(),
|
2021-06-09 20:31:45 +02:00
|
|
|
})
|
|
|
|
|
2023-10-23 23:16:24 +02:00
|
|
|
public programmingLanguage = Option.String("--language", {
|
|
|
|
description: "The programming language to use to solve the challenge.",
|
2021-06-09 20:31:45 +02:00
|
|
|
required: true,
|
2023-10-23 23:16:24 +02:00
|
|
|
validator: typanion.isString(),
|
2021-06-09 20:31:45 +02:00
|
|
|
})
|
|
|
|
|
2023-01-10 23:15:36 +01:00
|
|
|
public async execute(): Promise<number> {
|
2021-06-09 20:31:45 +02:00
|
|
|
try {
|
|
|
|
const solution = await Solution.generate({
|
|
|
|
name: this.solutionName,
|
|
|
|
githubUser: this.githubUser,
|
|
|
|
challengeName: this.challenge,
|
2023-10-23 23:16:24 +02:00
|
|
|
programmingLanguageName: this.programmingLanguage,
|
2021-06-09 20:31:45 +02:00
|
|
|
})
|
|
|
|
console.log(
|
2023-10-23 23:16:24 +02:00
|
|
|
`${chalk.bold.green("Success:")} created the new solution at ${
|
2021-06-09 20:31:45 +02:00
|
|
|
solution.path
|
2023-10-23 23:16:24 +02:00
|
|
|
}.`,
|
2021-06-09 20:31:45 +02:00
|
|
|
)
|
|
|
|
return 0
|
|
|
|
} catch (error) {
|
2021-08-27 13:03:41 +02:00
|
|
|
if (error instanceof Error) {
|
2023-10-23 23:16:24 +02:00
|
|
|
console.error(`${chalk.bold.red("Error:")} ${error.message}`)
|
2021-08-27 13:03:41 +02:00
|
|
|
}
|
2021-06-09 20:31:45 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|