Skip to content

Commit

Permalink
fix: remove all dynamic require() (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
mechanik-daniel authored Apr 17, 2024
1 parent e7a5fa6 commit 6c9d3fd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
21 changes: 11 additions & 10 deletions src/helpers/conformance/conformance.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/**
* © Copyright Outburn Ltd. 2022-2024 All Rights Reserved
* Project name: FUME-COMMUNITY
*/
/**
* © Copyright Outburn Ltd. 2022-2024 All Rights Reserved
* Project name: FUME-COMMUNITY
*/

import config from '../../config';
import { getFhirClient } from '../fhirServer';
import expressions from '../jsonataExpression';
import { getLogger } from '../logger';
import fs from 'fs-extra';

import config from '../../config';
import { getFhirClient } from '../fhirServer';
import expressions from '../jsonataExpression';
import { getLogger } from '../logger';
import { getFhirPackageIndex } from './loadFhirPackageIndex';

export const getStructureDefinition = async (definitionId: string): Promise<any> => {
Expand All @@ -26,8 +28,7 @@ export const getStructureDefinition = async (definitionId: string): Promise<any>
throw (error);
} else {
const path: string = indexed;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fullDef = require(path); // load file
const fullDef = JSON.parse(fs.readFileSync(path).toString()); // load file
return fullDef;
};
};
Expand Down
15 changes: 6 additions & 9 deletions src/helpers/conformance/loadFhirPackageIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ const createPackageIndexFile = (packagePath: string) => {
const fileList = fs.readdirSync(path.join(packagePath, 'package'));
const files = fileList.flatMap((file: string) => {
if (file.endsWith('.json') && file !== 'package.json') {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const content = require(path.join(packagePath, 'package', file));
const content = JSON.parse(fs.readFileSync(path.join(packagePath, 'package', file)).toString());
const indexEntry = {
filename: file,
resourceType: content.resourceType,
Expand Down Expand Up @@ -58,8 +57,8 @@ const buildFhirCacheIndex = async () => {
return {
package: pack,
path: path.join(cachePath, pack, 'package'),
packageManifest: require(path.join(cachePath, pack, 'package', 'package.json')),
packageIndex: require(path.join(cachePath, pack, 'package', '.index.json'))
packageManifest: JSON.parse(fs.readFileSync(path.join(cachePath, pack, 'package', 'package.json')).toString()),
packageIndex: JSON.parse(fs.readFileSync(path.join(cachePath, pack, 'package', '.index.json')).toString())
};
} else {
getLogger().warn(`Folder '${pack}' found in the local FHIR cache does not seem to be a FHIR package (no package.json file found). Skipping it...`);
Expand All @@ -72,8 +71,7 @@ const buildFhirCacheIndex = async () => {
pathJoin: path.join,
require: (filePath: string) => {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const payload = require(filePath);
const payload = JSON.parse(fs.readFileSync(filePath).toString());
return payload;
} catch (e) {
getLogger().error(e);
Expand All @@ -93,13 +91,12 @@ const parseFhirPackageIndex = async (): Promise<IFhirPackageIndex> => {
if (fs.existsSync(fumeIndexPath)) {
getLogger().info(`Found global package index file at ${fumeIndexPath}`);
const dirList: string[] = getCachedPackageDirs();
// eslint-disable-next-line @typescript-eslint/no-var-requires
const currentIndex = require(fumeIndexPath);
const currentIndex = JSON.parse(fs.readFileSync(fumeIndexPath).toString());
const currentPackages = await expressions.extractCurrentPackagesFromIndex.evaluate(currentIndex);
const diff: string[] = await expressions.checkPackagesMissingFromIndex.evaluate({ dirList, packages: currentPackages });
if (diff.length === 0) {
getLogger().info('Global package index file is up-to-date');
return require(fumeIndexPath);
return JSON.parse(fs.readFileSync(fumeIndexPath).toString());
} else {
getLogger().info('Global package index file is outdated');
};
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/jsonataFunctions/getStructureDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* © Copyright Outburn Ltd. 2022-2024 All Rights Reserved
* Project name: FUME-COMMUNITY
*/
import fs from 'fs-extra';

import config from '../../config';
import { getFhirPackageIndex } from '../conformance';
import fhirFuncs from '../fhirFunctions';
Expand Down Expand Up @@ -34,9 +36,7 @@ export const getStructureDefinition = (definitionId: string): any => {
try {
const path: string = getStructureDefinitionPath(definitionId);

// eslint-disable-next-line @typescript-eslint/no-var-requires
const fullDef = require(path); // load file
// getLogger().info(`Definition loaded: ${path}`);
const fullDef = JSON.parse(fs.readFileSync(path).toString()); // load file
return fullDef;
} catch (e) {
return thrower.throwParseError(`A Problem occured while getting the structure definition of '${definitionId}'. The error is: ${e}`);
Expand Down

0 comments on commit 6c9d3fd

Please sign in to comment.