-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstatus.ts
More file actions
319 lines (289 loc) · 11 KB
/
Copy pathstatus.ts
File metadata and controls
319 lines (289 loc) · 11 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import { log, colorize } from '../utils/logger.ts';
import { loadConfig } from '../core/config.ts';
import { discoverPackages } from '../core/workspace.ts';
import { DependencyGraph } from '../core/dep-graph.ts';
import { readBumpFiles, filterBranchBumpFiles } from '../core/bump-file.ts';
import { assembleReleasePlan } from '../core/release-plan.ts';
import { getCurrentBranch, getChangedFiles } from '../core/git.ts';
import { channelNames, resolveActiveChannel, type ResolvedChannel } from '../core/channels.ts';
import { buildChannelReleasePlan } from '../core/prerelease.ts';
import { publishTargetLabel, resolvePackageRegistry } from '../core/github-release.ts';
import type { BumpFile, BumpyConfig, PackageConfig, PlannedRelease, WorkspacePackage } from '../types.ts';
interface StatusOptions {
json?: boolean;
/** Output only package names, one per line (useful for piping) */
packagesOnly?: boolean;
/** Filter to specific bump types: "major", "minor", "patch" */
bumpType?: string;
/** Filter to specific packages (comma-separated names or globs) */
filter?: string;
/** Show verbose output including bump file details */
verbose?: boolean;
/** Channel name override (otherwise inferred from the current branch) */
channel?: string;
}
export async function statusCommand(rootDir: string, opts: StatusOptions): Promise<void> {
const config = await loadConfig(rootDir);
const packages = await discoverPackages(rootDir, config);
const depGraph = new DependencyGraph(packages);
const channel = resolveActiveChannel(rootDir, config, opts.channel);
if (channel) {
await channelStatus(rootDir, config, channel, packages, depGraph, opts);
return;
}
// Channel-dir bump files count as pending on the base branch (promotion)
const { bumpFiles, errors: parseErrors } = await readBumpFiles(rootDir, { channels: channelNames(config) });
if (parseErrors.length > 0) {
for (const err of parseErrors) {
log.error(err);
}
}
if (bumpFiles.length === 0) {
if (opts.json) {
console.log(JSON.stringify({ bumpFiles: [], releases: [], packageNames: [] }, null, 2));
} else if (!opts.packagesOnly) {
log.info('No pending bump files.');
}
process.exit(1); // exit 1 = no releases pending (useful for CI)
}
const plan = assembleReleasePlan(bumpFiles, packages, depGraph, config);
// Determine which bump files belong to the current branch (if not on base branch)
let branchBumpFileIds: Set<string> | undefined;
const currentBranch = getCurrentBranch({ cwd: rootDir });
if (currentBranch && currentBranch !== config.baseBranch) {
const changedFiles = getChangedFiles(rootDir, config.baseBranch);
const result = filterBranchBumpFiles(bumpFiles, changedFiles, rootDir);
branchBumpFileIds = result.branchBumpFileIds;
}
// Apply filters
let releases = plan.releases;
if (opts.bumpType) {
const types = opts.bumpType.split(',').map((t) => t.trim());
releases = releases.filter((r) => types.includes(r.type));
}
if (opts.filter) {
const { matchGlob } = await import('../core/config.ts');
const patterns = opts.filter.split(',').map((p) => p.trim());
releases = releases.filter((r) => patterns.some((p) => matchGlob(r.name, p)));
}
if (opts.json) {
const jsonOutput = {
bumpFiles: plan.bumpFiles.map((bf) => ({
id: bf.id,
summary: bf.summary,
releases: bf.releases.map((r) => ({ name: r.name, type: r.type })),
...(branchBumpFileIds ? { inCurrentBranch: branchBumpFileIds.has(bf.id) } : {}),
})),
releases: releases.map((r) => {
const pkg = packages.get(r.name);
const pkgConfig = pkg?.bumpy || {};
return {
name: r.name,
type: r.type,
oldVersion: r.oldVersion,
newVersion: r.newVersion,
dir: pkg?.relativeDir,
bumpFiles: r.bumpFiles,
isDependencyBump: r.isDependencyBump,
isCascadeBump: r.isCascadeBump,
...(branchBumpFileIds
? {
inCurrentBranch: r.bumpFiles.some((id) => branchBumpFileIds!.has(id)),
}
: {}),
publishTargets: getPublishTargets(pkg, pkgConfig, config),
};
}),
packageNames: releases.map((r) => r.name),
};
console.log(JSON.stringify(jsonOutput, null, 2));
return;
}
if (opts.packagesOnly) {
for (const r of releases) {
console.log(r.name);
}
return;
}
// Pretty output
log.bold(`${bumpFiles.length} bump file(s) pending\n`);
if (releases.length === 0) {
log.warn('No packages match the current filters.');
return;
}
// Group by bump type
const groups: [string, string, PlannedRelease[]][] = [
['Major', 'red', releases.filter((r) => r.type === 'major')],
['Minor', 'yellow', releases.filter((r) => r.type === 'minor')],
['Patch', 'green', releases.filter((r) => r.type === 'patch')],
];
for (const [label, color, group] of groups) {
if (group.length === 0) continue;
log.bold(colorize(label, color as 'red' | 'yellow' | 'green'));
for (const r of group) {
printRelease(r, packages);
}
console.log();
}
// Show warnings from the release plan
if (plan.warnings.length > 0) {
for (const w of plan.warnings) {
log.warn(w);
}
console.log();
}
if (opts.verbose) {
log.bold('Bump files:');
for (const bf of plan.bumpFiles) {
console.log(` ${colorize(bf.id, 'cyan')}`);
for (const r of bf.releases) {
console.log(` ${r.name}: ${r.type}`);
}
if (bf.summary) {
console.log(` ${colorize(bf.summary.split('\n')[0]!, 'dim')}`);
}
}
}
}
/**
* Status on a prerelease channel: shows the cycle (shipped + pending bump files)
* and the derived prerelease versions. Counters come from the registry — when it's
* unreachable, targets render with a ".?" counter placeholder.
*/
async function channelStatus(
rootDir: string,
config: BumpyConfig,
channel: ResolvedChannel,
packages: Map<string, WorkspacePackage>,
depGraph: DependencyGraph,
opts: StatusOptions,
): Promise<void> {
const { bumpFiles, errors: parseErrors } = await readBumpFiles(rootDir, { channels: channelNames(config) });
for (const err of parseErrors) log.error(err);
const shipped = bumpFiles.filter((bf) => bf.channel === channel.name);
const pending = bumpFiles.filter((bf) => bf.channel !== channel.name);
if (bumpFiles.length === 0) {
if (opts.json) {
console.log(JSON.stringify({ channel: channel.name, bumpFiles: [], releases: [], packageNames: [] }, null, 2));
} else if (!opts.packagesOnly) {
log.info(`No bump files in the "${channel.name}" cycle.`);
}
process.exit(1); // exit 1 = no releases pending (useful for CI)
}
const stablePlan = assembleReleasePlan(bumpFiles, packages, depGraph, config, {
prereleasePreid: channel.preid,
});
let releases = stablePlan.releases;
let countersExact = false;
try {
const built = await buildChannelReleasePlan(stablePlan, channel, packages, rootDir, { forDisplay: true });
if (built.plan.releases.length > 0) {
releases = built.plan.releases;
countersExact = true;
}
} catch {
// registry unreachable — fall through to ".?" display
}
if (!countersExact) {
releases = releases.map((r) => ({ ...r, newVersion: `${r.newVersion}-${channel.preid}.?` }));
}
if (opts.bumpType) {
const types = opts.bumpType.split(',').map((t) => t.trim());
releases = releases.filter((r) => types.includes(r.type));
}
if (opts.filter) {
const { matchGlob } = await import('../core/config.ts');
const patterns = opts.filter.split(',').map((p) => p.trim());
releases = releases.filter((r) => patterns.some((p) => matchGlob(r.name, p)));
}
if (opts.json) {
console.log(
JSON.stringify(
{
channel: channel.name,
preid: channel.preid,
tag: channel.tag,
bumpFiles: bumpFiles.map((bf) => ({
id: bf.id,
summary: bf.summary,
releases: bf.releases.map((r) => ({ name: r.name, type: r.type })),
shipped: bf.channel === channel.name,
})),
releases: releases.map((r) => {
const pkg = packages.get(r.name);
return {
name: r.name,
type: r.type,
oldVersion: r.oldVersion,
newVersion: r.newVersion,
dir: pkg?.relativeDir,
bumpFiles: r.bumpFiles,
isDependencyBump: r.isDependencyBump,
isCascadeBump: r.isCascadeBump,
publishTargets: getPublishTargets(pkg, pkg?.bumpy || {}, config),
};
}),
packageNames: releases.map((r) => r.name),
},
null,
2,
),
);
return;
}
if (opts.packagesOnly) {
for (const r of releases) console.log(r.name);
return;
}
log.bold(`Channel "${channel.name}" — preid "-${channel.preid}.N", dist-tag @${channel.tag}\n`);
printBumpFileGroup(`Shipped on this channel (.bumpy/${channel.name}/)`, shipped);
printBumpFileGroup('Pending (next prerelease)', pending);
log.bold(`Cycle releases${countersExact ? '' : colorize(' (registry unreachable — counters unknown)', 'dim')}`);
for (const r of releases) {
printRelease(r, packages);
}
console.log();
if (stablePlan.warnings.length > 0) {
for (const w of stablePlan.warnings) log.warn(w);
}
}
function printBumpFileGroup(label: string, files: BumpFile[]): void {
log.bold(label);
if (files.length === 0) {
console.log(colorize(' (none)', 'dim'));
}
for (const bf of files) {
const summary = bf.summary ? colorize(` — ${bf.summary.split('\n')[0]}`, 'dim') : '';
console.log(` ${colorize(`${bf.id}.md`, 'cyan')}${summary}`);
}
console.log();
}
function printRelease(r: PlannedRelease, packages: Map<string, WorkspacePackage>) {
const pkg = packages.get(r.name);
const dir = pkg ? colorize(` (${pkg.relativeDir})`, 'dim') : '';
const suffix = r.isDependencyBump
? colorize(' ← dependency bump', 'dim')
: r.isCascadeBump
? colorize(' ← cascade', 'dim')
: '';
console.log(` ${r.name}: ${r.oldVersion} → ${colorize(r.newVersion, 'cyan')}${suffix}${dir}`);
}
/** Determine which publish targets a package will use */
function getPublishTargets(
pkg: WorkspacePackage | undefined,
pkgConfig: Partial<PackageConfig>,
_config: BumpyConfig,
): Array<{ type: string; label: string; registry?: string }> {
if (!pkg) return [];
// Private packages with no custom command won't publish
if (pkg.private && !pkgConfig.publishCommand) return [];
const targets: Array<{ type: string; label: string; registry?: string }> = [];
if (pkgConfig.publishCommand) {
targets.push({ type: 'custom', label: 'custom' });
}
if (!pkgConfig.publishCommand && !pkgConfig.skipNpmPublish) {
const registry = resolvePackageRegistry(pkg, pkgConfig);
targets.push({ type: 'npm', label: publishTargetLabel('npm', registry), ...(registry ? { registry } : {}) });
}
return targets;
}