@adinsure-ops/ops-cli
Version:
Operations CLI for working with AdInsure
67 lines (66 loc) • 2.74 kB
JavaScript
import { __awaiter } from "tslib";
import { ux } from '@oclif/core';
import matter from 'gray-matter';
import path from 'path';
import CommandBase from '../../command.base.js';
class Validate extends CommandBase {
constructor() {
super(...arguments);
// Added capitalized types for compatibility with current scripts until we migrate away
this._typeRegex = /^(feature|fixed|improve|breaking|Fixed|BreakingChange|Improvement|Feature)$/;
this._issueRegex = /\w+-\d+/;
}
getProblems(path) {
return __awaiter(this, void 0, void 0, function* () {
const problems = [];
const contents = matter.read(path);
if (contents.data.issue === undefined) {
problems.push('Missing \'issue\' in front-matter');
}
else if (!this._issueRegex.test(contents.data.issue)) {
problems.push(`'issue' is not a valid Jira issue number (got: ${contents.data.issue})`);
}
if (contents.data.type === undefined) {
problems.push('Missing \'type\' in front-matter');
}
else if (!this._typeRegex.test(contents.data.type)) {
problems.push(`Invalid 'type' used (got: ${contents.data.type})`);
}
return problems;
});
}
run() {
const _super = Object.create(null, {
run: { get: () => super.run }
});
return __awaiter(this, void 0, void 0, function* () {
_super.run.call(this);
const directory = yield this.getChangelogsRoot();
const files = yield this.getFilesRecursive(directory, directory, /\.md$/i);
ux.action.start('validating changelogs', undefined, { stdout: true });
let problemsFound = 0;
for (const fileName of files) {
const filePath = path.resolve(directory, fileName);
const problems = yield this.getProblems(filePath);
if (problems.length > 0) {
this.log(filePath);
for (const val of problems)
this.warn(val);
problemsFound += problems.length;
}
}
if (problemsFound > 0) {
ux.action.stop('failed');
this.error(`Found ${problemsFound} problem(s), please correct and re-run`, { exit: 1 });
}
ux.action.stop('success');
});
}
}
Validate.aliases = ['log:validate'];
Validate.description = 'validates formatting of all unreleased changelogs';
Validate.usage = 'log:validate [options]';
Validate.examples = [
'$ ops log:validate',
];
export default Validate;