From dfe0b1d410ace55754c8d868858675c9df1d53ca Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Sat, 13 Oct 2018 16:00:11 +0200 Subject: [PATCH] Fix `nextVersion` config handling --- src/__mocks__/changelog.ts | 5 +++-- src/changelog.spec.ts | 10 ---------- src/changelog.ts | 12 ++++-------- src/cli.ts | 13 ++++++++++--- src/configuration.ts | 25 +++++++++++++------------ src/markdown-renderer.spec.ts | 1 + src/markdown-renderer.ts | 4 ++-- 7 files changed, 33 insertions(+), 37 deletions(-) diff --git a/src/__mocks__/changelog.ts b/src/__mocks__/changelog.ts index e52a73f9..8b424a26 100644 --- a/src/__mocks__/changelog.ts +++ b/src/__mocks__/changelog.ts @@ -19,9 +19,10 @@ const defaultConfig = { }; class MockedChangelog extends Changelog { - private loadConfig(options: Partial): Configuration { - return Object.assign({}, defaultConfig, options); + constructor(config: Partial) { + super(Object.assign({}, defaultConfig, config)); } + private getToday() { return "2099-01-01"; } diff --git a/src/changelog.spec.ts b/src/changelog.spec.ts index 652ceb88..7d312813 100644 --- a/src/changelog.spec.ts +++ b/src/changelog.spec.ts @@ -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; diff --git a/src/changelog.ts b/src/changelog.ts index a2f3069f..f52e4bee 100644 --- a/src/changelog.ts +++ b/src/changelog.ts @@ -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"; @@ -20,13 +20,13 @@ export default class Changelog { private github: GithubAPI; private renderer: MarkdownRenderer; - constructor(options: Partial = {}) { - 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", }); } @@ -39,10 +39,6 @@ export default class Changelog { return this.renderer.renderMarkdown(releases); } - private loadConfig(options: Partial): Configuration { - return loadConfig(options); - } - private async getCommitInfos(from: string, to: string): Promise { // Step 1: Get list of commits between tag A and B (local) const commits = this.getListOfCommits(from, to); diff --git a/src/cli.ts b/src/cli.ts index 1be9e014..8a0ea4ce 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -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() { @@ -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", diff --git a/src/configuration.ts b/src/configuration.ts index ad3ad9ea..44f2927f 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -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 { +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 { +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); @@ -39,12 +40,12 @@ export function fromPath(rootPath: string, options: Partial = {}) } } - 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) { diff --git a/src/markdown-renderer.spec.ts b/src/markdown-renderer.spec.ts index 30fc798f..2b325ccd 100644 --- a/src/markdown-renderer.spec.ts +++ b/src/markdown-renderer.spec.ts @@ -51,6 +51,7 @@ function renderer(options: any = {}): MarkdownRenderer { return new MarkdownRenderer({ baseIssueUrl: "http://foo.bar/", categories: [], + unreleasedName: "Unreleased", ...options, }); } diff --git a/src/markdown-renderer.ts b/src/markdown-renderer.ts index 20c4bea5..149f52aa 100644 --- a/src/markdown-renderer.ts +++ b/src/markdown-renderer.ts @@ -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[]) {