@adinsure-ops/ops-cli
Version:
Operations CLI for working with AdInsure
131 lines (130 loc) • 5.54 kB
JavaScript
import { __awaiter } from "tslib";
import { Args, Flags } from '@oclif/core';
import fs from 'fs-extra';
import matter from 'gray-matter';
import inquirer from 'inquirer';
import path from 'path';
import CommandBase from '../../command.base.js';
class Changelog extends CommandBase {
run() {
const _super = Object.create(null, {
run: { get: () => super.run }
});
return __awaiter(this, void 0, void 0, function* () {
_super.run.call(this);
const { args, flags } = yield this.parse(Changelog);
// Validation of input parameters
const changelogFolderBase = yield this.getChangelogsRoot();
const possibleComponents = [];
if (flags.force && !(yield fs.pathExists(changelogFolderBase))) {
yield fs.mkdirp(changelogFolderBase);
}
for (const file of fs.readdirSync(changelogFolderBase)) {
if (fs.statSync(path.resolve(changelogFolderBase, file)).isDirectory()) {
possibleComponents.push(file);
}
}
// Validate JIRA ticket
const issueRegex = /\w+-\d+/;
let changelogIssue = args.issue;
if (!issueRegex.test(changelogIssue)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const responses = yield inquirer.prompt([{
name: 'issue',
message: 'select a JIRA issue',
type: 'input',
validate(value) {
if (issueRegex.test(value)) {
return true;
}
return 'Please enter a valid JIRA ticket';
},
}]);
changelogIssue = responses.issue;
}
// Validate changelog type
let changelogType = flags.type;
if (!changelogType) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const responses = yield inquirer.prompt([{
name: 'type',
message: 'select a changelog type',
type: 'list',
choices: [{ name: 'feature' }, { name: 'fixed' }, { name: 'improve' }, { name: 'breaking' }],
}]);
changelogType = responses.type;
}
// Validate changelog component
let changelogComponent = flags.component;
let changelogFolder = path.resolve(changelogFolderBase, changelogComponent);
if ((possibleComponents.length > 0 && !changelogComponent) || !fs.pathExistsSync(path.resolve(changelogFolderBase, changelogComponent))) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const responses = yield inquirer.prompt([{
name: 'component',
message: 'Multiple components found. Select a changelog component',
type: 'list',
choices: possibleComponents,
}]);
changelogComponent = responses.component;
changelogFolder = path.resolve(changelogFolderBase, changelogComponent);
}
const fileName = `${changelogIssue.toUpperCase()}.md`;
const changelogPath = path.resolve(changelogFolder, fileName);
if (!(yield fs.pathExists(changelogFolder))) {
yield fs.mkdirp(changelogFolder);
}
if (yield fs.pathExists(changelogPath)) {
this.error(`Changelog for '${changelogIssue}' in '${changelogComponent}' already exists`);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const entry = {
meta: {
issue: changelogIssue,
type: changelogType,
},
content: flags.description || 'Explain your changes here',
};
yield fs.writeFile(changelogPath, matter.stringify(entry.content, entry.meta));
this.log(`Created changelog for ${changelogIssue} in '${changelogPath}'`);
});
}
}
Changelog.aliases = ['log', 'log:add'];
Changelog.description = 'creates a new changelog entry';
Changelog.usage = 'log <issue> -t [type] -c [options]';
Changelog.examples = [
`$ ops log LJADIRD-1234 -t feature`,
`$ ops log LJADIRD-5678 -t fixed`,
`$ ops log LJADIRD-5678 -t fixed -d "Fixed my bug"`,
`$ ops log LJADIRD-8765 -t breaking -c Platform`,
`$ ops log LJADIRD-4321 -t improve -c Configuration`,
];
Changelog.args = {
issue: Args.string({
required: true,
description: 'Jira issue number',
}),
};
Changelog.flags = {
type: Flags.string({
char: 't',
description: 'type of changelog',
options: ['feature', 'fixed', 'improve', 'breaking'],
}),
component: Flags.string({
char: 'c',
description: 'Component for which new changelog should be generated',
default: '',
}),
description: Flags.string({
char: 'd',
description: 'Description of a changelog',
default: '',
}),
force: Flags.boolean({
char: 'f',
description: 'Create logs directory if it does not exist',
default: false,
}),
};
export default Changelog;