mime-todo-cli/src/commands/StartCommand.ts

61 lines
2 KiB
TypeScript

import type { Argv, ArgumentsCamelCase } from "yargs"
import { CLICommand } from "../cli/CLICommand.js"
import { parseTodoFile, writeTodoFile } from "../file.js"
import { getCurrentBranch, branchExists, commitFileWithBody } from "../git.js"
import { validateStatusTransition } from "../issue.js"
export class StartCommand extends CLICommand {
readonly name = "start <id>"
readonly help = "Start work on an issue"
readonly description = "Set issue to in-progress (must be on develop)"
addArguments(yargs: Argv): Argv {
return yargs
.positional("id", { type: "number", demandOption: true })
.option("acceptance-criteria", {
type: "string",
demandOption: true,
description: "What must be true for this issue to be considered done",
})
}
async execute(args: ArgumentsCamelCase): Promise<number> {
const branch = getCurrentBranch()
if (branch !== "develop") {
console.error(`Must be on develop to start an issue (currently on ${branch})`)
return 1
}
const todo = await parseTodoFile()
const issue = todo.issues.find(i => i.id === args.id)
if (!issue) {
console.error(`Issue #${args.id} not found`)
return 1
}
const err = validateStatusTransition(issue.status, "in-progress")
if (err) {
console.error(err)
return 1
}
// Check that the issue branch doesn't already exist
const issueBranch = `${issue.type}/${issue.id}`
if (branchExists(issueBranch)) {
console.error(`Branch ${issueBranch} already exists`)
return 1
}
issue.status = "in-progress"
writeTodoFile(todo)
commitFileWithBody(
"TODO",
`todo(${issue.id}): in-progress`,
args.acceptanceCriteria as string
)
console.log(`Issue #${issue.id} is now in-progress`)
console.log(`Create the issue branch: git checkout -b ${issueBranch}`)
console.log(`If working in a submodule, also checkout there: cd <submodule> && git checkout -b ${issueBranch}`)
return 0
}
}