Skip to content

Commit 11ac6aa

Browse files
committed
fix: use @types/git-raw-commits
1 parent ade7af3 commit 11ac6aa

File tree

10 files changed

+181
-191
lines changed

10 files changed

+181
-191
lines changed

@commitlint/read/package.json

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"name": "@commitlint/read",
33
"version": "8.1.0",
44
"description": "Read commit messages from a specified range or last edit",
5-
"main": "lib/index.js",
6-
"types": "lib/index.d.ts",
5+
"main": "lib/read.js",
6+
"types": "lib/read.d.ts",
77
"files": [
88
"lib/"
99
],
@@ -41,15 +41,11 @@
4141
"devDependencies": {
4242
"@commitlint/test": "8.0.0",
4343
"@commitlint/utils": "^8.1.0",
44-
"babel-cli": "6.26.0",
45-
"babel-preset-commitlint": "^8.0.0",
46-
"babel-register": "6.26.0",
47-
"execa": "2.0.4"
44+
"@types/git-raw-commits": "^2.0.0"
4845
},
4946
"dependencies": {
5047
"@commitlint/top-level": "^8.1.0",
5148
"@marionebl/sander": "^0.6.0",
52-
"babel-runtime": "^6.23.0",
5349
"git-raw-commits": "^1.3.0"
5450
}
5551
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import toplevel from '@commitlint/top-level';
2+
3+
// Get recently edited commit message
4+
export async function getEditCommit(
5+
cwd?: string,
6+
edit?: boolean | string
7+
): Promise<string[]> {
8+
const top = await toplevel(cwd);
9+
10+
if (typeof top !== 'string') {
11+
throw new TypeError(`Could not find git root from ${cwd}`);
12+
}
13+
14+
const editFilePath = await getEditFilePath(top, edit);
15+
16+
const editFile: Buffer = await sander.readFile(editFilePath);
17+
return [`${editFile.toString('utf-8')}\n`];
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import path from 'path';
2+
import {Stats} from 'fs';
3+
4+
const sander = require('@marionebl/sander');
5+
6+
// Get path to recently edited commit message file
7+
export async function getEditFilePath(
8+
top: string,
9+
edit?: boolean | string
10+
): Promise<string> {
11+
let editFilePath: string;
12+
if (typeof edit === 'string') {
13+
editFilePath = path.resolve(top, edit);
14+
} else {
15+
const dotgitPath = path.join(top, '.git');
16+
const dotgitStats: Stats = sander.lstatSync(dotgitPath);
17+
if (dotgitStats.isDirectory()) {
18+
editFilePath = path.join(top, '.git/COMMIT_EDITMSG');
19+
} else {
20+
const gitFile: string = await sander.readFile(dotgitPath, {
21+
encoding: 'utf-8'
22+
});
23+
const relativeGitPath = gitFile.replace('gitdir: ', '').replace('\n', '');
24+
editFilePath = path.resolve(top, relativeGitPath, 'COMMIT_EDITMSG');
25+
}
26+
}
27+
28+
return editFilePath;
29+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import gitRawCommits from 'git-raw-commits';
2+
import {streamToPromise} from './stream-to-promise';
3+
4+
// Get commit messages from history
5+
export async function getHistoryCommits(
6+
options: {from?: string; to?: string},
7+
opts: {cwd?: string} = {}
8+
): Promise<string[]> {
9+
return streamToPromise(gitRawCommits(options, {cwd: opts.cwd}));
10+
}

@commitlint/read/src/index.ts

Lines changed: 0 additions & 87 deletions
This file was deleted.

@commitlint/read/src/index.test.ts renamed to @commitlint/read/src/read.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import execa from 'execa';
33
const {git} = require('@commitlint/test');
44
const sander = require('@marionebl/sander');
55

6-
import read from '.';
6+
import read from './read';
77

88
test('get edit commit message specified by the `edit` flag', async () => {
99
const cwd: string = await git.bootstrap();

@commitlint/read/src/read.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { getHistoryCommits } from './get-history-commits';
2+
import { getEditCommit } from './get-edit-commit';
3+
4+
interface GetCommitMessageOptions {
5+
cwd?: string;
6+
from?: string;
7+
to?: string;
8+
edit?: boolean | string;
9+
}
10+
11+
// Get commit messages
12+
export default async function getCommitMessages(settings: GetCommitMessageOptions): Promise<string[]> {
13+
const {cwd, from, to, edit} = settings;
14+
15+
if (edit) {
16+
return getEditCommit(cwd, edit);
17+
}
18+
19+
return getHistoryCommits({from, to}, {cwd});
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Readable } from 'stream';
2+
3+
export function streamToPromise(stream: Readable): Promise<string[]> {
4+
const data: string[] = [];
5+
return new Promise((resolve, reject) => (
6+
stream
7+
.on('data', chunk => data.push(chunk.toString('utf-8')))
8+
.on('error', reject)
9+
.on('end', () => resolve(data))
10+
));
11+
}

@commitlint/read/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@
1111
"exclude": [
1212
"./src/**/*.test.ts",
1313
"./lib/**/*"
14+
],
15+
"references": [
16+
{ "path": "../top-level" }
1417
]
1518
}

0 commit comments

Comments
 (0)