fume-fhir-converter
Version:
FHIR-Utilized Mapping Engine - Community
93 lines • 4.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadFhirPackageIndex = exports.getFhirPackageIndex = void 0;
const tslib_1 = require("tslib");
/**
* © Copyright Outburn Ltd. 2022-2024 All Rights Reserved
* Project name: FUME-COMMUNITY
*/
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const path_1 = tslib_1.__importDefault(require("path"));
const config_1 = tslib_1.__importDefault(require("../../config"));
const jsonataExpression_1 = tslib_1.__importDefault(require("../jsonataExpression"));
const logger_1 = require("../logger");
const objectFunctions_1 = require("../objectFunctions");
const stringFunctions_1 = require("../stringFunctions");
const getCachePath_1 = require("./getCachePath");
let fhirPackageIndex = {};
const buildFhirCacheIndex = async () => {
(0, logger_1.getLogger)().info('Building global package index (this might take some time...)');
const loadedPackageLabels = Object.keys(config_1.default.getFhirPackages());
(0, logger_1.getLogger)().info(`FHIR Packages to index: ${loadedPackageLabels.join(', ')}`);
const packageIndexArray = (await Promise.all(loadedPackageLabels.map(async (key) => {
const manifest = { ...config_1.default.getFhirPackages()[key] };
const packPath = manifest.installedPath;
const packageIndex = manifest['.index.json'];
delete manifest.installedPath;
delete manifest['.index.json'];
if (packPath && Object.keys(manifest).length > 0) {
return {
package: path_1.default.basename(packPath),
path: path_1.default.join(packPath, 'package'),
packageManifest: manifest,
packageIndex
};
}
else {
(0, logger_1.getLogger)().warn(`Package '${key}' is invalid (no package.json file found). Skipping it...`);
return undefined;
}
;
}))).filter(Boolean);
const bindings = {
omitKeys: objectFunctions_1.omitKeys,
pathJoin: path_1.default.join,
isNumeric: stringFunctions_1.isNumeric
};
const packageIndexObject = await jsonataExpression_1.default.createRawPackageIndexObject.evaluate(packageIndexArray, bindings);
const fixedIndex = await jsonataExpression_1.default.fixPackageIndexObject.evaluate(packageIndexObject, { isNumeric: stringFunctions_1.isNumeric });
return fixedIndex;
};
const parseFhirPackageIndex = async () => {
const fumeIndexPath = (0, getCachePath_1.getFumeIndexFilePath)();
if (fs_extra_1.default.existsSync(fumeIndexPath)) {
(0, logger_1.getLogger)().info(`Found global package index file at ${fumeIndexPath}`);
const dirList = Object.keys(config_1.default.getFhirPackages()).map(k => {
const packPath = config_1.default.getFhirPackages()[k].installedPath;
return packPath ? path_1.default.basename(packPath) : undefined;
}).filter(p => typeof p === 'string');
const currentIndex = JSON.parse(await fs_extra_1.default.readFile(fumeIndexPath, { encoding: 'utf8' }));
const currentPackages = await jsonataExpression_1.default.extractCurrentPackagesFromIndex.evaluate(currentIndex);
const diff = await jsonataExpression_1.default.checkPackagesMissingFromIndex.evaluate({ dirList, packages: currentPackages });
if (diff.length === 0) {
(0, logger_1.getLogger)().info('Global package index file is up-to-date');
return currentIndex;
}
else {
(0, logger_1.getLogger)().info('Global package index file is outdated');
}
;
}
else {
(0, logger_1.getLogger)().info('Global package index file is missing');
}
const fumeIndexFile = await buildFhirCacheIndex();
await fs_extra_1.default.writeFile(fumeIndexPath, JSON.stringify(fumeIndexFile, null, 2));
return fumeIndexFile;
};
const getFhirPackageIndex = () => fhirPackageIndex;
exports.getFhirPackageIndex = getFhirPackageIndex;
const loadFhirPackageIndex = async () => {
try {
(0, logger_1.getLogger)().info('Trying to load global package index...');
const index = await parseFhirPackageIndex();
fhirPackageIndex = index;
return true;
}
catch (e) {
(0, logger_1.getLogger)().error(e);
throw (e);
}
};
exports.loadFhirPackageIndex = loadFhirPackageIndex;
//# sourceMappingURL=loadFhirPackageIndex.js.map