UNPKG

declapract

Version:

A tool to declaratively define best practices, maintainable evolve them, and scalably enforce them.

117 lines 6.95 kB
"use strict"; 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.getFileCheckDeclaration = void 0; const domain_1 = require("../../../../../domain"); const doesFileExist_1 = require("../../../../../utils/fileio/doesFileExist"); const readFileAsync_1 = require("../../../../../utils/fileio/readFileAsync"); const UnexpectedCodePathError_1 = require("../../../../UnexpectedCodePathError"); const compile_1 = require("../../../../commands/compile"); const containsCheck_1 = require("./checkMethods/containsCheck"); const existsCheck_1 = require("./checkMethods/existsCheck"); const strictEqualsCheck_1 = require("./checkMethods/strictEqualsCheck"); const fixContainsJSONByReplacingAndAddingKeyValues_1 = require("./fixMethods/fixContainsJSONByReplacingAndAddingKeyValues"); const fixContainsWhenFileDoesntExistBySettingDeclaredContents_1 = require("./fixMethods/fixContainsWhenFileDoesntExistBySettingDeclaredContents"); const fixEqualsBySettingDeclaredContents_1 = require("./fixMethods/fixEqualsBySettingDeclaredContents"); const getHydratedCheckInputsForFile_1 = require("./getHydratedCheckInputsForFile"); const getFileCheckDeclaration = (_a) => __awaiter(void 0, [_a], void 0, function* ({ purpose, declaredProjectDirectory, declaredFileCorePath, }) { // get check inputs, if declared const { declaredCheckInputs, declaredFixFunction, declaredContentsFunction } = yield (0, getHydratedCheckInputsForFile_1.getHydratedCheckInputsForFile)({ declaredFileCorePath, declaredProjectDirectory, }); // get declared best practice contents, if declared const contentsFilePath = `${declaredProjectDirectory}/${declaredFileCorePath}`; // its the same path. i.e., the contents for `tsconfig.ts` are declared under `tsconfig.ts`) const contentsFileExists = yield (0, doesFileExist_1.doesFileExist)({ filePath: contentsFilePath, }); const declaredContents = yield (() => __awaiter(void 0, void 0, void 0, function* () { if (declaredContentsFunction) return declaredContentsFunction; if (contentsFileExists) return () => (0, readFileAsync_1.readFileAsync)({ filePath: contentsFilePath }); return null; }))(); // define the common attributes const pathGlob = (0, compile_1.deserializeGlobPathFromNpmPackaging)(declaredFileCorePath); // its the path relative to the project root (note that this path can is technically a glob (e.g., can be `src/**/*.ts`)) const required = !(declaredCheckInputs === null || declaredCheckInputs === void 0 ? void 0 : declaredCheckInputs.optional); // if not explicitly opted-in to be optional, then its required // define the check fns // define the fix fns const strictEqualsFix = purpose === domain_1.FileCheckPurpose.BEST_PRACTICE ? fixEqualsBySettingDeclaredContents_1.fixEqualsBySettingDeclaredContents : null; // TODO: think of a fix for bad practice case const containsFix = (() => { if (!declaredContents) return null; // contains fixes can only be defined when declared contents are defined (side note: we shouldn't be needing a contains fix otherwise, since contains type only occurs if there is a file) if (purpose === domain_1.FileCheckPurpose.BEST_PRACTICE) { if (pathGlob.endsWith('.json')) return fixContainsJSONByReplacingAndAddingKeyValues_1.fixContainsJSONByReplacingAndAddingKeyValues; return fixContainsWhenFileDoesntExistBySettingDeclaredContents_1.fixContainsWhenFileDoesntExistBySettingDeclaredContents; } return null; // otherwise, no fix })(); const existsFix = purpose === domain_1.FileCheckPurpose.BAD_PRACTICE ? () => ({ contents: null }) // to fix it for BAD_PRACTICE, just make the file not exist : null; // define what check type it is, based on the inputs + defaults const type = (() => { // if no input file, then default to equals check if (!declaredCheckInputs) { if (!declaredContents) throw new UnexpectedCodePathError_1.UnexpectedCodePathError('no hydrated input but also no contents. why are we even evaluating this file then?'); return domain_1.FileCheckType.EQUALS; // default to "equals"; } // if a custom check fn was defined, then check type = custom if (declaredCheckInputs.function) return domain_1.FileCheckType.CUSTOM; // if user specified a type, use it if (declaredCheckInputs.type) return declaredCheckInputs.type; // since user did not specify a type, see if we can default to equals if (declaredContents) return domain_1.FileCheckType.EQUALS; // otherwise, default to an existence check return domain_1.FileCheckType.EXISTS; })(); // define the check and fix based on the type const checkForType = (() => { if (type === domain_1.FileCheckType.EQUALS) return strictEqualsCheck_1.strictEqualsCheck; if (type === domain_1.FileCheckType.CUSTOM) return declaredCheckInputs.function; if (type === domain_1.FileCheckType.CONTAINS) return containsCheck_1.containsCheck; if (type === domain_1.FileCheckType.EXISTS) return existsCheck_1.existsCheck; throw new UnexpectedCodePathError_1.UnexpectedCodePathError('should have a check for each type defined'); })(); const fixForType = (() => { if (type === domain_1.FileCheckType.EQUALS) return strictEqualsFix; if (type === domain_1.FileCheckType.CONTAINS) return containsFix; if (type === domain_1.FileCheckType.EXISTS) return existsFix; return null; })(); // return the check based on what we've found for it return new domain_1.FileCheckDeclaration({ pathGlob, required, purpose, type, check: checkForType, fix: declaredFixFunction !== null && declaredFixFunction !== void 0 ? declaredFixFunction : fixForType, contents: declaredContents, }); }); exports.getFileCheckDeclaration = getFileCheckDeclaration; //# sourceMappingURL=getFileCheckDeclaration.js.map