Skip to content

Commit b2987c4

Browse files
authored
fix(diff): cached (#3)
1 parent 134f7cb commit b2987c4

File tree

6 files changed

+72
-43
lines changed

6 files changed

+72
-43
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
![thanks for stopping by](https://moshfeudev.wixsite.com/shield/_functions/view/git-wiz)
2-
![npm version](https://img.shields.io/npm/v/git-wiz)
2+
[![npm version](https://img.shields.io/npm/v/git-wiz)](https://www.npmjs.com/package/git-wiz)
33

44
# Git Wiz 🧙‍♂️
55

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "git-wiz",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"author": {
55
"email": "[email protected]",
66
"name": "Mosh Feu",
@@ -24,6 +24,11 @@
2424
"homepage": "https://github.com/spread-the-code/git-wiz#readme",
2525
"license": "MIT",
2626
"description": "Make git commands interactive",
27+
"keywords": [
28+
"git",
29+
"cli",
30+
"node"
31+
],
2732
"dependencies": {
2833
"commander": "^6.1.0",
2934
"inquirer": "^7.3.3",

src/utils/git.ts

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ import { execCommand } from './exec';
33
type File = {
44
status: 'tracked' | 'staged' | 'untracked';
55
path: string;
6+
deleted?: boolean;
67
};
78

8-
async function runCommand(command: string, files: Array<string>, stdio = false) {
9-
const gitCommand = `${command} ${files.join(' ')}`;
9+
async function runCommand(
10+
command: string,
11+
files: Array<string>,
12+
args?: Array<string>,
13+
stdio = false
14+
) {
15+
const gitCommand = `${command} ${[...(args || []), ...files].join(' ')}`;
1016
await execCommand(gitCommand, stdio);
1117
if (stdio) {
1218
return;
@@ -19,29 +25,35 @@ export async function gitStatus(): Promise<Array<File>> {
1925
const files = status
2026
.split(/\n/g)
2127
.filter((line) => line)
22-
.reduce((prev, line) => {
23-
const [index, ...params] = line.split(/ +/g);
24-
const path = params[params.length - 1];
28+
.reduce<Array<File>>((prev, line) => {
29+
try {
30+
const [index, ...params] = line.split(/ +/g);
31+
const path = params[params.length - 1];
2532

26-
if (index === '1') {
27-
const [stagedIndication, changedIndication] = params[0].split('');
28-
if (stagedIndication !== '.') {
33+
if (index === '1') {
34+
const [stagedIndication, changedIndication] = params[0].split('');
35+
if (stagedIndication !== '.') {
36+
prev.push({
37+
status: 'staged',
38+
path,
39+
deleted: stagedIndication === 'D',
40+
});
41+
}
42+
if (changedIndication !== '.') {
43+
prev.push({
44+
status: 'tracked',
45+
path,
46+
deleted: changedIndication === 'D',
47+
});
48+
}
49+
} else if (index === '?') {
2950
prev.push({
30-
status: 'staged',
51+
status: 'untracked',
3152
path,
3253
});
3354
}
34-
if (changedIndication !== '.') {
35-
prev.push({
36-
status: 'tracked',
37-
path,
38-
});
39-
}
40-
} else if (index === '?') {
41-
prev.push({
42-
status: 'untracked',
43-
path,
44-
});
55+
} catch (err) {
56+
console.log(line);
4557
}
4658
return prev;
4759
}, []);
@@ -62,6 +74,6 @@ export async function gitStash(files: Array<string>, message?: string) {
6274
await runCommand(`stash push${message ? ` -m ${message}` : ''}`, files);
6375
}
6476

65-
export function gitDiff(files: Array<string>) {
66-
return runCommand('diff', files, true);
77+
export function gitDiff(files: Array<string>, flags: Array<string>) {
78+
return runCommand('diff', files, flags, true);
6779
}

src/utils/program.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export function init() {
2323

2424
program
2525
.command('diff')
26-
.description('do "git diff" with style 🤔')
26+
.allowUnknownOption()
27+
.description(
28+
'do "git diff" with style 🤔 (Accept any argument "git diff" accpets)'
29+
)
2730
.action(diff);
2831

2932
program.parse(process.argv);

src/utils/wiz.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
import { Command } from 'commander';
12
import { showFilesChooser, showFilesChooserAnd } from './cli';
23
import { gitStatus, gitAdd, gitReset, gitStash, gitDiff } from './git';
34

45
export const add = withErrorHandler(async () => {
5-
const status = (await gitStatus()).filter(
6-
(file) => file.status !== 'staged'
7-
);
6+
const status = (await gitStatus()).filter((file) => file.status !== 'staged');
87
if (!status.length) {
98
console.log('\x1b[33m', 'There are no changes here. Get back to work 🤓');
109
return;
@@ -15,9 +14,7 @@ export const add = withErrorHandler(async () => {
1514
});
1615

1716
export const reset = withErrorHandler(async () => {
18-
const status = (await gitStatus()).filter(
19-
(file) => file.status === 'staged'
20-
);
17+
const status = (await gitStatus()).filter((file) => file.status === 'staged');
2118
if (!status.length) {
2219
console.log(
2320
'\x1b[33m',
@@ -31,40 +28,51 @@ export const reset = withErrorHandler(async () => {
3128
});
3229

3330
export const stash = withErrorHandler(async () => {
34-
const status = (await gitStatus());
31+
const status = await gitStatus();
3532
if (!status.length) {
3633
console.log('\x1b[33m', 'Stash what exactly 🤥?');
3734
return;
3835
}
3936
const choices = status.map((file) => file.path);
40-
const {files, message} = await showFilesChooserAnd('Files to stash', choices, {
41-
message: {
42-
type: 'input',
43-
message: 'Leave a message to your future self ("-m").. or not, whatever',
37+
const { files, message } = await showFilesChooserAnd(
38+
'Files to stash',
39+
choices,
40+
{
41+
message: {
42+
type: 'input',
43+
message:
44+
'Leave a message to your future self ("-m").. or not, whatever',
45+
},
4446
}
45-
});
47+
);
4648

4749
await gitStash(files, message);
4850
});
4951

50-
export const diff = withErrorHandler(async () => {
51-
const status = (await gitStatus());
52+
export const diff = withErrorHandler(async (comObj: Command) => {
53+
const cached = comObj.args.includes('--cached');
54+
const status = (await gitStatus()).filter(
55+
(file) =>
56+
!file.deleted &&
57+
(cached ? file.status === 'staged' : file.status !== 'staged')
58+
);
59+
5260
if (!status.length) {
5361
console.log('\x1b[33m', `You can't view diff of.. nothing 🧐`);
5462
return;
5563
}
5664
const choices = status.map((file) => file.path);
5765
const files = await showFilesChooser('Files to diff', choices);
5866

59-
await gitDiff(files);
67+
await gitDiff(files, comObj.args);
6068
});
6169

6270
function withErrorHandler(fn: Function) {
63-
return (...args: any[]): Promise<void> => {
71+
return (...args: Array<unknown>): Promise<void> => {
6472
try {
6573
return fn(...args);
6674
} catch (error) {
6775
console.log('\x1b[31m', 'Oops, something went wrong', error);
6876
}
69-
}
70-
}
77+
};
78+
}

webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const webpack = require('webpack');
33
const nodeExternals = require('webpack-node-externals');
44

55
module.exports = {
6+
mode: 'development',
67
entry: './src/index.ts',
78
target: 'node',
89
module: {

0 commit comments

Comments
 (0)