Skip to content

Commit

Permalink
Merge pull request #99 from Turbo87/prettier
Browse files Browse the repository at this point in the history
Use `prettier` to format code
  • Loading branch information
Turbo87 authored Jun 16, 2018
2 parents 8ee764a + e5e4143 commit a56360b
Show file tree
Hide file tree
Showing 15 changed files with 306 additions and 169 deletions.
18 changes: 16 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"fix": "npm run lint -- --fix",
"lint": "tslint --project . --format stylish",
"prepublish": "npm run build",
"prettier": "prettier --write 'src/**/*.ts'",
"test": "jest",
"test-ci": "npm run build && jest",
"watch": "npm run build -- --watch"
Expand All @@ -47,9 +48,12 @@
"fs-extra": "^6.0.1",
"jest": "^23.1.0",
"jest-runner-tslint": "^1.0.5",
"prettier": "1.13.5",
"rimraf": "^2.5.4",
"ts-jest": "^22.4.6",
"tslint": "^5.10.0",
"tslint-config-prettier": "^1.13.0",
"tslint-plugin-prettier": "^1.3.0",
"typescript": "^2.9.2"
},
"engines": {
Expand Down Expand Up @@ -89,9 +93,19 @@
{
"runner": "jest-runner-tslint",
"displayName": "lint",
"moduleFileExtensions": ["ts"],
"testMatch": ["<rootDir>/src/**/*.ts"]
"moduleFileExtensions": [
"ts"
],
"testMatch": [
"<rootDir>/src/**/*.ts"
]
}
]
},
"prettier": {
"printWidth": 120,
"tabWidth": 2,
"singleQuote": false,
"trailingComma": "es5"
}
}
71 changes: 46 additions & 25 deletions src/changelog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,33 @@ describe("Changelog", () => {
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: "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(() => [
"v0.2.0",
"v0.1.1",
"v0.1.0",
"v0.0.1",
]);
require("./git").listTagNames.mockImplementation(() => ["v0.2.0", "v0.1.1", "v0.1.0", "v0.0.1"]);

require("./git").changedPaths.mockImplementation(() => []);

Expand All @@ -73,10 +88,7 @@ describe("Changelog", () => {
"https://api.github.com/repos/lerna/lerna-changelog/issues/2": {
number: 2,
title: "This is the commit title for the issue (#2)",
labels: [
{ name: "Type: New Feature" },
{ name: "Status: In Progress" },
],
labels: [{ name: "Type: New Feature" }, { name: "Status: In Progress" }],
user: usersCache["https://api.github.com/users/test-user"],
},
};
Expand Down Expand Up @@ -135,22 +147,31 @@ describe("Changelog", () => {
});

const testCommits = [
{ commitSHA: "a0000004", githubIssue: { user: { login: "test-user-1" } } },
{ commitSHA: "a0000003", githubIssue: { user: { login: "test-user-2" } } },
{
commitSHA: "a0000004",
githubIssue: { user: { login: "test-user-1" } },
},
{
commitSHA: "a0000003",
githubIssue: { user: { login: "test-user-2" } },
},
{ commitSHA: "a0000002", githubIssue: { user: { login: "user-bot" } } },
{ commitSHA: "a0000001" },
];
const committers = await changelog.getCommitters(testCommits);

expect(committers).toEqual([{
login: "test-user-1",
html_url: "https://github.com/test-user-1",
name: "Test User 1",
}, {
login: "test-user-2",
html_url: "https://github.com/test-user-2",
name: "Test User 2",
}]);
expect(committers).toEqual([
{
login: "test-user-1",
html_url: "https://github.com/test-user-1",
name: "Test User 1",
},
{
login: "test-user-2",
html_url: "https://github.com/test-user-2",
name: "Test User 2",
},
]);
});
});
});
48 changes: 28 additions & 20 deletions src/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import progressBar from "./progress-bar";
import * as Configuration from "./configuration";
import findPullRequestId from "./find-pull-request-id";
import * as Git from "./git";
import GithubAPI, {GitHubUserResponse} from "./github-api";
import {CommitInfo, Release} from "./interfaces";
import GithubAPI, { GitHubUserResponse } from "./github-api";
import { CommitInfo, Release } from "./interfaces";
import MarkdownRenderer from "./markdown-renderer";

const UNRELEASED_TAG = "___unreleased___";
Expand Down Expand Up @@ -113,7 +113,7 @@ export default class Changelog {
}
}

return Object.keys(committers).map((k) => committers[k]);
return Object.keys(committers).map(k => committers[k]);
}

private ignoreCommitter(login: string): boolean {
Expand All @@ -126,7 +126,7 @@ export default class Changelog {

private async toCommitInfos(commits: Git.CommitListItem[]): Promise<CommitInfo[]> {
const allTags = await Git.listTagNames();
return commits.map((commit) => {
return commits.map(commit => {
const { sha, refName, summary: message, date } = commit;

let tagsInCommit;
Expand All @@ -152,15 +152,19 @@ export default class Changelog {

private async downloadIssueData(commitInfos: CommitInfo[]) {
progressBar.init(commitInfos.length);
await pMap(commitInfos, async (commitInfo: CommitInfo) => {
progressBar.setTitle(commitInfo.commitSHA);
await pMap(
commitInfos,
async (commitInfo: CommitInfo) => {
progressBar.setTitle(commitInfo.commitSHA);

if (commitInfo.issueNumber) {
commitInfo.githubIssue = await this.github.getIssueData(this.config.repo, commitInfo.issueNumber);
}
if (commitInfo.issueNumber) {
commitInfo.githubIssue = await this.github.getIssueData(this.config.repo, commitInfo.issueNumber);
}

progressBar.tick();
}, { concurrency: 5 });
progressBar.tick();
},
{ concurrency: 5 }
);
progressBar.terminate();
}

Expand Down Expand Up @@ -191,7 +195,7 @@ export default class Changelog {
}
}

return Object.keys(releaseMap).map((tag) => releaseMap[tag]);
return Object.keys(releaseMap).map(tag => releaseMap[tag]);
}

private getToday() {
Expand All @@ -203,24 +207,28 @@ export default class Changelog {
for (const commit of commits) {
if (!commit.githubIssue || !commit.githubIssue.labels) continue;

const labels = commit.githubIssue.labels.map((label) => label.name.toLowerCase());
const labels = commit.githubIssue.labels.map(label => label.name.toLowerCase());

commit.categories = Object.keys(this.config.labels)
.filter((label) => labels.indexOf(label.toLowerCase()) !== -1)
.map((label) => this.config.labels[label]);
.filter(label => labels.indexOf(label.toLowerCase()) !== -1)
.map(label => this.config.labels[label]);
}
}

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

await pMap(commits, async (commit: CommitInfo) => {
progressBar.setTitle(commit.commitSHA);
await pMap(
commits,
async (commit: CommitInfo) => {
progressBar.setTitle(commit.commitSHA);

commit.packages = await this.getListOfUniquePackages(commit.commitSHA);
commit.packages = await this.getListOfUniquePackages(commit.commitSHA);

progressBar.tick();
}, { concurrency: 5 });
progressBar.tick();
},
{ concurrency: 5 }
);

progressBar.terminate();
}
Expand Down
13 changes: 4 additions & 9 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,22 @@ export async function run() {
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",
)
.example("lerna-changelog", "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;
.help().argv;

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

try {
let result = await (new Changelog(options)).createMarkdown();
let result = await new Changelog(options).createMarkdown();
console.log(result);

} catch (e) {
if (e instanceof ConfigurationError) {
console.log(chalk.red(e.message));
Expand Down
22 changes: 13 additions & 9 deletions src/configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,23 @@ describe("Configuration", function() {

tests.forEach(([input, output]) => {
it(`'${input}' -> '${output}'`, function() {
expect(findRepoFromPkg({
repository: {
type: "git",
url: input,
},
})).toEqual(output);
expect(
findRepoFromPkg({
repository: {
type: "git",
url: input,
},
})
).toEqual(output);
});
});

it(`works with shorthand 'repository' syntax`, function() {
expect(findRepoFromPkg({
repository: "https://github.com/babel/ember-cli-babel",
})).toEqual("babel/ember-cli-babel");
expect(
findRepoFromPkg({
repository: "https://github.com/babel/ember-cli-babel",
})
).toEqual("babel/ember-cli-babel");
});
});
});
2 changes: 1 addition & 1 deletion src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function fromPath(rootPath: string): any {
if (!config) {
throw new ConfigurationError(
"Missing changelog config in `lerna.json`.\n" +
"See docs for setup: https://github.com/lerna/lerna-changelog#readme",
"See docs for setup: https://github.com/lerna/lerna-changelog#readme"
);
}

Expand Down
9 changes: 6 additions & 3 deletions src/find-pull-request-id.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ import findPullRequestId from "./find-pull-request-id";

describe("findPullRequestId", function() {
it("finds the id in a GitHub merge commit", function() {
const message = "Merge pull request #42 from Turbo87/pkg-config\n\nRead \"changelog\" config key from \"package.json\" too";
const message =
'Merge pull request #42 from Turbo87/pkg-config\n\nRead "changelog" config key from "package.json" too';
const result = findPullRequestId(message);
expect(result).toEqual("42");
});

it("finds the id in a GitHub squash-merge commit", function() {
const message = "Adjust \"lint\" script (#48)\n\n* bin/cli: Use \"const\" instead of \"var\"\n\n* package.json: Adjust \"lint\" script";
const message =
'Adjust "lint" script (#48)\n\n* bin/cli: Use "const" instead of "var"\n\n* package.json: Adjust "lint" script';
const result = findPullRequestId(message);
expect(result).toEqual("48");
});

it("finds the id in a homu merge commit", function() {
const message = "Auto merge of #7056 - fixTypos:fix_typos, r=Turbo87\n\nfix_typos\n\nThis PR is part of a campaign to fix a lot of typos on github!\nYou can see the progress on https://github.com/fixTypos/fix_typos/\n\nhttps://github.com/client9/misspell";
const message =
"Auto merge of #7056 - fixTypos:fix_typos, r=Turbo87\n\nfix_typos\n\nThis PR is part of a campaign to fix a lot of typos on github!\nYou can see the progress on https://github.com/fixTypos/fix_typos/\n\nhttps://github.com/client9/misspell";
const result = findPullRequestId(message);
expect(result).toEqual("7056");
});
Expand Down
Loading

0 comments on commit a56360b

Please sign in to comment.