UNPKG

declapract

Version:

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

62 lines 3.7 kB
"use strict"; 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 = async ({ purpose, declaredProjectDirectory, }) => { // grab name from the directory const name = (() => { if (purpose === domain_1.FileCheckPurpose.BEST_PRACTICE) return ((new RegExp(/\/([\w-]+)\/best-practice$/).exec(declaredProjectDirectory) ?? [])[1] ?? null); if (purpose === domain_1.FileCheckPurpose.BAD_PRACTICE) return ((new RegExp(/bad-practices\/([\w-]+)$/).exec(declaredProjectDirectory) ?? [])[1] ?? 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 = await (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 = await 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') ? await (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