Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: port read to typescript #816

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion @commitlint/cli/src/cli.js
@@ -1,9 +1,10 @@
#!/usr/bin/env node
require('babel-polyfill'); // eslint-disable-line import/no-unassigned-import

import read from '@commitlint/read';

const load = require('@commitlint/load');
const lint = require('@commitlint/lint');
const read = require('@commitlint/read');
const meow = require('meow');
const {merge, pick, isFunction} = require('lodash');
const stdin = require('get-stdin');
Expand Down
26 changes: 3 additions & 23 deletions @commitlint/read/package.json
Expand Up @@ -3,30 +3,13 @@
"version": "8.1.0",
"description": "Read commit messages from a specified range or last edit",
"main": "lib/index.js",
"types": "lib/index.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": "concurrently \"ava -c 4 --verbose --watch\" \"yarn run watch\"",
"test": "ava -c 4 --verbose",
"watch": "babel src --out-dir lib --watch --source-maps"
},
"ava": {
"files": [
"src/**/*.test.js",
"!lib/**/*"
],
"source": [
"src/**/*.js",
"!lib/**/*"
],
"babel": "inherit",
"require": [
"babel-register"
]
"pkg": "pkg-check --skip-import"
},
"babel": {
"presets": [
Expand Down Expand Up @@ -58,13 +41,10 @@
"devDependencies": {
"@commitlint/test": "8.0.0",
"@commitlint/utils": "^8.1.0",
"ava": "0.22.0",
"babel-cli": "6.26.0",
"babel-preset-commitlint": "^8.0.0",
"babel-register": "6.26.0",
"concurrently": "3.6.1",
"cross-env": "5.1.1",
"execa": "0.11.0"
"execa": "2.0.4"
},
"dependencies": {
"@commitlint/top-level": "^8.1.0",
Expand Down
@@ -1,33 +1,33 @@
import {git} from '@commitlint/test';
import test from 'ava';
import execa from 'execa';
import * as sander from '@marionebl/sander';

const {git} = require('@commitlint/test');
const sander = require('@marionebl/sander');

import read from '.';

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

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

const expected = ['foo\n'];
const actual = await read({edit: 'commit-msg-file', cwd});
t.deepEqual(actual, expected);
expect(actual).toEqual(expected);
});

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

await sander.writeFile(cwd, 'alpha.txt', 'alpha');
await execa('git', ['add', '.'], {cwd});
await execa('git', ['commit', '-m', 'alpha'], {cwd});
const expected = ['alpha\n\n'];
const actual = await read({edit: true, cwd});
t.deepEqual(actual, expected);
expect(actual).toEqual(expected);
});

test('get history commit messages', async t => {
const cwd = await git.bootstrap();
test('get history commit messages', async () => {
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 @@ -36,11 +36,11 @@ test('get history commit messages', async t => {

const expected = ['remove alpha\n\n', 'alpha\n\n'];
const actual = await read({cwd});
t.deepEqual(actual, expected);
expect(actual).toEqual(expected);
});

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

Expand All @@ -49,5 +49,5 @@ test('get edit commit message from git subdirectory', async t => {

const expected = ['beta\n\n'];
const actual = await read({edit: true, cwd});
t.deepEqual(actual, expected);
expect(actual).toEqual(expected);
});
50 changes: 34 additions & 16 deletions @commitlint/read/src/index.js → @commitlint/read/src/index.ts
@@ -1,14 +1,24 @@
import path from 'path';
import gitRawCommits from 'git-raw-commits';
import * as sander from '@marionebl/sander';
import {Stats} from 'fs';
import Buffer from 'buffer';
import {Readable} from 'stream';

import toplevel from '@commitlint/top-level';

const gitRawCommits = require('git-raw-commits');
const sander = require('@marionebl/sander');

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

export default getCommitMessages;

// Get commit messages
// Object => Promise<Array<String>>
async function getCommitMessages(settings) {
async function getCommitMessages(settings: Settings): Promise<string[]> {
const {cwd, from, to, edit} = settings;

if (edit) {
Expand All @@ -19,11 +29,13 @@ async function getCommitMessages(settings) {
}

// Get commit messages from history
// Object => Promise<string[]>
function getHistoryCommits(options, opts = {}) {
function getHistoryCommits(
options: {from?: string; to?: string},
opts: {cwd?: string} = {}
): Promise<string[]> {
return new Promise((resolve, reject) => {
const data = [];
gitRawCommits(options, {cwd: opts.cwd})
const data: string[] = [];
(gitRawCommits(options, {cwd: opts.cwd}) as Readable)
.on('data', chunk => data.push(chunk.toString('utf-8')))
.on('error', reject)
.on('end', () => {
Expand All @@ -33,8 +45,10 @@ function getHistoryCommits(options, opts = {}) {
}

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

if (typeof top !== 'string') {
Expand All @@ -43,23 +57,27 @@ async function getEditCommit(cwd, edit) {

const editFilePath = await getEditFilePath(top, edit);

const editFile = await sander.readFile(editFilePath);
const editFile: Buffer = await sander.readFile(editFilePath);
return [`${editFile.toString('utf-8')}\n`];
}

// Get path to recently edited commit message file
// (top: string, edit: any) => Promise<String>
async function getEditFilePath(top, edit) {
let editFilePath;
async function getEditFilePath(
top: string,
edit?: boolean | string
): Promise<string> {
let editFilePath: string;
if (typeof edit === 'string') {
editFilePath = path.resolve(top, edit);
} else {
const dotgitPath = path.join(top, '.git');
const dotgitStats = sander.lstatSync(dotgitPath);
const dotgitStats: Stats = sander.lstatSync(dotgitPath);
if (dotgitStats.isDirectory()) {
editFilePath = path.join(top, '.git/COMMIT_EDITMSG');
} else {
const gitFile = await sander.readFile(dotgitPath, {encoding: 'utf-8'});
const gitFile: string = await sander.readFile(dotgitPath, {
encoding: 'utf-8'
});
const relativeGitPath = gitFile.replace('gitdir: ', '').replace('\n', '');
editFilePath = path.resolve(top, relativeGitPath, 'COMMIT_EDITMSG');
}
Expand Down
15 changes: 15 additions & 0 deletions @commitlint/read/tsconfig.json
@@ -0,0 +1,15 @@
{
"extends": "../../tsconfig.shared.json",
"compilerOptions": {
"composite": true,
"rootDir": "./src",
"outDir": "./lib"
},
"include": [
"./src"
],
"exclude": [
"./src/**/*.test.ts",
"./lib/**/*"
]
}
2 changes: 1 addition & 1 deletion @commitlint/top-level/src/index.ts
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 up('.git', {cwd, type: 'directory'});

if (typeof found !== 'string') {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Expand Up @@ -12,5 +12,6 @@
{ "path": "@commitlint/resolve-extends" },
{ "path": "@commitlint/to-lines" },
{ "path": "@commitlint/top-level" },
{ "path": "@commitlint/read" }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because read depends on top-level, I put the path of read under that of top-level here. Otherwise, the build would fail. I cannot find a reference saying that the order matters, it would be great if anyone can tell me something more about this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is the order here is equal to the order of compilation. If something depends on another thing, it needs to be listed after that package 😄 So you are correct!

]
}