Skip to content

Update TSLint and fix issues #91

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

Merged
merged 4 commits into from
Jun 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"changelog": "node ./bin/cli.js",
"clean": "rimraf lib",
"fix": "npm run lint -- --fix",
"lint": "tslint src test",
"lint": "tslint --project . --format stylish",
"prepublish": "npm run build",
"test": "jest",
"test-ci": "npm run build && jest",
Expand All @@ -48,7 +48,7 @@
"jest": "^20.0.4",
"rimraf": "^2.5.4",
"ts-jest": "^20.0.4",
"tslint": "^5.3.2",
"tslint": "^5.10.0",
"typescript": "^2.9.2"
},
"changelog": {
Expand Down
8 changes: 4 additions & 4 deletions src/__mocks__/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ const defaultConfig = {
"Type: Bug": ":bug: Bug Fix",
"Type: Enhancement": ":nail_care: Enhancement",
"Type: Documentation": ":memo: Documentation",
"Type: Maintenance": ":house: Maintenance"
"Type: Maintenance": ":house: Maintenance",
},
cacheDir: ".changelog"
cacheDir: ".changelog",
};

class MockedChangelog extends Changelog {
getConfig() {
private getConfig() {
return defaultConfig;
}
getToday() {
private getToday() {
return "2099-01-01";
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/__mocks__/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
"use strict";

let mockResponses: { [url: string]: string } = {};

Expand All @@ -7,12 +7,11 @@ export default async function fetch(url: string) {
if (mockResponse) {
return { json: () => mockResponse };
}
console.log(url);
throw new Error(`Unknown URL: ${url}`);
};
}

export function __setMockResponses(newMockResponses: { [url: string]: string }) {
mockResponses = newMockResponses
mockResponses = newMockResponses;
}

export function __resetMockResponses() {
Expand Down
2 changes: 1 addition & 1 deletion src/__mocks__/github-api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const GithubAPI = require.requireActual("../github-api").default;

class MockedGithubAPI extends GithubAPI {
getAuthToken() {
private getAuthToken() {
return "123";
}
}
Expand Down
47 changes: 26 additions & 21 deletions src/changelog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ describe("Changelog", () => {
const MockedChangelog = require("./changelog").default;

const TESTS = [
['', ''],
['foo.js', ''],
['packages/foo.js', ''],
['packages/foo/bar.js', 'foo'],
['packages/foo/bar/baz.js', 'foo'],
['packages/@foo/bar.js', '@foo'],
['packages/@foo/bar/baz.js', '@foo/bar'],
["", ""],
["foo.js", ""],
["packages/foo.js", ""],
["packages/foo/bar.js", "foo"],
["packages/foo/bar/baz.js", "foo"],
["packages/@foo/bar.js", "@foo"],
["packages/@foo/bar/baz.js", "@foo/bar"],
];

for (let [input, expected] of TESTS) {
Expand All @@ -41,11 +41,16 @@ describe("Changelog", () => {
require("./fetch").__resetMockResponses();

require("./git").listCommits.mockImplementation(() => [
{ sha: "a0000005", refName:"HEAD -> master, tag: v0.2.0, origin/master, origin/HEAD", summary: "chore(release): releasing component", date: "2017-01-01" },
{ sha: "a0000004", refName:"", summary: "Merge pull request #2 from my-feature", date: "2017-01-01" },
{ sha: "a0000003", refName:"", summary: "feat(module) Add new module (#2)", date: "2017-01-01" },
{ sha: "a0000002", refName:"", summary: "refactor(module) Simplify implementation", date: "2017-01-01" },
{ sha: "a0000001", refName:"tag: v0.1.0", summary: "chore(release): releasing component", date: "2017-01-01" },
{
sha: "a0000005",
refName: "HEAD -> master, tag: v0.2.0, origin/master, origin/HEAD",
summary: "chore(release): releasing component",
date: "2017-01-01",
},
{ sha: "a0000004", refName: "", summary: "Merge pull request #2 from my-feature", date: "2017-01-01" },
{ sha: "a0000003", refName: "", summary: "feat(module) Add new module (#2)", date: "2017-01-01" },
{ sha: "a0000002", refName: "", summary: "refactor(module) Simplify implementation", date: "2017-01-01" },
{ sha: "a0000001", refName: "tag: v0.1.0", summary: "chore(release): releasing component", date: "2017-01-01" },
]);

require("./git").listTagNames.mockImplementation(() => [
Expand All @@ -61,7 +66,7 @@ describe("Changelog", () => {
"https://api.github.com/users/test-user": {
login: "test-user",
html_url: "https://github.com/test-user",
name: "Test User"
name: "Test User",
},
};
const issuesCache = {
Expand All @@ -73,7 +78,7 @@ describe("Changelog", () => {
{ name: "Status: In Progress" },
],
user: usersCache["https://api.github.com/users/test-user"],
}
},
};
require("./fetch").__setMockResponses({
...usersCache,
Expand Down Expand Up @@ -102,22 +107,22 @@ describe("Changelog", () => {
"https://api.github.com/users/test-user": {
login: "test-user",
html_url: "https://github.com/test-user",
name: "Test User"
name: "Test User",
},
"https://api.github.com/users/test-user-1": {
login: "test-user-1",
html_url: "https://github.com/test-user-1",
name: "Test User 1"
name: "Test User 1",
},
"https://api.github.com/users/test-user-2": {
login: "test-user-2",
html_url: "https://github.com/test-user-2",
name: "Test User 2"
name: "Test User 2",
},
"https://api.github.com/users/user-bot": {
login: "user-bot",
html_url: "https://github.com/user-bot",
name: "User Bot"
name: "User Bot",
},
};
require("./fetch").__setMockResponses(usersCache);
Expand All @@ -126,7 +131,7 @@ describe("Changelog", () => {
it("get list of valid commiters", async () => {
const MockedChangelog = require("./changelog").default;
const changelog = new MockedChangelog({
ignoreCommitters: ["user-bot"]
ignoreCommitters: ["user-bot"],
});

const testCommits = [
Expand All @@ -140,11 +145,11 @@ describe("Changelog", () => {
expect(committers).toEqual([{
login: "test-user-1",
html_url: "https://github.com/test-user-1",
name: "Test User 1"
name: "Test User 1",
}, {
login: "test-user-2",
html_url: "https://github.com/test-user-2",
name: "Test User 2"
name: "Test User 2",
}]);
});
});
Expand Down
56 changes: 28 additions & 28 deletions src/changelog.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const pMap = require("p-map");

import progressBar from "./progress-bar";
import progressBar from "./progress-bar";
import * as Configuration from "./configuration";
import findPullRequestId from "./find-pull-request-id";
import findPullRequestId from "./find-pull-request-id";
import * as Git from "./git";
import GithubAPI, {GitHubUserResponse} from "./github-api";
import {CommitInfo, Release} from "./interfaces";
Expand All @@ -16,9 +16,9 @@ interface Options {
}

export default class Changelog {
config: any;
github: GithubAPI;
renderer: MarkdownRenderer;
private config: any;
private github: GithubAPI;
private renderer: MarkdownRenderer;

constructor(options: Options = {}) {
this.config = Object.assign(this.getConfig(), options);
Expand All @@ -29,11 +29,17 @@ export default class Changelog {
});
}

getConfig() {
public async createMarkdown() {
const releases = await this.listReleases();

return this.renderer.renderMarkdown(releases);
}

private getConfig() {
return Configuration.fromGitRoot(process.cwd());
}

async getCommitInfos(): Promise<CommitInfo[]> {
private async getCommitInfos(): Promise<CommitInfo[]> {
// Step 1: Get list of commits between tag A and B (local)
const commits = await this.getListOfCommits();

Expand All @@ -52,7 +58,7 @@ export default class Changelog {
return commitInfos;
}

async listReleases(): Promise<Release[]> {
private async listReleases(): Promise<Release[]> {
// Get all info about commits in a certain tags range
const commits = await this.getCommitInfos();

Expand All @@ -65,20 +71,14 @@ export default class Changelog {
return releases;
}

async createMarkdown() {
const releases = await this.listReleases();

return this.renderer.renderMarkdown(releases);
}

async getListOfUniquePackages(sha: string): Promise<string[]> {
private async getListOfUniquePackages(sha: string): Promise<string[]> {
return (await Git.changedPaths(sha))
.map(path => this.packageFromPath(path))
.filter(Boolean)
.filter(onlyUnique);
}

packageFromPath(path: string): string {
private packageFromPath(path: string): string {
const parts = path.split("/");
if (parts[0] !== "packages" || parts.length < 3) {
return "";
Expand All @@ -91,15 +91,15 @@ export default class Changelog {
return parts[1];
}

async getListOfCommits(): Promise<Git.CommitListItem[]> {
private async getListOfCommits(): Promise<Git.CommitListItem[]> {
// Determine the tags range to get the commits for. Custom from/to can be
// provided via command-line options.
// Default is "from last tag".
const tagFrom = this.config.tagFrom || (await Git.lastTag());
return Git.listCommits(tagFrom, this.config.tagTo);
}

async getCommitters(commits: CommitInfo[]): Promise<GitHubUserResponse[]> {
private async getCommitters(commits: CommitInfo[]): Promise<GitHubUserResponse[]> {
const committers: { [id: string]: GitHubUserResponse } = {};

for (const commit of commits) {
Expand All @@ -116,15 +116,15 @@ export default class Changelog {
return Object.keys(committers).map((k) => committers[k]);
}

ignoreCommitter(login: string): boolean {
private ignoreCommitter(login: string): boolean {
if (!this.config.ignoreCommitters) {
return false;
}

return this.config.ignoreCommitters.some((c: string) => c === login || login.indexOf(c) > -1)
return this.config.ignoreCommitters.some((c: string) => c === login || login.indexOf(c) > -1);
}

async toCommitInfos(commits: Git.CommitListItem[]): Promise<CommitInfo[]> {
private async toCommitInfos(commits: Git.CommitListItem[]): Promise<CommitInfo[]> {
const allTags = await Git.listTagNames();
return commits.map((commit) => {
const { sha, refName, summary: message, date } = commit;
Expand All @@ -140,7 +140,7 @@ export default class Changelog {

return {
commitSHA: sha,
message: message,
message,
// Note: Only merge commits or commits referencing an issue / PR
// will be kept in the changelog.
tags: tagsInCommit,
Expand All @@ -150,7 +150,7 @@ export default class Changelog {
});
}

async downloadIssueData(commitInfos: CommitInfo[]) {
private async downloadIssueData(commitInfos: CommitInfo[]) {
progressBar.init(commitInfos.length);
await pMap(commitInfos, async (commitInfo: CommitInfo) => {
progressBar.setTitle(commitInfo.commitSHA);
Expand All @@ -164,7 +164,7 @@ export default class Changelog {
progressBar.terminate();
}

groupByRelease(commits: CommitInfo[]): Release[] {
private groupByRelease(commits: CommitInfo[]): Release[] {
// Analyze the commits and group them by tag.
// This is useful to generate multiple release logs in case there are
// multiple release tags.
Expand Down Expand Up @@ -194,12 +194,12 @@ export default class Changelog {
return Object.keys(releaseMap).map((tag) => releaseMap[tag]);
}

getToday() {
private getToday() {
const date = new Date().toISOString();
return date.slice(0, date.indexOf("T"));
}

fillInCategories(commits: CommitInfo[]) {
private fillInCategories(commits: CommitInfo[]) {
for (const commit of commits) {
if (!commit.githubIssue || !commit.githubIssue.labels) continue;

Expand All @@ -211,7 +211,7 @@ export default class Changelog {
}
}

async fillInPackages(commits: CommitInfo[]) {
private async fillInPackages(commits: CommitInfo[]) {
progressBar.init(commits.length);

await pMap(commits, async (commit: CommitInfo) => {
Expand All @@ -225,7 +225,7 @@ export default class Changelog {
progressBar.terminate();
}

async fillInContributors(releases: Release[]) {
private async fillInContributors(releases: Release[]) {
for (const release of releases) {
release.contributors = await this.getCommitters(release.commits);
}
Expand Down
16 changes: 9 additions & 7 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* tslint:disable:no-console */

const chalk = require("chalk");

import Changelog from "./changelog";
Expand All @@ -9,28 +11,28 @@ export async function run() {
.options({
"tag-from": {
type: "string",
desc: "A git tag that determines the lower bound of the range of commits (defaults to last available)"
desc: "A git tag that determines the lower bound of the range of commits (defaults to last available)",
},
"tag-to": {
type: "string",
desc: "A git tag that determines the upper bound of the range of commits"
}
desc: "A git tag that determines the upper bound of the range of commits",
},
})
.example(
"lerna-changelog",
"create a changelog for the changes after the latest available tag"
"create a changelog for the changes after the latest available tag",
)
.example(
"lerna-changelog --tag-from 0.1.0 --tag-to 0.3.0",
"create a changelog for the changes in all tags within the given range"
"create a changelog for the changes in all tags within the given range",
)
.version()
.help()
.argv;

let options = {
tagFrom: argv['tag-from'],
tagTo: argv['tag-to'],
tagFrom: argv["tag-from"],
tagTo: argv["tag-to"],
};

try {
Expand Down
Loading