Skip to content

Commit

Permalink
feat(cli): option to add specific files or all
Browse files Browse the repository at this point in the history
  • Loading branch information
renejfc committed Apr 10, 2024
1 parent cb22691 commit 68febee
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,56 @@
import { intro, log, outro } from "@clack/prompts"
import { $ } from "bun"
import c from "picocolors"
import { commitPrompt } from "~/prompts"
import { parseArgs } from "~/lib"
import { addPrompt, commitPrompt } from "~/prompts"
import { getCommitMessage, handleNonZeroExit, tasks } from "~/utils"

console.clear()
$.nothrow()

intro(c.bold(c.bgCyan(" CONMMIT ")))
const args = parseArgs([
[["add", "a"], "Choose which files to add"],
[["add-all", "A"], "Add all changes to the commit"],
])

intro(c.bold(c.bgCyan("CONMMIT")))

const addResults = (args.get("add") && (await addPrompt())) || []
const commitResults = await commitPrompt()

await tasks([
{
progress: ["Adding files", "Files added!"],
enabled: addResults.length > 0,
task: async ({ message, stop }) => {
for (const file of addResults) {
message(`Adding ${file}`)

const { stderr, stdout, exitCode } = await $`git add ${file}`.quiet()
const [error, output] = [stderr.toString(), stdout.toString()]

handleNonZeroExit(() => stop(c.bold(`Failed adding ${file}.`), exitCode), {
error,
output,
exitCode,
})
}
},
},
{
progress: ["Adding all changes", "All changes added!"],
enabled: args.get("add-all"),
task: async ({ stop }) => {
const { stderr, stdout, exitCode } = await $`git add -A`.quiet()
const [error, output] = [stderr.toString(), stdout.toString()]

handleNonZeroExit(() => stop(c.bold("Failed adding all changes."), exitCode), {
error,
output,
exitCode,
})
},
},
{
progress: ["Committing", "Commit created!"],
enabled: true,
Expand Down
53 changes: 53 additions & 0 deletions src/prompts/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { groupMultiselect, log } from "@clack/prompts"
import { $, type ShellOutput } from "bun"
import c from "picocolors"
import { cancelOnCancel } from "~/utils"

export const addPrompt = async () => {
const options = await getOptions()

if (options.size === 0) {
log.warn(c.italic("No changes to add."))
return
}

const results = await groupMultiselect({
message: "Which changes would you like to add?",
required: true,
options: Object.fromEntries(options.entries()),
})

cancelOnCancel(results)

return results as string[]
}

const getOptions = async () => {
const [changedFilesSh, untrackedFilesSh, deletedFilesSh] = await Promise.all([
$`git ls-files --modified --exclude-standard`.quiet(),
$`git ls-files --others --exclude-standard`.quiet(),
$`git ls-files --deleted --exclude-standard`.quiet(),
])

const filesStrToOptions = (input: ShellOutput) =>
input.stdout
.toString()
.split("\n")
.map(fileStr => ({
label: c.italic(fileStr),
value: fileStr,
}))
.filter(str => str.value !== "")

const changed = filesStrToOptions(changedFilesSh)
const untracked = filesStrToOptions(untrackedFilesSh)
const deleted = filesStrToOptions(deletedFilesSh)

const output = new Map<string, { label: string; value: string }[]>()

if (changed.length) output.set(c.bold("Changed Files"), changed)
if (untracked.length) output.set(c.bold("Untracked Files"), untracked)
if (deleted.length) output.set(c.bold("Deleted Files"), deleted)

return output
}
1 change: 1 addition & 0 deletions src/prompts/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./commit"
export * from "./add"

0 comments on commit 68febee

Please sign in to comment.