Skip to content

Commit

Permalink
refactor: port read to ts (#922)
Browse files Browse the repository at this point in the history
* refactor: port read to typescript

* fix: use @types/git-raw-commits

* fix: adapt to latest master

* refactor: unnest getEditFilePath

* style: apply autoformatting
  • Loading branch information
marionebl committed Feb 3, 2020
1 parent ac71331 commit f0dd45d
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 114 deletions.
14 changes: 4 additions & 10 deletions @commitlint/read/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
"name": "@commitlint/read",
"version": "8.3.4",
"description": "Read commit messages from a specified range or last edit",
"main": "lib/index.js",
"main": "lib/read.js",
"types": "lib/read.d.ts",
"files": [
"lib/"
],
"scripts": {
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
"deps": "dep-check",
"pkg": "pkg-check --skip-import",
"start": "yarn run watch",
"watch": "babel src --out-dir lib --watch --source-maps"
"pkg": "pkg-check --skip-import"
},
"babel": {
"presets": [
Expand Down Expand Up @@ -41,13 +39,9 @@
},
"license": "MIT",
"devDependencies": {
"@babel/core": "7.7.7",
"@babel/cli": "7.7.7",
"@commitlint/test": "8.2.0",
"@commitlint/utils": "^8.3.4",
"babel-preset-commitlint": "^8.2.0",
"cross-env": "6.0.3",
"execa": "0.11.0"
"@types/git-raw-commits": "^2.0.0"
},
"dependencies": {
"@commitlint/top-level": "^8.3.4",
Expand Down
21 changes: 21 additions & 0 deletions @commitlint/read/src/get-edit-commit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import toplevel from '@commitlint/top-level';
import {getEditFilePath} from './get-edit-file-path';

const sander = require('@marionebl/sander');

// Get recently edited commit message
export async function getEditCommit(
cwd?: string,
edit?: boolean | string
): Promise<string[]> {
const top = await toplevel(cwd);

if (typeof top !== 'string') {
throw new TypeError(`Could not find git root from ${cwd}`);
}

const editFilePath = await getEditFilePath(top, edit);

const editFile: Buffer = await sander.readFile(editFilePath);
return [`${editFile.toString('utf-8')}\n`];
}
27 changes: 27 additions & 0 deletions @commitlint/read/src/get-edit-file-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import path from 'path';
import {Stats} from 'fs';

const sander = require('@marionebl/sander');

// Get path to recently edited commit message file
export async function getEditFilePath(
top: string,
edit?: boolean | string
): Promise<string> {
if (typeof edit === 'string') {
return path.resolve(top, edit);
}

const dotgitPath = path.join(top, '.git');
const dotgitStats: Stats = sander.lstatSync(dotgitPath);

if (dotgitStats.isDirectory()) {
return path.join(top, '.git/COMMIT_EDITMSG');
}

const gitFile: string = await sander.readFile(dotgitPath, {
encoding: 'utf-8'
});
const relativeGitPath = gitFile.replace('gitdir: ', '').replace('\n', '');
return path.resolve(top, relativeGitPath, 'COMMIT_EDITMSG');
}
10 changes: 10 additions & 0 deletions @commitlint/read/src/get-history-commits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import gitRawCommits from 'git-raw-commits';
import {streamToPromise} from './stream-to-promise';

// Get commit messages from history
export async function getHistoryCommits(
options: {from?: string; to?: string},
opts: {cwd?: string} = {}
): Promise<string[]> {
return streamToPromise(gitRawCommits(options, {cwd: opts.cwd}));
}
67 changes: 0 additions & 67 deletions @commitlint/read/src/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {git} from '@commitlint/test';
import execa from 'execa';
import * as sander from '@marionebl/sander';

import read from '.';
const sander = require('@marionebl/sander');

import read from './read';

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

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

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

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

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

test('get history commit messages', async () => {
const cwd = await git.bootstrap();
const cwd: string = await git.bootstrap();
await sander.writeFile(cwd, 'alpha.txt', 'alpha');
await execa('git', ['add', 'alpha.txt'], {cwd});
await execa('git', ['commit', '-m', 'alpha'], {cwd});
Expand All @@ -39,7 +40,7 @@ test('get history commit messages', async () => {
});

test('get edit commit message from git subdirectory', async () => {
const cwd = await git.bootstrap();
const cwd: string = await git.bootstrap();
await sander.mkdir(cwd, 'beta');
await sander.writeFile(cwd, 'beta/beta.txt', 'beta');

Expand Down
22 changes: 22 additions & 0 deletions @commitlint/read/src/read.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {getHistoryCommits} from './get-history-commits';
import {getEditCommit} from './get-edit-commit';

interface GetCommitMessageOptions {
cwd?: string;
from?: string;
to?: string;
edit?: boolean | string;
}

// Get commit messages
export default async function getCommitMessages(
settings: GetCommitMessageOptions
): Promise<string[]> {
const {cwd, from, to, edit} = settings;

if (edit) {
return getEditCommit(cwd, edit);
}

return getHistoryCommits({from, to}, {cwd});
}
11 changes: 11 additions & 0 deletions @commitlint/read/src/stream-to-promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {Readable} from 'stream';

export function streamToPromise(stream: Readable): Promise<string[]> {
const data: string[] = [];
return new Promise((resolve, reject) =>
stream
.on('data', chunk => data.push(chunk.toString('utf-8')))
.on('error', reject)
.on('end', () => resolve(data))
);
}
18 changes: 18 additions & 0 deletions @commitlint/read/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": "../../tsconfig.shared.json",
"compilerOptions": {
"composite": true,
"rootDir": "./src",
"outDir": "./lib"
},
"include": [
"./src"
],
"exclude": [
"./src/**/*.test.ts",
"./lib/**/*"
],
"references": [
{ "path": "../top-level" }
]
}
4 changes: 2 additions & 2 deletions @commitlint/top-level/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default toplevel;
/**
* Find the next git root
*/
async function toplevel(cwd: string) {
async function toplevel(cwd?: string) {
const found = await searchDotGit(cwd);

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

Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
{ "path": "@commitlint/parse" },
{ "path": "@commitlint/resolve-extends" },
{ "path": "@commitlint/to-lines" },
{ "path": "@commitlint/top-level" },
{ "path": "@commitlint/top-level" },
{ "path": "@commitlint/read" },
{ "path": "@commitlint/rules" }
]
}
38 changes: 10 additions & 28 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -691,17 +691,6 @@
js-levenshtein "^1.1.3"
semver "^5.5.0"

"@babel/register@7.7.7", "@babel/register@^7.7.7":
version "7.7.7"
resolved "https://registry.npmjs.org/@babel/register/-/register-7.7.7.tgz#46910c4d1926b9c6096421b23d1f9e159c1dcee1"
integrity sha512-S2mv9a5dc2pcpg/ConlKZx/6wXaEwHeqfo7x/QbXsdCAZm+WJC1ekVvL1TVxNsedTs5y/gG63MhJTEsmwmjtiA==
dependencies:
find-cache-dir "^2.0.0"
lodash "^4.17.13"
make-dir "^2.1.0"
pirates "^4.0.0"
source-map-support "^0.5.16"

"@babel/runtime@^7.0.0":
version "7.7.7"
resolved "https://registry.npmjs.org/@babel/runtime/-/runtime-7.7.7.tgz#194769ca8d6d7790ec23605af9ee3e42a0aa79cf"
Expand Down Expand Up @@ -1837,6 +1826,13 @@
dependencies:
"@types/node" "*"

"@types/git-raw-commits@^2.0.0":
version "2.0.0"
resolved "https://packages.atlassian.com/api/npm/npm-remote/@types/git-raw-commits/-/git-raw-commits-2.0.0.tgz#157e9e4709db0748fb1aa623f8927ddd4864bac6"
integrity sha1-FX6eRwnbB0j7GqYj+JJ93UhkusY=
dependencies:
"@types/node" "*"

"@types/glob@^7.1.1":
version "7.1.1"
resolved "https://registry.npmjs.org/@types/glob/-/glob-7.1.1.tgz#aa59a1c6e3fbc421e07ccd31a944c30eba521575"
Expand Down Expand Up @@ -3163,11 +3159,6 @@ common-path-prefix@^1.0.0:
resolved "https://registry.npmjs.org/common-path-prefix/-/common-path-prefix-1.0.0.tgz#cd52f6f0712e0baab97d6f9732874f22f47752c0"
integrity sha1-zVL28HEuC6q5fW+XModPIvR3UsA=

commondir@^1.0.1:
version "1.0.1"
resolved "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b"
integrity sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs=

compare-func@^1.3.1:
version "1.3.2"
resolved "https://registry.npmjs.org/compare-func/-/compare-func-1.3.2.tgz#99dd0ba457e1f9bc722b12c08ec33eeab31fa648"
Expand Down Expand Up @@ -4415,15 +4406,6 @@ finalhandler@1.1.2:
statuses "~1.5.0"
unpipe "~1.0.0"

find-cache-dir@^2.0.0:
version "2.1.0"
resolved "https://registry.npmjs.org/find-cache-dir/-/find-cache-dir-2.1.0.tgz#8d0f94cd13fe43c6c7c261a0d86115ca918c05f7"
integrity sha512-Tq6PixE0w/VMFfCgbONnkiQIVol/JJL7nRMi20fqzA4NRs9AfeqMGeRdPi3wIhYkxjeBaWh2rxwapn5Tu3IqOQ==
dependencies:
commondir "^1.0.1"
make-dir "^2.0.0"
pkg-dir "^3.0.0"

find-node-modules@2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/find-node-modules/-/find-node-modules-2.0.0.tgz#5db1fb9e668a3d451db3d618cd167cdd59e41b69"
Expand Down Expand Up @@ -6780,7 +6762,7 @@ make-dir@^1.0.0:
dependencies:
pify "^3.0.0"

make-dir@^2.0.0, make-dir@^2.1.0:
make-dir@^2.1.0:
version "2.1.0"
resolved "https://registry.npmjs.org/make-dir/-/make-dir-2.1.0.tgz#5f0310e18b8be898cc07009295a30ae41e91e6f5"
integrity sha512-LS9X+dc8KLxXCb8dni79fLIIUA5VyZoyjSMCwTluaXA0o27cCK0bhXkpgw+sTXVpPy/lSO57ilRixqk0vDmtRA==
Expand Down Expand Up @@ -7998,7 +7980,7 @@ pinkie@^2.0.0:
resolved "https://registry.npmjs.org/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870"
integrity sha1-clVrgM+g1IqXToDnckjoDtT3+HA=

pirates@^4.0.0, pirates@^4.0.1:
pirates@^4.0.1:
version "4.0.1"
resolved "https://registry.npmjs.org/pirates/-/pirates-4.0.1.tgz#643a92caf894566f91b2b986d2c66950a8e2fb87"
integrity sha512-WuNqLTbMI3tmfef2TKxlQmAiLHKtFhlsCZnPIpuv2Ow0RDVO8lfy1Opf4NUzlMXLjPl+Men7AuVdX6TA+s+uGA==
Expand Down Expand Up @@ -9108,7 +9090,7 @@ source-map-resolve@^0.5.0:
source-map-url "^0.4.0"
urix "^0.1.0"

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

0 comments on commit f0dd45d

Please sign in to comment.