Skip to content

Commit

Permalink
Merge pull request #117 from alex-pex/feature/set-next-version
Browse files Browse the repository at this point in the history
Allow "Unreleased" commit group to be renamed
  • Loading branch information
Turbo87 authored Sep 19, 2018
2 parents a536a41 + 2eaf353 commit 6fc2941
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ The supported options are:
- `repo`: Your "org/repo" on GitHub
(automatically inferred from the `package.json` file)

- `nextVersion`: Title for unreleased commits
(e.g. `Unreleased`)

- `labels`: GitHub PR labels mapped to changelog section headers

- `ignoreCommitters`: List of committers to ignore (exact or partial match).
Expand Down
1 change: 1 addition & 0 deletions src/__mocks__/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const defaultConfig = {
},
ignoreCommitters: [],
cacheDir: ".changelog",
nextVersion: "Unreleased",
};

class MockedChangelog extends Changelog {
Expand Down
14 changes: 14 additions & 0 deletions src/__snapshots__/markdown-renderer.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,17 @@ exports[`MarkdownRenderer renderContributorList renders a list of GitHub users 1
- Tobias Bieniek ([@Turbo87](https://github.com/Turbo87))
- [@hzoo](https://github.com/hzoo)"
`;

exports[`MarkdownRenderer renderRelease renders unreleased commits 1`] = `
"## Unreleased (2018-07-10)
#### :rocket: New Feature
* My cool PR ([@hzoo](http://hzoo.com))"
`;

exports[`MarkdownRenderer renderRelease renders unreleased commits, with named next release 1`] = `
"## v2.0.0-alpha.0 (2018-07-10)
#### :rocket: New Feature
* My cool PR ([@hzoo](http://hzoo.com))"
`;
1 change: 1 addition & 0 deletions src/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default class Changelog {
this.renderer = new MarkdownRenderer({
categories: Object.keys(this.config.labels).map(key => this.config.labels[key]),
baseIssueUrl: this.github.getBaseIssueUrl(this.config.repo),
unreleasedName: this.config.nextVersion,
});
}

Expand Down
17 changes: 16 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,21 @@ export async function run() {
type: "string",
desc: "A git tag that determines the upper bound of the range of commits",
},
"next-version": {
type: "string",
desc: "The name of the next version",
default: "Unreleased",
},
"next-version-from-metadata": {
type: "boolean",
desc: "Infer the name of the next version from package metadata",
default: false,
},
})
.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, under "Unreleased" section'
)
.example(
"lerna-changelog --from=0.1.0 --to=0.3.0",
"create a changelog for the changes in all tags within the given range"
Expand All @@ -45,6 +58,8 @@ export async function run() {
let options = {
tagFrom: argv["from"] || argv["tag-from"],
tagTo: argv["to"] || argv["tag-to"],
nextVersion: argv["next-version"],
nextVersionFromMetadata: argv["next-version-from-metadata"],
};

try {
Expand Down
13 changes: 9 additions & 4 deletions src/configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,37 @@ describe("Configuration", function() {

it("reads the configuration from 'lerna.json'", function() {
fs.writeJsonSync(path.join(tmpDir, "lerna.json"), {
changelog: { repo: "foo/bar" },
changelog: { repo: "foo/bar", nextVersion: "next" },
});

const result = fromPath(tmpDir);
expect(result.nextVersion).toEqual("next");
expect(result.repo).toEqual("foo/bar");
});

it("reads the configuration from 'package.json'", function() {
fs.writeJsonSync(path.join(tmpDir, "package.json"), {
changelog: { repo: "foo/bar" },
changelog: { repo: "foo/bar", nextVersion: "next" },
});

const result = fromPath(tmpDir);
expect(result.nextVersion).toEqual("next");
expect(result.repo).toEqual("foo/bar");
});

it("prefers 'package.json' over 'lerna.json'", function() {
fs.writeJsonSync(path.join(tmpDir, "lerna.json"), {
changelog: { repo: "foo/lerna" },
version: "1.0.0-lerna.0",
changelog: { repo: "foo/lerna", nextVersionFromMetadata: true },
});

fs.writeJsonSync(path.join(tmpDir, "package.json"), {
changelog: { repo: "foo/package" },
version: "1.0.0-package.0",
changelog: { repo: "foo/package", nextVersionFromMetadata: true },
});

const result = fromPath(tmpDir);
expect(result.nextVersion).toEqual("v1.0.0-package.0");
expect(result.repo).toEqual("foo/package");
});

Expand Down
25 changes: 23 additions & 2 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export interface Configuration {
labels: { [key: string]: string };
ignoreCommitters: string[];
cacheDir?: string;
nextVersion: string;
nextVersionFromMetadata?: boolean;
}

export function load(options: Partial<Configuration> = {}): Configuration {
Expand All @@ -28,15 +30,23 @@ export function fromPath(rootPath: string, options: Partial<Configuration> = {})
Object.assign(config, options);

// Step 3: fill partial config with defaults
let { repo, labels, cacheDir, ignoreCommitters } = config;
let { repo, nextVersion, nextVersionFromMetadata, labels, cacheDir, ignoreCommitters } = config;

if (!repo) {
repo = findRepo(rootPath);
if (!repo) {
throw new ConfigurationError('Could not infer "repo from the "package.json" file.');
throw new ConfigurationError('Could not infer "repo" from the "package.json" file.');
}
}

if (nextVersionFromMetadata) {
nextVersion = findNextVersion(rootPath);
}

if (!nextVersion) {
throw new ConfigurationError('Could not infer "nextVersion" from the "package.json" file.');
}

if (!labels) {
labels = {
breaking: ":boom: Breaking Change",
Expand All @@ -60,6 +70,7 @@ export function fromPath(rootPath: string, options: Partial<Configuration> = {})

return {
repo,
nextVersion,
rootPath,
labels,
ignoreCommitters,
Expand Down Expand Up @@ -95,6 +106,16 @@ function findRepo(rootPath: string): string | undefined {
return findRepoFromPkg(pkg);
}

function findNextVersion(rootPath: string): string | undefined {
const pkgPath = path.join(rootPath, "package.json");
const lernaPath = path.join(rootPath, "lerna.json");

const pkg = fs.existsSync(pkgPath) ? JSON.parse(fs.readFileSync(pkgPath)) : {};
const lerna = fs.existsSync(lernaPath) ? JSON.parse(fs.readFileSync(lernaPath)) : {};

return pkg.version ? `v${pkg.version}` : lerna.version ? `v${lerna.version}` : undefined;
}

export function findRepoFromPkg(pkg: any): string | undefined {
const url = pkg.repository.url || pkg.repository;
const normalized = normalize(url).url;
Expand Down
47 changes: 46 additions & 1 deletion src/markdown-renderer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { CommitInfo } from "./interfaces";
import { CommitInfo, Release } from "./interfaces";
import MarkdownRenderer from "./markdown-renderer";

const UNRELEASED_TAG = "___unreleased___";

const BASIC_COMMIT = {
githubIssue: {
title: "My cool PR",
Expand Down Expand Up @@ -53,6 +55,10 @@ function renderer(options: any = {}): MarkdownRenderer {
});
}

function getToday() {
return "2018-07-10";
}

describe("MarkdownRenderer", () => {
describe("renderPackageNames", () => {
it(`renders an empty list of package names as "Other"`, () => {
Expand Down Expand Up @@ -172,4 +178,43 @@ describe("MarkdownRenderer", () => {
expect(commitsByCategory).toMatchSnapshot();
});
});

describe("renderRelease", () => {
it(`renders unreleased commits`, () => {
const release: Release = {
name: UNRELEASED_TAG,
date: getToday(),
commits: [
{
...BASIC_COMMIT,
categories: [":rocket: New Feature"],
},
],
};
const options = {
categories: [":rocket: New Feature"],
};
const result = renderer(options).renderRelease(release);
expect(result).toMatchSnapshot();
});

it(`renders unreleased commits, with named next release`, () => {
const release: Release = {
name: UNRELEASED_TAG,
date: getToday(),
commits: [
{
...BASIC_COMMIT,
categories: [":rocket: New Feature"],
},
],
};
const options = {
categories: [":rocket: New Feature"],
unreleasedName: "v2.0.0-alpha.0",
};
const result = renderer(options).renderRelease(release);
expect(result).toMatchSnapshot();
});
});
});
7 changes: 4 additions & 3 deletions src/markdown-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ interface CategoryInfo {
interface Options {
categories: string[];
baseIssueUrl: string;
unreleasedName: string;
}

export default class MarkdownRenderer {
private options: Options;

constructor(options: Options) {
this.options = options;
constructor({ unreleasedName = "Unreleased", ...options }: Options) {
this.options = { unreleasedName, ...options };
}

public renderMarkdown(releases: Release[]) {
Expand All @@ -36,7 +37,7 @@ export default class MarkdownRenderer {
// Skip this iteration if there are no commits available for the release
if (categoriesWithCommits.length === 0) return "";

const releaseTitle = release.name === UNRELEASED_TAG ? "Unreleased" : release.name;
const releaseTitle = release.name === UNRELEASED_TAG ? this.options.unreleasedName : release.name;

let markdown = `## ${releaseTitle} (${release.date})`;

Expand Down

0 comments on commit 6fc2941

Please sign in to comment.