Skip to content

Commit

Permalink
Merge pull request #125 from Turbo87/config-fix
Browse files Browse the repository at this point in the history
Fix `nextVersion` config handling
  • Loading branch information
Turbo87 committed Oct 14, 2018
2 parents 958dc28 + dfe0b1d commit 17b2a9c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 37 deletions.
5 changes: 3 additions & 2 deletions src/__mocks__/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ const defaultConfig = {
};

class MockedChangelog extends Changelog {
private loadConfig(options: Partial<Configuration>): Configuration {
return Object.assign({}, defaultConfig, options);
constructor(config: Partial<Configuration>) {
super(Object.assign({}, defaultConfig, config));
}

private getToday() {
return "2099-01-01";
}
Expand Down
10 changes: 0 additions & 10 deletions src/changelog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ jest.mock("./git");
jest.mock("./fetch");

describe("Changelog", () => {
describe("contructor", () => {
const MockedChangelog = require("./changelog").default;

it("set cli options", () => {
const changelog = new MockedChangelog({ tagFrom: "1", tagTo: "2" });
expect(changelog.config.tagFrom).toBe("1");
expect(changelog.config.tagTo).toBe("2");
});
});

describe("packageFromPath", () => {
const MockedChangelog = require("./changelog").default;

Expand Down
12 changes: 4 additions & 8 deletions src/changelog.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const pMap = require("p-map");

import progressBar from "./progress-bar";
import { Configuration, load as loadConfig } from "./configuration";
import { Configuration } from "./configuration";
import findPullRequestId from "./find-pull-request-id";
import * as Git from "./git";
import GithubAPI, { GitHubUserResponse } from "./github-api";
Expand All @@ -20,13 +20,13 @@ export default class Changelog {
private github: GithubAPI;
private renderer: MarkdownRenderer;

constructor(options: Partial<Configuration> = {}) {
this.config = this.loadConfig(options);
constructor(config: Configuration) {
this.config = config;
this.github = new GithubAPI(this.config);
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,
unreleasedName: this.config.nextVersion || "Unreleased",
});
}

Expand All @@ -39,10 +39,6 @@ export default class Changelog {
return this.renderer.renderMarkdown(releases);
}

private loadConfig(options: Partial<Configuration>): Configuration {
return loadConfig(options);
}

private async getCommitInfos(from: string, to: string): Promise<CommitInfo[]> {
// Step 1: Get list of commits between tag A and B (local)
const commits = this.getListOfCommits(from, to);
Expand Down
13 changes: 10 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import chalk from "chalk";
import { highlight } from "cli-highlight";

import Changelog from "./changelog";
import { load as loadConfig } from "./configuration";
import ConfigurationError from "./configuration-error";

export async function run() {
Expand Down Expand Up @@ -58,12 +59,18 @@ 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 {
let result = await new Changelog().createMarkdown(options);
let config = loadConfig({
nextVersionFromMetadata: argv["next-version-from-metadata"],
});

if (argv["next-version"]) {
config.nextVersion = argv["next-version"];
}

let result = await new Changelog(config).createMarkdown(options);

let highlighted = highlight(result, {
language: "Markdown",
Expand Down
25 changes: 13 additions & 12 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,27 @@ export interface Configuration {
labels: { [key: string]: string };
ignoreCommitters: string[];
cacheDir?: string;
nextVersion: string;
nextVersion: string | undefined;
nextVersionFromMetadata?: boolean;
}

export function load(options: Partial<Configuration> = {}): Configuration {
export interface ConfigLoaderOptions {
nextVersionFromMetadata?: boolean;
}

export function load(options: ConfigLoaderOptions = {}): Configuration {
let cwd = process.cwd();
let rootPath = execa.sync("git", ["rev-parse", "--show-toplevel"], { cwd }).stdout;

return fromPath(rootPath, options);
}

export function fromPath(rootPath: string, options: Partial<Configuration> = {}): Configuration {
export function fromPath(rootPath: string, options: ConfigLoaderOptions = {}): Configuration {
// Step 1: load partial config from `package.json` or `lerna.json`
let config = fromPackageConfig(rootPath) || fromLernaConfig(rootPath) || {};

// Step 2: override partial config with passed in options
Object.assign(config, options);

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

if (!repo) {
repo = findRepo(rootPath);
Expand All @@ -39,12 +40,12 @@ export function fromPath(rootPath: string, options: Partial<Configuration> = {})
}
}

if (nextVersionFromMetadata) {
if (options.nextVersionFromMetadata || config.nextVersionFromMetadata) {
nextVersion = findNextVersion(rootPath);
}

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

if (!labels) {
Expand Down
1 change: 1 addition & 0 deletions src/markdown-renderer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function renderer(options: any = {}): MarkdownRenderer {
return new MarkdownRenderer({
baseIssueUrl: "http://foo.bar/",
categories: [],
unreleasedName: "Unreleased",
...options,
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/markdown-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ interface Options {
export default class MarkdownRenderer {
private options: Options;

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

public renderMarkdown(releases: Release[]) {
Expand Down

0 comments on commit 17b2a9c

Please sign in to comment.