Skip to content

Commit f0dd45d

Browse files
authored
refactor: port read to ts (#922)
* refactor: port read to typescript * fix: use @types/git-raw-commits * fix: adapt to latest master * refactor: unnest getEditFilePath * style: apply autoformatting
1 parent ac71331 commit f0dd45d

12 files changed

+134
-114
lines changed

@commitlint/read/package.json

+4-10
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
"name": "@commitlint/read",
33
"version": "8.3.4",
44
"description": "Read commit messages from a specified range or last edit",
5-
"main": "lib/index.js",
5+
"main": "lib/read.js",
6+
"types": "lib/read.d.ts",
67
"files": [
78
"lib/"
89
],
910
"scripts": {
10-
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
1111
"deps": "dep-check",
12-
"pkg": "pkg-check --skip-import",
13-
"start": "yarn run watch",
14-
"watch": "babel src --out-dir lib --watch --source-maps"
12+
"pkg": "pkg-check --skip-import"
1513
},
1614
"babel": {
1715
"presets": [
@@ -41,13 +39,9 @@
4139
},
4240
"license": "MIT",
4341
"devDependencies": {
44-
"@babel/core": "7.7.7",
45-
"@babel/cli": "7.7.7",
4642
"@commitlint/test": "8.2.0",
4743
"@commitlint/utils": "^8.3.4",
48-
"babel-preset-commitlint": "^8.2.0",
49-
"cross-env": "6.0.3",
50-
"execa": "0.11.0"
44+
"@types/git-raw-commits": "^2.0.0"
5145
},
5246
"dependencies": {
5347
"@commitlint/top-level": "^8.3.4",
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import toplevel from '@commitlint/top-level';
2+
import {getEditFilePath} from './get-edit-file-path';
3+
4+
const sander = require('@marionebl/sander');
5+
6+
// Get recently edited commit message
7+
export async function getEditCommit(
8+
cwd?: string,
9+
edit?: boolean | string
10+
): Promise<string[]> {
11+
const top = await toplevel(cwd);
12+
13+
if (typeof top !== 'string') {
14+
throw new TypeError(`Could not find git root from ${cwd}`);
15+
}
16+
17+
const editFilePath = await getEditFilePath(top, edit);
18+
19+
const editFile: Buffer = await sander.readFile(editFilePath);
20+
return [`${editFile.toString('utf-8')}\n`];
21+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
if (typeof edit === 'string') {
12+
return path.resolve(top, edit);
13+
}
14+
15+
const dotgitPath = path.join(top, '.git');
16+
const dotgitStats: Stats = sander.lstatSync(dotgitPath);
17+
18+
if (dotgitStats.isDirectory()) {
19+
return path.join(top, '.git/COMMIT_EDITMSG');
20+
}
21+
22+
const gitFile: string = await sander.readFile(dotgitPath, {
23+
encoding: 'utf-8'
24+
});
25+
const relativeGitPath = gitFile.replace('gitdir: ', '').replace('\n', '');
26+
return path.resolve(top, relativeGitPath, 'COMMIT_EDITMSG');
27+
}
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.js

-67
This file was deleted.

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import {git} from '@commitlint/test';
22
import execa from 'execa';
3-
import * as sander from '@marionebl/sander';
43

5-
import read from '.';
4+
const sander = require('@marionebl/sander');
5+
6+
import read from './read';
67

78
test('get edit commit message specified by the `edit` flag', async () => {
8-
const cwd = await git.bootstrap();
9+
const cwd: string = await git.bootstrap();
910

1011
await sander.writeFile(cwd, 'commit-msg-file', 'foo');
1112

@@ -15,7 +16,7 @@ test('get edit commit message specified by the `edit` flag', async () => {
1516
});
1617

1718
test('get edit commit message from git root', async () => {
18-
const cwd = await git.bootstrap();
19+
const cwd: string = await git.bootstrap();
1920

2021
await sander.writeFile(cwd, 'alpha.txt', 'alpha');
2122
await execa('git', ['add', '.'], {cwd});
@@ -26,7 +27,7 @@ test('get edit commit message from git root', async () => {
2627
});
2728

2829
test('get history commit messages', async () => {
29-
const cwd = await git.bootstrap();
30+
const cwd: string = await git.bootstrap();
3031
await sander.writeFile(cwd, 'alpha.txt', 'alpha');
3132
await execa('git', ['add', 'alpha.txt'], {cwd});
3233
await execa('git', ['commit', '-m', 'alpha'], {cwd});
@@ -39,7 +40,7 @@ test('get history commit messages', async () => {
3940
});
4041

4142
test('get edit commit message from git subdirectory', async () => {
42-
const cwd = await git.bootstrap();
43+
const cwd: string = await git.bootstrap();
4344
await sander.mkdir(cwd, 'beta');
4445
await sander.writeFile(cwd, 'beta/beta.txt', 'beta');
4546

@commitlint/read/src/read.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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(
13+
settings: GetCommitMessageOptions
14+
): Promise<string[]> {
15+
const {cwd, from, to, edit} = settings;
16+
17+
if (edit) {
18+
return getEditCommit(cwd, edit);
19+
}
20+
21+
return getHistoryCommits({from, to}, {cwd});
22+
}
+11
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

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"extends": "../../tsconfig.shared.json",
3+
"compilerOptions": {
4+
"composite": true,
5+
"rootDir": "./src",
6+
"outDir": "./lib"
7+
},
8+
"include": [
9+
"./src"
10+
],
11+
"exclude": [
12+
"./src/**/*.test.ts",
13+
"./lib/**/*"
14+
],
15+
"references": [
16+
{ "path": "../top-level" }
17+
]
18+
}

@commitlint/top-level/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default toplevel;
1313
/**
1414
* Find the next git root
1515
*/
16-
async function toplevel(cwd: string) {
16+
async function toplevel(cwd?: string) {
1717
const found = await searchDotGit(cwd);
1818

1919
if (typeof found !== 'string') {
@@ -26,7 +26,7 @@ async function toplevel(cwd: string) {
2626
/**
2727
* Search .git, the '.git' can be a file(submodule), also can be a directory(normal)
2828
*/
29-
async function searchDotGit(cwd: string) {
29+
async function searchDotGit(cwd?: string) {
3030
const foundFile = await up('.git', {cwd, type: 'file'});
3131
const foundDir = await up('.git', {cwd, type: 'directory'});
3232

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
{ "path": "@commitlint/parse" },
1414
{ "path": "@commitlint/resolve-extends" },
1515
{ "path": "@commitlint/to-lines" },
16-
{ "path": "@commitlint/top-level" },
16+
{ "path": "@commitlint/top-level" },
17+
{ "path": "@commitlint/read" },
1718
{ "path": "@commitlint/rules" }
1819
]
1920
}

yarn.lock

+10-28
Original file line numberDiff line numberDiff line change
@@ -691,17 +691,6 @@
691691
js-levenshtein "^1.1.3"
692692
semver "^5.5.0"
693693

694-
"@babel/[email protected]", "@babel/register@^7.7.7":
695-
version "7.7.7"
696-
resolved "https://registry.npmjs.org/@babel/register/-/register-7.7.7.tgz#46910c4d1926b9c6096421b23d1f9e159c1dcee1"
697-
integrity sha512-S2mv9a5dc2pcpg/ConlKZx/6wXaEwHeqfo7x/QbXsdCAZm+WJC1ekVvL1TVxNsedTs5y/gG63MhJTEsmwmjtiA==
698-
dependencies:
699-
find-cache-dir "^2.0.0"
700-
lodash "^4.17.13"
701-
make-dir "^2.1.0"
702-
pirates "^4.0.0"
703-
source-map-support "^0.5.16"
704-
705694
"@babel/runtime@^7.0.0":
706695
version "7.7.7"
707696
resolved "https://registry.npmjs.org/@babel/runtime/-/runtime-7.7.7.tgz#194769ca8d6d7790ec23605af9ee3e42a0aa79cf"
@@ -1837,6 +1826,13 @@
18371826
dependencies:
18381827
"@types/node" "*"
18391828

1829+
"@types/git-raw-commits@^2.0.0":
1830+
version "2.0.0"
1831+
resolved "https://packages.atlassian.com/api/npm/npm-remote/@types/git-raw-commits/-/git-raw-commits-2.0.0.tgz#157e9e4709db0748fb1aa623f8927ddd4864bac6"
1832+
integrity sha1-FX6eRwnbB0j7GqYj+JJ93UhkusY=
1833+
dependencies:
1834+
"@types/node" "*"
1835+
18401836
"@types/glob@^7.1.1":
18411837
version "7.1.1"
18421838
resolved "https://registry.npmjs.org/@types/glob/-/glob-7.1.1.tgz#aa59a1c6e3fbc421e07ccd31a944c30eba521575"
@@ -3163,11 +3159,6 @@ common-path-prefix@^1.0.0:
31633159
resolved "https://registry.npmjs.org/common-path-prefix/-/common-path-prefix-1.0.0.tgz#cd52f6f0712e0baab97d6f9732874f22f47752c0"
31643160
integrity sha1-zVL28HEuC6q5fW+XModPIvR3UsA=
31653161

3166-
commondir@^1.0.1:
3167-
version "1.0.1"
3168-
resolved "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b"
3169-
integrity sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs=
3170-
31713162
compare-func@^1.3.1:
31723163
version "1.3.2"
31733164
resolved "https://registry.npmjs.org/compare-func/-/compare-func-1.3.2.tgz#99dd0ba457e1f9bc722b12c08ec33eeab31fa648"
@@ -4415,15 +4406,6 @@ [email protected]:
44154406
statuses "~1.5.0"
44164407
unpipe "~1.0.0"
44174408

4418-
find-cache-dir@^2.0.0:
4419-
version "2.1.0"
4420-
resolved "https://registry.npmjs.org/find-cache-dir/-/find-cache-dir-2.1.0.tgz#8d0f94cd13fe43c6c7c261a0d86115ca918c05f7"
4421-
integrity sha512-Tq6PixE0w/VMFfCgbONnkiQIVol/JJL7nRMi20fqzA4NRs9AfeqMGeRdPi3wIhYkxjeBaWh2rxwapn5Tu3IqOQ==
4422-
dependencies:
4423-
commondir "^1.0.1"
4424-
make-dir "^2.0.0"
4425-
pkg-dir "^3.0.0"
4426-
44274409
44284410
version "2.0.0"
44294411
resolved "https://registry.npmjs.org/find-node-modules/-/find-node-modules-2.0.0.tgz#5db1fb9e668a3d451db3d618cd167cdd59e41b69"
@@ -6780,7 +6762,7 @@ make-dir@^1.0.0:
67806762
dependencies:
67816763
pify "^3.0.0"
67826764

6783-
make-dir@^2.0.0, make-dir@^2.1.0:
6765+
make-dir@^2.1.0:
67846766
version "2.1.0"
67856767
resolved "https://registry.npmjs.org/make-dir/-/make-dir-2.1.0.tgz#5f0310e18b8be898cc07009295a30ae41e91e6f5"
67866768
integrity sha512-LS9X+dc8KLxXCb8dni79fLIIUA5VyZoyjSMCwTluaXA0o27cCK0bhXkpgw+sTXVpPy/lSO57ilRixqk0vDmtRA==
@@ -7998,7 +7980,7 @@ pinkie@^2.0.0:
79987980
resolved "https://registry.npmjs.org/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870"
79997981
integrity sha1-clVrgM+g1IqXToDnckjoDtT3+HA=
80007982

8001-
pirates@^4.0.0, pirates@^4.0.1:
7983+
pirates@^4.0.1:
80027984
version "4.0.1"
80037985
resolved "https://registry.npmjs.org/pirates/-/pirates-4.0.1.tgz#643a92caf894566f91b2b986d2c66950a8e2fb87"
80047986
integrity sha512-WuNqLTbMI3tmfef2TKxlQmAiLHKtFhlsCZnPIpuv2Ow0RDVO8lfy1Opf4NUzlMXLjPl+Men7AuVdX6TA+s+uGA==
@@ -9108,7 +9090,7 @@ source-map-resolve@^0.5.0:
91089090
source-map-url "^0.4.0"
91099091
urix "^0.1.0"
91109092

9111-
source-map-support@^0.5.13, source-map-support@^0.5.16, source-map-support@^0.5.6:
9093+
source-map-support@^0.5.13, source-map-support@^0.5.6:
91129094
version "0.5.16"
91139095
resolved "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.16.tgz#0ae069e7fe3ba7538c64c98515e35339eac5a042"
91149096
integrity sha512-efyLRJDr68D9hBBNIPWFjhpFzURh+KJykQwvMyW5UiZzYwoF6l4YMMDIJJEyFWxWCqfyxLzz6tSfUFR+kXXsVQ==

0 commit comments

Comments
 (0)