Skip to content

Commit

Permalink
feat: infer "nextVersion" option from package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-pex committed Aug 6, 2018
1 parent e290695 commit 2eaf353
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export async function run() {
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",
Expand All @@ -54,6 +59,7 @@ export async function run() {
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
7 changes: 5 additions & 2 deletions src/configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ describe("Configuration", function() {

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
21 changes: 20 additions & 1 deletion src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface Configuration {
ignoreCommitters: string[];
cacheDir?: string;
nextVersion: string;
nextVersionFromMetadata?: boolean;
}

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

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

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

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 Down Expand Up @@ -97,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

0 comments on commit 2eaf353

Please sign in to comment.