declapract
Version:
A tool to declaratively define best practices, maintainable evolve them, and scalably enforce them.
46 lines • 3.06 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fixFile = void 0;
const readFileIfExistsAsync_1 = require("../../../../utils/fileio/readFileIfExistsAsync");
const removeFileAsync_1 = require("../../../../utils/fileio/removeFileAsync");
const writeFileAsync_1 = require("../../../../utils/fileio/writeFileAsync");
const UnexpectedCodePathError_1 = require("../../../UnexpectedCodePathError");
/**
* fix a file using the fix function of a check, based on an evaluation of the file
*
* input is a "checkEvaluation", because:
* - it tells us this file _was_ checked before this was called
* - it has the exact "path" that the check was evaluated on (instead of just a glob, like on the path)
* - it has the reference to the declared check, which has the fix function
*/
const fixFile = (_a) => __awaiter(void 0, [_a], void 0, function* ({ evaluation, projectRootDirectory, }) {
if (!evaluation.fix)
throw new UnexpectedCodePathError_1.UnexpectedCodePathError('fixFile called on an eval that doesnt have a fix defined');
// define the current state
const relativeFilePath = evaluation.path;
const filePath = `${projectRootDirectory}/${evaluation.path}`;
const fileContents = yield (0, readFileIfExistsAsync_1.readFileIfExistsAsync)({ filePath });
// define the desired, "fixed", state
const { contents: desiredContents = fileContents, // if contents not returned from fix function, then desired contents = current contents
relativeFilePath: desiredRelativeFilePath = relativeFilePath, // if relative file path not returned from fix function, then desired relative file path = current relative file path
} = yield evaluation.fix(fileContents, evaluation.context);
const desiredFilePath = `${projectRootDirectory}/${desiredRelativeFilePath}`;
// define what we need to do to get there
if (desiredContents)
// if they still desired contents, then write it to the desired path
yield (0, writeFileAsync_1.writeFileAsync)({ path: desiredFilePath, content: desiredContents });
if (desiredFilePath !== filePath || !desiredContents)
// if the file path changed - or they no longer desire contents, then remove the orig file
yield (0, removeFileAsync_1.removeFileAsync)({ path: filePath });
});
exports.fixFile = fixFile;
//# sourceMappingURL=fixFile.js.map