@kazupon/lerna-changelog
Version:
Generate a changelog for a lerna monorepo
108 lines (107 loc) • 3.61 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findRepoFromPkg = exports.fromPath = exports.load = void 0;
const fs = require('fs');
const path = require('path');
const hostedGitInfo = require('hosted-git-info');
const execa_1 = __importDefault(require("execa"));
const configuration_error_1 = __importDefault(require("./configuration-error"));
async function load(options = {}) {
const cwd = process.cwd();
const { stdout: rootPath } = await execa_1.default('git', ['rev-parse', '--show-toplevel'], {
cwd
});
return Promise.resolve(fromPath(rootPath, options));
}
exports.load = load;
function fromPath(rootPath, options = {}) {
const config = fromPackageConfig(rootPath) || fromLernaConfig(rootPath) || {};
let { repo, nextVersion, labels, cacheDir, ignoreCommitters } = config;
if (!repo) {
repo = findRepo(rootPath);
if (!repo) {
throw new configuration_error_1.default('Could not infer "repo" from the "package.json" file.');
}
}
if (options.nextVersionFromMetadata || config.nextVersionFromMetadata) {
nextVersion = findNextVersion(rootPath);
if (!nextVersion) {
throw new configuration_error_1.default('Could not infer "nextVersion" from the "package.json" file.');
}
}
if (!labels) {
labels = {
breaking: ':boom: Breaking Change',
enhancement: ':rocket: Enhancement',
bug: ':bug: Bug Fix',
documentation: ':memo: Documentation',
internal: ':house: Internal'
};
}
if (!ignoreCommitters) {
ignoreCommitters = [
'dependabot-bot',
'dependabot[bot]',
'greenkeeperio-bot',
'greenkeeper[bot]',
'renovate-bot',
'renovate[bot]'
];
}
return {
repo,
nextVersion,
rootPath,
labels,
ignoreCommitters,
cacheDir
};
}
exports.fromPath = fromPath;
function fromLernaConfig(rootPath) {
const lernaPath = path.join(rootPath, 'lerna.json');
if (fs.existsSync(lernaPath)) {
return JSON.parse(fs.readFileSync(lernaPath)).changelog;
}
}
function fromPackageConfig(rootPath) {
const pkgPath = path.join(rootPath, 'package.json');
if (fs.existsSync(pkgPath)) {
return JSON.parse(fs.readFileSync(pkgPath)).changelog;
}
}
function findRepo(rootPath) {
const pkgPath = path.join(rootPath, 'package.json');
if (!fs.existsSync(pkgPath)) {
return;
}
const pkg = JSON.parse(fs.readFileSync(pkgPath));
if (!pkg.repository) {
return;
}
return findRepoFromPkg(pkg);
}
function findNextVersion(rootPath) {
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;
}
function findRepoFromPkg(pkg) {
const url = pkg.repository.url || pkg.repository;
const info = hostedGitInfo.fromUrl(url);
if (info && info.type === 'github') {
return `${info.user}/${info.project}`;
}
}
exports.findRepoFromPkg = findRepoFromPkg;