@5minds/processcube_engine
Version:
The ProcessCube Engine. Stores and executes BPMNs.
52 lines • 2.38 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = seed;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk");
const IocRegistrations_1 = require("../Contracts/IocRegistrations");
let processDefinitionMediator;
async function seed(container, seedDir, identity) {
const logger = new processcube_engine_sdk_1.Logger('seeding');
if (!fs_1.default.existsSync(seedDir)) {
const error = new processcube_engine_sdk_1.InternalServerError(`Could not seed files from "${seedDir}", because the path does not exists.`, 'setup');
throw error;
}
logger.info(`Seeding bpmns from path "${seedDir}"`);
processDefinitionMediator = container.get(IocRegistrations_1.IocRegistrationKeys.internal.ProcessDefinitionMediator);
const filePaths = getFilePathsFromFolder(seedDir);
logger.info(`Found "${filePaths.length}" BPMNs in seed directory.`);
for (const filePath of filePaths) {
const pathInfo = path_1.default.parse(filePath);
logger.info(`Deploying file "${pathInfo.name}"`);
await registerProcess(filePath, identity);
}
}
function getFilePathsFromFolder(folderPath) {
const filePaths = [];
const folderContentList = fs_1.default.readdirSync(folderPath);
for (const folderContent of folderContentList) {
const pathToContent = path_1.default.join(folderPath, folderContent);
if (fs_1.default.statSync(pathToContent).isDirectory()) {
filePaths.push(...getFilePathsFromFolder(pathToContent));
continue;
}
if (pathToContent.endsWith('.bpmn')) {
filePaths.push(pathToContent);
continue;
}
}
return filePaths;
}
async function registerProcess(processFileName, identity) {
const xml = readProcessModelFromFile(processFileName);
await processDefinitionMediator.persistProcessDefinitions(identity, xml, true);
}
function readProcessModelFromFile(processModelPath) {
const processModelAsXml = fs_1.default.readFileSync(processModelPath, 'utf-8');
return processModelAsXml;
}
//# sourceMappingURL=BpmnSeeder.js.map