Skip to content

Commit 389ccbb

Browse files
authored
feat (rename): autocomlpete
Improvement of #5
1 parent 2f5fbeb commit 389ccbb

6 files changed

Lines changed: 50 additions & 12 deletions

File tree

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "git-wiz",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"author": {
55
"email": "moshfeu.dev@gmail.com",
66
"name": "Mosh Feu",
@@ -31,15 +31,17 @@
3131
],
3232
"dependencies": {
3333
"commander": "^6.1.0",
34-
"inquirer": "^7.3.3"
34+
"fast-glob": "^3.2.4",
35+
"inquirer": "^7.3.3",
36+
"inquirer-autocomplete-prompt": "git+https://github.com/moshfeu/inquirer-autocomplete-prompt.git"
3537
},
3638
"devDependencies": {
3739
"@types/inquirer": "^7.3.1",
3840
"json-loader": "^0.5.7",
41+
"ts-loader": "^8.0.3",
42+
"typescript": "^4.0.2",
3943
"webpack": "^4.44.1",
4044
"webpack-cli": "^3.3.12",
41-
"webpack-node-externals": "^2.5.2",
42-
"ts-loader": "^8.0.3",
43-
"typescript": "^4.0.2"
45+
"webpack-node-externals": "^2.5.2"
4446
}
4547
}

src/utils/cli.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import inquirer, { Question } from 'inquirer';
2+
import glob from 'fast-glob';
3+
4+
inquirer.registerPrompt(
5+
'autocomplete',
6+
require('inquirer-autocomplete-prompt')
7+
);
28

39
export async function showFilesChooser(
410
message: string,
@@ -36,3 +42,20 @@ export async function showFilesChooserAnd<
3642
...additionalQuestionsArray,
3743
]);
3844
}
45+
46+
export function chooseFileFromFileSystemAnd(
47+
...additionalQuestions: Array<Question>
48+
) {
49+
return inquirer.prompt([
50+
{
51+
type: 'autocomplete',
52+
name: 'path',
53+
message: 'Search by file name',
54+
pageSize: 4,
55+
source: (_answers, input = '*') => {
56+
return glob(`**/*${input}*`);
57+
},
58+
},
59+
...additionalQuestions,
60+
]);
61+
}

src/utils/git.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { execCommand } from './exec';
2-
import { basename } from 'path';
2+
import { join, parse } from 'path';
33

44
type File = {
55
status: 'tracked' | 'staged' | 'untracked';
@@ -84,6 +84,8 @@ export function gitDiff(files: Array<string>, flags: Array<string>) {
8484
}
8585

8686
export function gitMv(path: string, newName: string) {
87-
const newPath = path.replace(basename(path), newName);
87+
const {dir, ext} = parse(path);
88+
const {name, ext: newExt} = parse(newName);
89+
const newPath = join(dir, `${name}${newExt || ext}`);
8890
return runCommand('mv', [], [path, newPath]);
8991
}

src/utils/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function init() {
3030
.action(diff);
3131

3232
program
33-
.command('rename <path> <newName>')
33+
.command('rename')
3434
.description(
3535
'do "git mv" (for renaming) with style 🔖'
3636
)

src/utils/wiz.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { Command } from 'commander';
2-
import { showFilesChooser, showFilesChooserAnd } from './cli';
2+
import {
3+
chooseFileFromFileSystemAnd,
4+
showFilesChooser,
5+
showFilesChooserAnd,
6+
} from './cli';
37
import { gitStatus, gitAdd, gitReset, gitStash, gitDiff, gitMv } from './git';
48

59
export const add = withErrorHandler(async () => {
@@ -28,7 +32,7 @@ export const reset = withErrorHandler(async () => {
2832
});
2933

3034
export const stash = withErrorHandler(async () => {
31-
const status = (await gitStatus()).filter(file => !file.deleted);
35+
const status = (await gitStatus()).filter((file) => !file.deleted);
3236
if (!status.length) {
3337
console.log('\x1b[33m', 'Stash what exactly 🤥?');
3438
return;
@@ -67,7 +71,13 @@ export const diff = withErrorHandler(async (comObj: Command) => {
6771
await gitDiff(files, comObj.args);
6872
});
6973

70-
export const rename = withErrorHandler(async ({args: [path, newName]}: Command) => {
74+
export const rename = withErrorHandler(async () => {
75+
const { path, newName } = await chooseFileFromFileSystemAnd({
76+
type: 'input',
77+
name: 'newName',
78+
message: 'New name (Can has a different extension)',
79+
validate: (name) => (name.length ? true : 'Hello.. new name? 🙄'),
80+
});
7181
await gitMv(path, newName);
7282
});
7383

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"moduleResolution": "node",
1111
"resolveJsonModule": true,
1212
"types": [
13-
"commander"
13+
"commander",
14+
"inquirer-autocomplete-prompt"
1415
]
1516
}
1617
}

0 commit comments

Comments
 (0)