1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.findRepoFromPkg = exports.fromPath = exports.load = void 0;
|
4 | const fs = require("fs");
|
5 | const path = require("path");
|
6 | const execa = require("execa");
|
7 | const hostedGitInfo = require("hosted-git-info");
|
8 | const configuration_error_1 = require("./configuration-error");
|
9 | const git_1 = require("./git");
|
10 | function load(options = {}) {
|
11 | let rootPath = (0, git_1.getRootPath)();
|
12 | return fromPath(rootPath, options);
|
13 | }
|
14 | exports.load = load;
|
15 | function fromPath(rootPath, options = {}) {
|
16 | let config = fromPackageConfig(rootPath) || fromLernaConfig(rootPath) || {};
|
17 | if (options.repo) {
|
18 | config.repo = options.repo;
|
19 | }
|
20 | let { repo, nextVersion, labels, cacheDir, ignoreCommitters } = config;
|
21 | if (!repo) {
|
22 | repo = findRepo(rootPath);
|
23 | if (!repo) {
|
24 | throw new configuration_error_1.default('Could not infer "repo" from the "package.json" file.');
|
25 | }
|
26 | }
|
27 | if (options.nextVersionFromMetadata || config.nextVersionFromMetadata) {
|
28 | nextVersion = findNextVersion(rootPath);
|
29 | if (!nextVersion) {
|
30 | throw new configuration_error_1.default('Could not infer "nextVersion" from the "package.json" file.');
|
31 | }
|
32 | }
|
33 | if (!labels) {
|
34 | labels = {
|
35 | breaking: ":boom: Breaking Change",
|
36 | enhancement: ":rocket: Enhancement",
|
37 | bug: ":bug: Bug Fix",
|
38 | documentation: ":memo: Documentation",
|
39 | internal: ":house: Internal",
|
40 | };
|
41 | }
|
42 | if (!ignoreCommitters) {
|
43 | ignoreCommitters = [
|
44 | "dependabot-bot",
|
45 | "dependabot[bot]",
|
46 | "dependabot-preview[bot]",
|
47 | "greenkeeperio-bot",
|
48 | "greenkeeper[bot]",
|
49 | "renovate-bot",
|
50 | "renovate[bot]",
|
51 | ];
|
52 | }
|
53 | return {
|
54 | repo,
|
55 | nextVersion,
|
56 | rootPath,
|
57 | labels,
|
58 | ignoreCommitters,
|
59 | cacheDir,
|
60 | };
|
61 | }
|
62 | exports.fromPath = fromPath;
|
63 | function fromLernaConfig(rootPath) {
|
64 | const lernaPath = path.join(rootPath, "lerna.json");
|
65 | if (fs.existsSync(lernaPath)) {
|
66 | return JSON.parse(fs.readFileSync(lernaPath)).changelog;
|
67 | }
|
68 | }
|
69 | function fromPackageConfig(rootPath) {
|
70 | const pkgPath = path.join(rootPath, "package.json");
|
71 | if (fs.existsSync(pkgPath)) {
|
72 | return JSON.parse(fs.readFileSync(pkgPath)).changelog;
|
73 | }
|
74 | }
|
75 | function findRepo(rootPath) {
|
76 | const pkgPath = path.join(rootPath, "package.json");
|
77 | if (!fs.existsSync(pkgPath)) {
|
78 | return;
|
79 | }
|
80 | const pkg = JSON.parse(fs.readFileSync(pkgPath));
|
81 | if (!pkg.repository) {
|
82 | return;
|
83 | }
|
84 | return findRepoFromPkg(pkg);
|
85 | }
|
86 | function findNextVersion(rootPath) {
|
87 | const pkgPath = path.join(rootPath, "package.json");
|
88 | const lernaPath = path.join(rootPath, "lerna.json");
|
89 | const pkg = fs.existsSync(pkgPath) ? JSON.parse(fs.readFileSync(pkgPath)) : {};
|
90 | const lerna = fs.existsSync(lernaPath) ? JSON.parse(fs.readFileSync(lernaPath)) : {};
|
91 | return pkg.version ? `v${pkg.version}` : lerna.version ? `v${lerna.version}` : undefined;
|
92 | }
|
93 | function findRepoFromPkg(pkg) {
|
94 | const url = pkg.repository.url || pkg.repository;
|
95 | const info = hostedGitInfo.fromUrl(url);
|
96 | if (info && info.type === "github") {
|
97 | return `${info.user}/${info.project}`;
|
98 | }
|
99 | }
|
100 | exports.findRepoFromPkg = findRepoFromPkg;
|