UNPKG

declapract

Version:

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

72 lines 4.6 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.getProjectCheckDeclaration = void 0; const domain_1 = require("../../../../domain"); const ProjectCheckDeclaration_1 = require("../../../../domain/objects/ProjectCheckDeclaration"); const readFileAsync_1 = require("../../../../utils/fileio/readFileAsync"); const listFilesInDirectory_1 = require("../../../../utils/filepaths/listFilesInDirectory"); const UnexpectedCodePathError_1 = require("../../../UnexpectedCodePathError"); const getFileCheckDeclaration_1 = require("./getFileCheckDeclaration/getFileCheckDeclaration"); const getProjectCheckDeclaration = (_a) => __awaiter(void 0, [_a], void 0, function* ({ purpose, declaredProjectDirectory, }) { // grab name from the directory const name = (() => { var _a, _b, _c, _d; if (purpose === domain_1.FileCheckPurpose.BEST_PRACTICE) return ((_b = ((_a = new RegExp(/\/([\w-]+)\/best-practice$/).exec(declaredProjectDirectory)) !== null && _a !== void 0 ? _a : [])[1]) !== null && _b !== void 0 ? _b : null); if (purpose === domain_1.FileCheckPurpose.BAD_PRACTICE) return ((_d = ((_c = new RegExp(/bad-practices\/([\w-]+)$/).exec(declaredProjectDirectory)) !== null && _c !== void 0 ? _c : [])[1]) !== null && _d !== void 0 ? _d : null); throw new UnexpectedCodePathError_1.UnexpectedCodePathError('unsupported file check purpose', { purpose, }); })(); if (!name) throw new UnexpectedCodePathError_1.UnexpectedCodePathError(`neither best-practice name nor bad-practice name was extractable from the declared project directory '${declaredProjectDirectory}'`); // grab paths to _all_ files in this dir (not just at root level) const paths = yield (0, listFilesInDirectory_1.listFilesInDirectory)({ directory: declaredProjectDirectory, }); // grab the meta files (i.e., path matches `${projectRoot}/.declapract.*`) const metaFilePaths = paths.filter((path) => new RegExp(/^\.declapract\./).test(path)); // group all of the other files by main file name (i.e., key = filePath.replace('.declapract.ts$', '')) const projectFilePaths = [ ...new Set(paths .filter((path) => !metaFilePaths.includes(path)) .map((path) => path.replace(/\.declapract\.ts$/, ''))), ].sort(); // for each "main file", get the FileCheckDefinition, now that we have all the files defined for it const checksAndErrors = yield Promise.all(projectFilePaths.map((declaredFileCorePath) => (0, getFileCheckDeclaration_1.getFileCheckDeclaration)({ purpose, declaredProjectDirectory, declaredFileCorePath, }).catch((error) => error))); const isError = (err) => err instanceof Error || // if its an error from the same context (err && err.stack && err.message) || // if its an error from a different context (e.g., jest+babel parsing error); // https://stackoverflow.com/a/30469297/3068233 (err && err.diagnosticCodes && err.diagnosticText); // if its a typescript error from a different context (e.g., jest+babel parsing error); // https://stackoverflow.com/a/30469297/3068233 const anError = checksAndErrors.find(isError); if (anError) throw anError; const checks = checksAndErrors.filter((checkOrError) => !isError(checkOrError)); // get readme contents, if readme defined const readme = metaFilePaths.includes('.declapract.readme.md') ? yield (0, readFileAsync_1.readFileAsync)({ filePath: `${declaredProjectDirectory}/.declapract.readme.md`, }) : null; // return the project def return new ProjectCheckDeclaration_1.ProjectCheckDeclaration({ name, checks, readme, }); }); exports.getProjectCheckDeclaration = getProjectCheckDeclaration; //# sourceMappingURL=getProjectCheckDeclaration.js.map