Skip to content

Commit

Permalink
feature(command): restore files
Browse files Browse the repository at this point in the history
- add list function of git status
- restore files command
  • Loading branch information
djrasmusp committed Jan 30, 2025
1 parent c78b337 commit bcd7a5a
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 3 deletions.
169 changes: 166 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@inquirer/prompts": "^7.2.4",
"commander": "^13.1.0",
"consola": "^3.4.0",
"inquirer-select-pro": "^1.0.0-alpha.9",
"simple-git": "^3.27.0"
}
}
27 changes: 27 additions & 0 deletions src/commands/restoreFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {consola} from "consola";
import {listOfFiles} from "../utils/gitUtils.js";
import {select} from 'inquirer-select-pro';


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

const answers = {
selectedFiles: await select({
multiple: true,
message: 'Select files',
options: files
})
}

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

consola.error(error);
}
}
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {program} from "commander";
import {createCommit} from "./commands/createCommit.js";
import {appendToCommit, goHome} from "./utils/gitUtils.js";
import {createBranch} from "./commands/createBranch.js";
import {restoreFiles} from "./commands/restoreFiles.js";

program
.command("commit", {
Expand All @@ -26,4 +27,8 @@ program.command("gohome").action(async () => {
await goHome()
})

program.command("restore").action(async () => {
await restoreFiles()
})

program.parse(process.argv);
19 changes: 19 additions & 0 deletions src/utils/gitUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ export async function goHome() {

consola.success('Welcome home');

} catch (error) {
consola.error(error);
}
}

export async function listOfFiles() {
try {
const files = await git.raw(['status', '--short']);

return files.split('\n').filter(line => line.trim() !== '').map(line => {
const [status, ...filePath] = line.trim().split(' ')
const file = filePath.join(' ')

return {
name: `(${status}) ${file}`,
value: file
}
});

} catch (error) {
consola.error(error);
}
Expand Down

0 comments on commit bcd7a5a

Please sign in to comment.