-
Notifications
You must be signed in to change notification settings - Fork 100
/
configuration.ts
126 lines (104 loc) · 3.45 KB
/
configuration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const fs = require("fs");
const path = require("path");
const execa = require("execa");
const hostedGitInfo = require("hosted-git-info");
import ConfigurationError from "./configuration-error";
export interface Configuration {
repo: string;
rootPath: string;
labels: { [key: string]: string };
ignoreCommitters: string[];
cacheDir?: string;
nextVersion: string | undefined;
nextVersionFromMetadata?: boolean;
}
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: ConfigLoaderOptions = {}): Configuration {
// Step 1: load partial config from `package.json` or `lerna.json`
let config = fromPackageConfig(rootPath) || fromLernaConfig(rootPath) || {};
// Step 2: fill partial config with defaults
let { repo, nextVersion, labels, cacheDir, ignoreCommitters } = config;
if (!repo) {
repo = findRepo(rootPath);
if (!repo) {
throw new ConfigurationError('Could not infer "repo" from the "package.json" file.');
}
}
if (options.nextVersionFromMetadata || config.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",
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,
};
}
function fromLernaConfig(rootPath: string): Partial<Configuration> | undefined {
const lernaPath = path.join(rootPath, "lerna.json");
if (fs.existsSync(lernaPath)) {
return JSON.parse(fs.readFileSync(lernaPath)).changelog;
}
}
function fromPackageConfig(rootPath: string): Partial<Configuration> | undefined {
const pkgPath = path.join(rootPath, "package.json");
if (fs.existsSync(pkgPath)) {
return JSON.parse(fs.readFileSync(pkgPath)).changelog;
}
}
function findRepo(rootPath: string): string | undefined {
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: 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 info = hostedGitInfo.fromUrl(url);
if (info && info.type === "github") {
return `${info.user}/${info.project}`;
}
}