wgc
Version:
The official CLI tool to manage the GraphQL Federation Platform Cosmo
67 lines • 3.08 kB
JavaScript
import { writeFile } from 'node:fs/promises';
import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb';
import { Command, program } from 'commander';
import { endOfDay, formatISO, startOfDay, subDays } from 'date-fns';
import pc from 'picocolors';
import { resolve } from 'pathe';
import { getBaseHeaders } from '../../../core/config.js';
export default (opts) => {
const graphType = opts.isMonograph ? 'monograph' : 'federated graph';
const command = new Command('changelog');
command.description(`Fetches the changelog for a ${graphType}`);
command.argument('<name>', `The name of the ${graphType} to update.`);
command.option('-n, --namespace [string]', `The namespace of the ${graphType}.`);
command.option('-l, --limit [number]', 'Limit of entries. Defaults to 10', '10');
command.option('-f, --offset [number]', 'Offset of entries. Defaults to 0', '0');
command.option('-s, --start [date]', 'Start date. Defaults to 3 days back');
command.option('-e, --end [date]', 'End date. Defaults to today');
command.option('-o, --out [string]', 'Destination file for changelog. Defaults to changelog.json', 'changelog.json');
command.action(async (name, options) => {
var _a, _b, _c;
let startDate = subDays(new Date(), 3);
let endDate = new Date();
if (options.start) {
startDate = new Date(options.start);
}
if (options.end) {
endDate = new Date(options.end);
}
const resp = await opts.client.platform.getFederatedGraphChangelog({
name,
pagination: {
limit: Number(options.limit),
offset: Number(options.offset),
},
dateRange: {
start: formatISO(startOfDay(startDate)),
end: formatISO(endOfDay(endDate)),
},
namespace: options.namespace,
}, {
headers: getBaseHeaders(),
});
if (((_a = resp.response) === null || _a === void 0 ? void 0 : _a.code) === EnumStatusCode.OK) {
const output = resp.federatedGraphChangelogOutput.map((op) => ({
createdAt: op.createdAt,
schemaVersionId: op.schemaVersionId,
changelogs: op.changelogs.map((cl) => ({
id: cl.id,
path: cl.path,
changeType: cl.changeType,
changeMessage: cl.changeMessage,
createdAt: cl.createdAt,
})),
}));
await writeFile(resolve(options.out), JSON.stringify(output));
}
else {
let message = `Failed to fetch changelog for ${pc.bold(name)}.`;
if ((_b = resp.response) === null || _b === void 0 ? void 0 : _b.details) {
message += pc.red(pc.bold((_c = resp.response) === null || _c === void 0 ? void 0 : _c.details));
}
program.error(message);
}
});
return command;
};
//# sourceMappingURL=changelog.js.map