UNPKG

declapract

Version:

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

62 lines 3.84 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.readPracticeDeclaration = void 0; const domain_1 = require("../../../../domain"); const listPathsInDirectory_1 = require("../../../../utils/filepaths/listPathsInDirectory"); const UnexpectedCodePathError_1 = require("../../../UnexpectedCodePathError"); const UserInputError_1 = require("../../../UserInputError"); const getProjectCheckDeclaration_1 = require("./getProjectCheckDeclaration"); const readPracticeDeclaration = (_a) => __awaiter(void 0, [_a], void 0, function* ({ declaredPracticeDirectory, }) { // grab the file paths in this dir const paths = yield (0, listPathsInDirectory_1.listPathsInDirectory)({ directory: declaredPracticeDirectory, }); // grab the name of the practice const practiceName = declaredPracticeDirectory.split('/').slice(-1)[0]; if (!practiceName) throw new UnexpectedCodePathError_1.UnexpectedCodePathError('could not extract practice name', { declaredPracticeDirectory, }); // find the "best-practice" dir, if one was defined const bestPracticeProjectCheck = paths.includes('best-practice') ? yield (0, getProjectCheckDeclaration_1.getProjectCheckDeclaration)({ purpose: domain_1.FileCheckPurpose.BEST_PRACTICE, declaredProjectDirectory: `${declaredPracticeDirectory}/best-practice`, }) : null; // find the "bad-practice" dirs, if any were defined const badPracticeProjectChecks = yield (() => __awaiter(void 0, void 0, void 0, function* () { if (!paths.includes('bad-practices')) return []; const badPracticesPaths = yield (0, listPathsInDirectory_1.listPathsInDirectory)({ directory: `${declaredPracticeDirectory}/bad-practices`, }); return Promise.all(badPracticesPaths.map((path) => (0, getProjectCheckDeclaration_1.getProjectCheckDeclaration)({ purpose: domain_1.FileCheckPurpose.BAD_PRACTICE, declaredProjectDirectory: `${declaredPracticeDirectory}/bad-practices/${path}`, }))); }))(); // make sure that the `/bad-practices` dir was not typo'ed as `/bad-practice` on accident (happened on more than one occasion 😅) if (paths.includes('bad-practice')) throw new UserInputError_1.UserInputError(`a 'bad-practice/' directory was defined for practice '${practiceName}'. this is probably a typo and should have been 'bad-practices/'`, { potentialSolution: "rename 'bad-practice/' to 'bad-practices/'" }); // ensure atleast one of best-practice or bad-practice is defined if (!bestPracticeProjectCheck && !badPracticeProjectChecks.length) throw new UserInputError_1.UserInputError(`either a 'best-practice' or atleast one of 'bad-practices' must be defined for '${practiceName}'`); // return it return new domain_1.PracticeDeclaration({ name: practiceName, bestPractice: bestPracticeProjectCheck, badPractices: badPracticeProjectChecks, }); }); exports.readPracticeDeclaration = readPracticeDeclaration; //# sourceMappingURL=readPracticeDeclaration.js.map