declapract
Version:
A tool to declaratively define best practices, maintainable evolve them, and scalably enforce them.
53 lines • 3.32 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
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_1 = require("../../../../domain");
const UnexpectedCodePathError_1 = require("../../../UnexpectedCodePathError");
const getColoredPlanTitle_1 = require("../display/color/getColoredPlanTitle");
const sortFileCheckEvaluationsByPracticeRef_1 = require("../sortFileCheckEvaluationsByPracticeRef");
const sortFilePracticeEvaluationsByPracticeName_1 = require("../sortFilePracticeEvaluationsByPracticeName");
const fixFile_1 = require("./fixFile");
/**
* for each "fixable" and "failed" check in the plan, apply the fix
*/
const applyPlan = (_a) => __awaiter(void 0, [_a], void 0, function* ({ plan, projectRootDirectory, }) {
// sanity check that the plan has fixable actions
if (plan.action !== domain_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_1.hasFailed)
.filter(domain_1.isFixableCheck)
.sort(sortFileCheckEvaluationsByPracticeRef_1.sortFileCheckEvaluationsByPracticeRef);
// apply each of them, one at a time sequentially
for (const checkEvaluation of failedFixableChecks) {
yield (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