Skip to content

Commit

Permalink
feature(command): add files selector to append and commit
Browse files Browse the repository at this point in the history
  • Loading branch information
djrasmusp committed Jan 31, 2025
1 parent 32fc17a commit 5b01520
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 44 deletions.
10 changes: 2 additions & 8 deletions src/commands/createBranch.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {input, select} from '@inquirer/prompts';
import {COMMIT_TYPES} from '../utils/constants.js'
import {newBranch} from "../utils/gitUtils.js";
import {kebabCase} from "../utils/utils.js";
import {consola} from "consola";
import {errorHandling, kebabCase} from "../utils/utils.js";

export async function createBranch() {
try {
Expand Down Expand Up @@ -31,11 +30,6 @@ export async function createBranch() {

await newBranch(name)
} catch (error) {
if (error instanceof Error && error.name === 'ExitPromptError') {
console.log('👋 until next time!');
return
}

consola.error(error);
errorHandling(error)
}
}
28 changes: 18 additions & 10 deletions src/commands/createCommit.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import {editor, input, select} from '@inquirer/prompts';
import {COMMIT_TYPES, ENVIROMENTS} from '../utils/constants.js'
import {appendFiles, commitMessage, getDefaults} from "../utils/gitUtils.js";
import {consola} from "consola";
import {select as multiselect} from 'inquirer-select-pro';
import {appendFiles, commitMessage, getDefaults, listOfFiles} from "../utils/gitUtils.js";
import {errorHandling} from "../utils/utils.js";

export async function createCommit() {
export async function createCommit(allFilesSelected) {
try {
const {scope, id} = await getDefaults()
let selectedFiles = ['.']

appendFiles()
if (!allFilesSelected) {
const files = await listOfFiles()

selectedFiles = await multiselect({
multiple: true,
message: 'Select files',
options: files,
validate: (options) => options.length > 0
})
}

const answers = {
scope: await select({
Expand Down Expand Up @@ -50,6 +61,8 @@ export async function createCommit() {
})
}

await appendFiles(selectedFiles)

let message = `${answers.scope}(${answers.id}): ${answers.title}`

if (answers.message) {
Expand All @@ -67,11 +80,6 @@ ENV: ${answers.environment}`
await commitMessage(message)

} catch (error) {
if (error instanceof Error && error.name === 'ExitPromptError') {
console.log('👋 until next time!');
return
}

consola.error(error);
errorHandling(error)
}
}
30 changes: 15 additions & 15 deletions src/commands/restoreFiles.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import {consola} from "consola";
import {listOfFiles} from "../utils/gitUtils.js";
import {select} from 'inquirer-select-pro';
import {gitRestoreFiles, listOfFiles} from "../utils/gitUtils.js";
import {select as multiselect} from 'inquirer-select-pro';
import {errorHandling} from "../utils/utils.js";


export async function restoreFiles() {
export async function restoreFiles(allFilesSelected) {
try {
const files = await listOfFiles()

const answers = {
selectedFiles: await select({
let selectedFiles = ['.']

if (!allFilesSelected) {
const files = await listOfFiles()

selectedFiles = await multiselect({
multiple: true,
message: 'Select files',
options: files
options: files,
validate: (options) => options.length > 0
})
}


console.log(answers.selectedFiles);
await gitRestoreFiles(selectedFiles)
} catch (error) {
if (error instanceof Error && error.name === 'ExitPromptError') {
console.log('👋 until next time!');
return
}

consola.error(error);
errorHandling(error)
}
}
30 changes: 23 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,42 @@ program
.command("commit", {
isDefault: true
})
.action(async () => {
await createCommit()
.option('--all', 'Add All changes to commit', false)
.action(async (str) => {
await createCommit(str.all)
})

program.command("append")
.action(async () => {
await appendToCommit()
.description('Append files to lastest commit')
.option('--all', 'Restore all changes', false)
.action(async (str) => {
await appendToCommit(str.all)
})

program.command("branch")
.description('Create new branch')
.action(async () => {
await createBranch()
})

program.command("gohome").action(async () => {
program.command("gohome").description('Go to default branch and pull lastest changes').action(async () => {
await goHome()
})

program.command("restore").action(async () => {
await restoreFiles()
program.command("restore")
.description('Restore files')
.option('--all', 'Restore all changes', false).action(async (str) => {
await restoreFiles(str.all)
})

program.command("test").description('TEST agfa').option('--all', 'Add All changes to commit', false).action((str, options) => {

if (options.all) {
return console.log('Add all')
}

return console.log('Selected changes')
})


program.parse(process.argv);
16 changes: 12 additions & 4 deletions src/utils/gitUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export async function getDefaults() {
}
}

export async function appendFiles() {
export async function appendFiles(selectedFiles) {
try {
await git.add(['.'])
await git.add(selectedFiles)
} catch (error) {
consola.error(error)
}
Expand All @@ -67,9 +67,9 @@ export async function commitMessage(message) {
}
}

export async function appendToCommit() {
export async function appendToCommit(selectedFiles) {
try {
await git.add('.')
await git.add(selectedFiles)
await git.raw(['commit', '--amend', '--no-edit']);

consola.success('Successfully append files to latest commit');
Expand Down Expand Up @@ -152,4 +152,12 @@ export async function listOfFiles() {
} catch (error) {
consola.error(error);
}
}

export async function gitRestoreFiles(files) {
try {
consola.success(files)
} catch (error) {
consola.error(error);
}
}
14 changes: 14 additions & 0 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,18 @@ export function kebabCase(str, keepLeadingDash = true) {
return ((keepLeadingDash) ? '-' : '') + str.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
.join('-')
.toLowerCase();
}

export function errorHandling(error) {
if (error instanceof Error && error.name === 'ExitPromptError') {
console.log('👋 until next time!');
return
}

if (error?.constructor?.name === 'ExitPromptError') {
console.log('👋 until next time!');
return
}

console.error(error);
}

0 comments on commit 5b01520

Please sign in to comment.