Skip to content

Commit 5b22a2d

Browse files
authored
Merge pull request #142 from mcode/config-as-object
Moving config file parsing from app to CLI
2 parents 2e4e350 + 5739d70 commit 5b22a2d

File tree

7 files changed

+36
-32
lines changed

7 files changed

+36
-32
lines changed

src/application/app.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
1-
const fs = require('fs');
2-
const path = require('path');
31
const moment = require('moment');
42
const logger = require('../helpers/logger');
53
const { RunInstanceLogger } = require('./tools/RunInstanceLogger');
64
const { sendEmailNotification, zipErrors } = require('./tools/emailNotifications');
75
const { extractDataForPatients } = require('./tools/mcodeExtraction');
86
const { parsePatientIds } = require('../helpers/appUtils');
9-
const { validateConfig } = require('../helpers/configValidator');
10-
11-
function getConfig(pathToConfig) {
12-
// Checks pathToConfig points to valid JSON file
13-
const fullPath = path.resolve(pathToConfig);
14-
try {
15-
return JSON.parse(fs.readFileSync(fullPath));
16-
} catch (err) {
17-
throw new Error(`The provided filepath to a configuration file ${pathToConfig}, full path ${fullPath} did not point to a valid JSON file.`);
18-
}
19-
}
7+
const { validateConfig } = require('../helpers/configUtils');
208

219
function checkInputAndConfig(config, fromDate, toDate) {
2210
// Check input args and needed config variables based on client being used
@@ -33,9 +21,8 @@ function checkInputAndConfig(config, fromDate, toDate) {
3321
}
3422
}
3523

36-
async function mcodeApp(Client, fromDate, toDate, pathToConfig, pathToRunLogs, debug, allEntries) {
24+
async function mcodeApp(Client, fromDate, toDate, config, pathToRunLogs, debug, allEntries) {
3725
logger.level = debug ? 'debug' : 'info';
38-
const config = getConfig(pathToConfig);
3926
checkInputAndConfig(config, fromDate, toDate);
4027

4128
// Create and initialize client

src/cli/cli.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const program = require('commander');
44
const { MCODEClient } = require('../client/MCODEClient');
55
const logger = require('../helpers/logger');
66
const { mcodeApp } = require('../application');
7+
const { getConfig } = require('../helpers/configUtils');
78

89
const defaultPathToConfig = path.join('config', 'csv.config.json');
910
const defaultPathToRunLogs = path.join('logs', 'run-logs.json');
@@ -27,7 +28,8 @@ const allEntries = !entriesFilter;
2728

2829
async function runApp() {
2930
try {
30-
const extractedData = await mcodeApp(MCODEClient, fromDate, toDate, configFilepath, runLogFilepath, debug, allEntries);
31+
const config = getConfig(configFilepath);
32+
const extractedData = await mcodeApp(MCODEClient, fromDate, toDate, config, runLogFilepath, debug, allEntries);
3133

3234
// Finally, save the data to disk
3335
const outputPath = './output';

src/helpers/configValidator.js renamed to src/helpers/configUtils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1+
const fs = require('fs');
2+
const path = require('path');
13
const Ajv = require('ajv');
24
const metaSchema = require('ajv/lib/refs/json-schema-draft-06.json');
35
const logger = require('./logger');
46
const configSchema = require('./schemas/config.schema.json');
57

8+
function getConfig(pathToConfig) {
9+
// Checks pathToConfig points to valid JSON file
10+
const fullPath = path.resolve(pathToConfig);
11+
try {
12+
return JSON.parse(fs.readFileSync(fullPath));
13+
} catch (err) {
14+
throw new Error(`The provided filepath to a configuration file ${pathToConfig}, full path ${fullPath} did not point to a valid JSON file.`);
15+
}
16+
}
17+
618
const ajv = new Ajv({ logger: false, allErrors: true });
719
ajv.addMetaSchema(metaSchema);
820

@@ -34,5 +46,6 @@ function validateConfig(config) {
3446
}
3547

3648
module.exports = {
49+
getConfig,
3750
validateConfig,
3851
};

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const { formatDate, formatDateTime } = require('./helpers/dateUtils');
6767
const { lowercaseLookupQuery, createLowercaseLookup, createInvertedLookup } = require('./helpers/lookupUtils');
6868
const { getConditionEntriesFromContext, getConditionsFromContext, getEncountersFromContext, getPatientFromContext } = require('./helpers/contextUtils');
6969
const { parsePatientIds } = require('./helpers/appUtils');
70+
const { getConfig, validateConfig } = require('./helpers/configUtils');
7071

7172
module.exports = {
7273
// CLI Related utilities
@@ -76,6 +77,8 @@ module.exports = {
7677
parsePatientIds,
7778
sendEmailNotification,
7879
zipErrors,
80+
getConfig,
81+
validateConfig,
7982
// Extractors and Clients
8083
BaseClient,
8184
BaseFHIRExtractor,

test/application/app.test.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
const rewire = require('rewire');
2-
const testConfig = require('./fixtures/test-config.json');
32

43
const app = rewire('../../src/application/app.js');
5-
const getConfig = app.__get__('getConfig');
64
const checkInputAndConfig = app.__get__('checkInputAndConfig');
75

86
describe('App Tests', () => {
9-
describe('getConfig', () => {
10-
const pathToConfig = 'test/application/fixtures/test-config.json';
11-
12-
it('should throw error when pathToConfig does not point to valid JSON file.', () => {
13-
expect(() => getConfig()).toThrowError();
14-
});
15-
16-
it('should return test config', () => {
17-
const config = getConfig(pathToConfig);
18-
expect(config).toEqual(testConfig);
19-
});
20-
});
21-
227
describe('checkInputAndConfig', () => {
238
const config = { patientIdCsvPath: '', extractors: [] };
249
it('should throw error when fromDate is invalid.', () => {

test/helpers/configValidator.test.js renamed to test/helpers/configUtils.test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
const { validateConfig } = require('../../src/helpers/configValidator.js');
1+
const { validateConfig, getConfig } = require('../../src/helpers/configUtils.js');
2+
const testConfig = require('./fixtures/test-config.json');
3+
4+
describe('getConfig', () => {
5+
const pathToConfig = 'test/helpers/fixtures/test-config.json';
6+
7+
it('should throw error when pathToConfig does not point to valid JSON file.', () => {
8+
expect(() => getConfig()).toThrowError();
9+
});
10+
11+
it('should return test config', () => {
12+
const config = getConfig(pathToConfig);
13+
expect(config).toEqual(testConfig);
14+
});
15+
});
216

317
describe('validateConfig', () => {
418
const missingPropertyConfig = { patientIdCsvPath: '' };

0 commit comments

Comments
 (0)