declapract
Version:
A tool to declaratively define best practices, maintainable evolve them, and scalably enforce them.
52 lines • 3.38 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyPlan = void 0;
const chalk_1 = __importDefault(require("chalk"));
const indent_string_1 = __importDefault(require("indent-string"));
const domain_objects_1 = require("../../../../domain.objects");
const UnexpectedCodePathError_1 = require("../../../../domain.operations/UnexpectedCodePathError");
const getColoredPlanTitle_1 = require("../../../../domain.operations/usage/plan/display/color/getColoredPlanTitle");
const sortFileCheckEvaluationsByPracticeRef_1 = require("../../../../domain.operations/usage/plan/sortFileCheckEvaluationsByPracticeRef");
const sortFilePracticeEvaluationsByPracticeName_1 = require("../../../../domain.operations/usage/plan/sortFilePracticeEvaluationsByPracticeName");
const fixFile_1 = require("./fixFile");
const isWithinPracticeDeclarationDirectory_1 = require("./isWithinPracticeDeclarationDirectory");
/**
* for each "fixable" and "failed" check in the plan, apply the fix
*/
const applyPlan = async ({ plan, projectRootDirectory, }) => {
// guard: never apply fixes to files within practice declaration directories
// why? because these files define the practices themselves - modifying them would corrupt the source of truth
if ((0, isWithinPracticeDeclarationDirectory_1.isWithinPracticeDeclarationDirectory)(plan.path)) {
const warningToken = chalk_1.default.yellow('⚠');
console.log(` ${warningToken} skipped ${plan.path} (within practice declaration directory)`); // tslint:disable-line: no-console
return;
}
// sanity check that the plan has fixable actions
if (plan.action !== domain_objects_1.RequiredAction.FIX_AUTOMATIC)
throw new UnexpectedCodePathError_1.UnexpectedCodePathError('asked to apply a plan which does not have action FIX_AUTOMATIC specified', {
plan,
}); // should have been filtered out by now
// print out the title for this file
const title = (0, getColoredPlanTitle_1.getColoredPlanTitle)({ plan });
console.log(` * ${title}`); // tslint:disable-line: no-console
// for each failed evaluation check, output that we're applying it
for (const practiceEvaluation of plan.evaluations.sort(sortFilePracticeEvaluationsByPracticeName_1.sortFilePracticeEvaluationsByPracticeName)) {
// grab the failed, fixable checks
const failedFixableChecks = practiceEvaluation.checks
.filter(domain_objects_1.hasFailed)
.filter(domain_objects_1.isFixableCheck)
.sort(sortFileCheckEvaluationsByPracticeRef_1.sortFileCheckEvaluationsByPracticeRef);
// apply each of them, one at a time sequentially
for (const checkEvaluation of failedFixableChecks) {
await (0, fixFile_1.fixFile)({ evaluation: checkEvaluation, projectRootDirectory });
const statusToken = chalk_1.default.green('✓');
const fixabilityToken = chalk_1.default.gray('(fix:applied)');
console.log((0, indent_string_1.default)(`${statusToken} practice:${checkEvaluation.practiceRef} ${fixabilityToken}`, 4)); // tslint:disable-line: no-console
}
}
};
exports.applyPlan = applyPlan;
//# sourceMappingURL=applyPlan.js.map