-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfiguration.ts
190 lines (159 loc) · 4.97 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const fs = require("fs");
const path = require("path");
const hostedGitInfo = require("hosted-git-info");
const { getPackagesSync } = require("@manypkg/get-packages");
import ConfigurationError from "./configuration-error";
import { getRootPath } from "./git";
export interface Configuration {
repo: string;
rootPath: string;
labels: { [key: string]: string };
ignoreCommitters: string[];
cacheDir?: string;
nextVersion: string | undefined;
nextVersionFromMetadata?: boolean;
wildcardLabel?: string;
packages: { name: string; path: string }[];
}
export interface ConfigLoaderOptions {
repo?: string;
nextVersionFromMetadata?: boolean;
}
export function load(options: ConfigLoaderOptions = {}): Configuration {
let rootPath = getRootPath();
return fromPath(rootPath, options);
}
interface PackageJson {
type: boolean;
name: string;
}
interface Package {
dir: string;
relativeDir: string;
packageJson: PackageJson;
}
interface PackagesResult {
tool: {
type: "pnpm" | "yarn" | "npm";
};
packages: Package[];
rootPackage: Package;
}
function getPackages(rootPath: string): { name: string; path: string }[] {
try {
let { packages } = getPackagesSync(rootPath) as PackagesResult;
let publishablePackages = packages.filter(pkg => !(pkg.packageJson as any)["private"]);
return publishablePackages.map(pkg => ({
name: pkg.packageJson.name,
path: pkg.dir,
}));
} catch (e) {
// Pre-existing lerna-changelog behavior returns []
// when something goes wrong with package discovery.
// The error is logged here, just in case it's helpful for folks
// to debug their projects.
//
// Mainly:
// - packages must have a name when not using private=true
// - at least one package.json must exist
//
// In practice, folks shouldn't see this error at all
console.error(e);
return [];
}
}
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) || {};
if (options.repo) {
config.repo = options.repo;
}
// Step 2: fill partial config with defaults
let { repo, nextVersion, labels, cacheDir, ignoreCommitters, wildcardLabel } = config;
const packages = getPackages(rootPath);
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 (!wildcardLabel) {
wildcardLabel = `_github-changelog_unlabeled_`;
}
if (wildcardLabel && !labels[wildcardLabel]) {
labels[wildcardLabel] = ":present: Additional updates";
}
if (!ignoreCommitters) {
ignoreCommitters = [
"dependabot-bot",
"dependabot[bot]",
"dependabot-preview[bot]",
"greenkeeperio-bot",
"greenkeeper[bot]",
"renovate-bot",
"renovate[bot]",
];
}
return {
repo,
nextVersion,
rootPath,
labels,
ignoreCommitters,
cacheDir,
wildcardLabel,
packages,
};
}
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}`;
}
}