declapract
Version:
A tool to declaratively define best practices, maintainable evolve them, and scalably enforce them.
80 lines • 5.04 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readUseCaseDeclarations = void 0;
const lodash_uniqby_1 = __importDefault(require("lodash.uniqby"));
const domain_1 = require("../../../domain");
const UseCasesDeclarationInput_1 = require("../../../domain/objects/UseCasesDeclarationInput");
const readYmlFile_1 = require("../../../utils/fileio/readYmlFile");
const UserInputError_1 = require("../../UserInputError");
const readUseCaseDeclarations = (_a) => __awaiter(void 0, [_a], void 0, function* ({ declaredUseCasesPath, practices, examples, }) {
// try to read the file
const useCasesYml = yield (0, readYmlFile_1.readYmlFile)({ filePath: declaredUseCasesPath });
// read and validate the useCases yml file
const useCasesInput = yield (() => __awaiter(void 0, void 0, void 0, function* () {
try {
return new UseCasesDeclarationInput_1.UseCasesDeclarationInput(useCasesYml);
}
catch (error) {
throw new UserInputError_1.UserInputError('use-cases declaration file contents failed validation', {
potentialSolution: error.message,
});
}
}))();
// now build up the use cases
const useCases = Object.entries(useCasesInput['use-cases']).map(([name, definition]) => new domain_1.UseCaseDeclaration({
name,
practices: definition.practices.map((requestedPracticeName) => {
const foundPractice = practices.find((practice) => practice.name === requestedPracticeName);
if (!foundPractice)
throw new UserInputError_1.UserInputError(`A use-case specified a practices that was not defined. 'use-case:${name}' specified 'practice:${requestedPracticeName}', but no practice with name '${requestedPracticeName}' has been defined`, {
potentialSolution: 'Have you checked that there is not a typo in the practice name?',
});
return foundPractice;
}),
example: definition.example
? (() => {
const example = examples.find((thisExample) => thisExample.name === definition.example);
if (!example)
throw new UserInputError_1.UserInputError(`example declared for use case but no example with this name was declared: '${definition.example}'`);
return example;
})()
: null,
}));
// now hydrate the use cases that requested "extends" of another use case
const hydratedUseCases = useCases.map((useCase) => {
// determine whether this use case asked for an extension
const { extends: extendsUseCaseNames } = useCasesInput['use-cases'][useCase.name];
if (!extendsUseCaseNames)
return useCase; // if it doesn't extend any use cases, then we can stop here
// for each use case it extends and build up the full, hydrated set of practices
const hydratedPractices = [...useCase.practices];
extendsUseCaseNames.forEach((extendsUseCaseName) => {
// lookup the use case that it extends
const extendedUseCase = useCases.find((candidateUseCase) => candidateUseCase.name === extendsUseCaseName);
if (!extendedUseCase)
throw new UserInputError_1.UserInputError(`A use-case was defined to extend a non-existent use-case. 'use-case:${useCase.name}' was defined to extend 'use-case:${extendsUseCaseName}', but no use case with name '${extendsUseCaseName}' is defined`, { potentialSolution: 'Was there a typo?' });
// push the practices on that extended use case
hydratedPractices.push(...extendedUseCase.practices);
});
// now dedupe them by name
const uniqueHydratedPractices = (0, lodash_uniqby_1.default)(hydratedPractices, (practice) => practice.name);
// and return the hydrated use case
return new domain_1.UseCaseDeclaration(Object.assign(Object.assign({}, useCase), { practices: uniqueHydratedPractices }));
});
// and return the use cases
return hydratedUseCases;
});
exports.readUseCaseDeclarations = readUseCaseDeclarations;
//# sourceMappingURL=readUseCaseDeclarations.js.map