Skip to content

Commit

Permalink
feat(format): include git author, date, and hash
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMason committed Aug 2, 2019
1 parent 9a7c136 commit 3aea523
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
npm install --save-dev eslint eslint-formatter-git-log
```

## 🕹 Usage

```
eslint --format=git-log file.js
```

## ❓ Getting Help

- Get help with issues by creating a
Expand Down
78 changes: 78 additions & 0 deletions src/formatters/git-log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import chalk from 'chalk';
import { execSync } from 'child_process';

export interface EslintMessage {
column: number;
endColumn?: number;
endLine?: number;
fatal?: boolean;
fix?: {
range: [number, number];
text: string;
};
line: number;
message: string;
nodeType: string;
ruleId: string | null;
severity: number;
}

export interface EslintResult {
errorCount: number;
filePath: string;
fixableErrorCount: number;
fixableWarningCount: number;
messages: EslintMessage[];
warningCount: number;
output?: string;
source?: string;
}

export const gitLogFormatter = (results: EslintResult[]) => {
const {
dim,
red,
underline,
yellow,
magenta,
greenBright,
blueBright
} = chalk;
const errorLabel = 'error';
const warningLabel = 'warning';
const GUTTER = ' ';
const WARNING = yellow(warningLabel);
const ERROR = red(errorLabel);

return results.reduce((output, { filePath, messages }) => {
if (messages.length > 0) {
output += `\n${underline(filePath)}\n`;
messages.forEach(
({ ruleId, severity, message, line, column, endLine }) => {
const command = `git blame --date=relative --show-email -L ${line},${endLine} ${filePath}`;
const blame = execSync(command, { encoding: 'utf8' });
const rawLocation = `${line}:${column}`;
const status = severity === 1 ? WARNING : ERROR;
const location = dim(rawLocation);
const rule = ruleId ? dim(ruleId) : '';
const commitMatch = blame.match(/^[^ ]+/) || [''];
const dateMatch = blame.match(/> (.+ ago)/) || ['', ''];
const emailMatch = blame.match(/<([^>]+)>/) || ['', ''];
const commit = magenta(`${commitMatch[0]}`);
const date = greenBright(`(${dateMatch[1].trim()})`);
const email = blueBright(`<${emailMatch[1]}>`);
const locationColumnWith = 8;
const rightAlignLocations = ' '.repeat(
locationColumnWith - rawLocation.length
);
const leftAlignCommitsWithStatuses = ' '.repeat(
rightAlignLocations.length + rawLocation.length + GUTTER.length
);
output += `${rightAlignLocations}${location}${GUTTER}${status}${GUTTER}${message}${GUTTER}${rule}\n`;
output += `${leftAlignCommitsWithStatuses}${commit} ${email} ${date}\n`;
}
);
}
return output;
}, '');
};
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { gitLogFormatter } from './formatters/git-log';

export = gitLogFormatter;

0 comments on commit 3aea523

Please sign in to comment.