Skip to content

Commit fc9b54a

Browse files
authored
Merge pull request #121 from mcode/cli-process-exit
process.exit moved to cli.js
2 parents 0aedf4b + 1731b2a commit fc9b54a

File tree

2 files changed

+79
-73
lines changed

2 files changed

+79
-73
lines changed

src/cli/app.js

Lines changed: 66 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -71,84 +71,78 @@ function getEffectiveFromDate(fromDate, runLogger) {
7171
}
7272

7373
async function mcodeApp(Client, fromDate, toDate, pathToConfig, pathToRunLogs, debug, allEntries) {
74-
try {
75-
if (debug) logger.level = 'debug';
76-
// Don't require a run-logs file if we are extracting all-entries. Only required when using --entries-filter.
77-
if (!allEntries) checkLogFile(pathToRunLogs);
78-
const config = getConfig(pathToConfig);
79-
checkInputAndConfig(config, fromDate, toDate);
80-
81-
// Create and initialize client
82-
const mcodeClient = new Client(config);
83-
await mcodeClient.init();
84-
85-
// Parse CSV for list of patient mrns
86-
const patientIdsCsvPath = path.resolve(config.patientIdCsvPath);
87-
const patientIds = parse(fs.readFileSync(patientIdsCsvPath, 'utf8'), { columns: true, bom: true }).map((row) => row.mrn);
88-
89-
// Get RunInstanceLogger for recording new runs and inferring dates from previous runs
90-
const runLogger = allEntries ? null : new RunInstanceLogger(pathToRunLogs);
91-
const effectiveFromDate = allEntries ? null : getEffectiveFromDate(fromDate, runLogger);
92-
const effectiveToDate = allEntries ? null : toDate;
93-
94-
// Extract the data
95-
logger.info(`Extracting data for ${patientIds.length} patients`);
96-
const { extractedData, successfulExtraction, totalExtractionErrors } = await extractDataForPatients(patientIds, mcodeClient, effectiveFromDate, effectiveToDate);
97-
98-
// If we have notification information, send an emailNotification
99-
const { notificationInfo } = config;
100-
if (notificationInfo) {
101-
const notificationErrors = zipErrors(totalExtractionErrors);
102-
try {
103-
await sendEmailNotification(notificationInfo, notificationErrors, debug);
104-
} catch (e) {
105-
logger.error(e.message);
106-
}
74+
if (debug) logger.level = 'debug';
75+
// Don't require a run-logs file if we are extracting all-entries. Only required when using --entries-filter.
76+
if (!allEntries) checkLogFile(pathToRunLogs);
77+
const config = getConfig(pathToConfig);
78+
checkInputAndConfig(config, fromDate, toDate);
79+
80+
// Create and initialize client
81+
const mcodeClient = new Client(config);
82+
await mcodeClient.init();
83+
84+
// Parse CSV for list of patient mrns
85+
const patientIdsCsvPath = path.resolve(config.patientIdCsvPath);
86+
const patientIds = parse(fs.readFileSync(patientIdsCsvPath, 'utf8'), { columns: true }).map((row) => row.mrn);
87+
88+
// Get RunInstanceLogger for recording new runs and inferring dates from previous runs
89+
const runLogger = allEntries ? null : new RunInstanceLogger(pathToRunLogs);
90+
const effectiveFromDate = allEntries ? null : getEffectiveFromDate(fromDate, runLogger);
91+
const effectiveToDate = allEntries ? null : toDate;
92+
93+
// Extract the data
94+
logger.info(`Extracting data for ${patientIds.length} patients`);
95+
const { extractedData, successfulExtraction, totalExtractionErrors } = await extractDataForPatients(patientIds, mcodeClient, effectiveFromDate, effectiveToDate);
96+
97+
// If we have notification information, send an emailNotification
98+
const { notificationInfo } = config;
99+
if (notificationInfo) {
100+
const notificationErrors = zipErrors(totalExtractionErrors);
101+
try {
102+
await sendEmailNotification(notificationInfo, notificationErrors, debug);
103+
} catch (e) {
104+
logger.error(e.message);
107105
}
108-
// A run is successful and should be logged when both extraction finishes without fatal errors
109-
// and messages are posted without fatal errors
110-
if (!allEntries && effectiveFromDate) {
111-
const successCondition = successfulExtraction;
112-
if (successCondition) {
113-
runLogger.addRun(effectiveFromDate, effectiveToDate);
114-
}
106+
}
107+
// A run is successful and should be logged when both extraction finishes without fatal errors
108+
// and messages are posted without fatal errors
109+
if (!allEntries && effectiveFromDate) {
110+
const successCondition = successfulExtraction;
111+
if (successCondition) {
112+
runLogger.addRun(effectiveFromDate, effectiveToDate);
115113
}
114+
}
116115

117-
// check if config specifies that MRN needs to be masked
118-
// if it does need to be masked, mask all references to MRN outside of the patient resource
119-
const patientConfig = config.extractors.find((e) => e.type === 'CSVPatientExtractor');
120-
if (patientConfig && ('constructorArgs' in patientConfig && 'mask' in patientConfig.constructorArgs)) {
121-
if (patientConfig.constructorArgs.mask.includes('mrn')) {
122-
extractedData.forEach((bundle, i) => {
123-
// NOTE: This may fail to mask MRN-related properties on non-patient resources
124-
// Need to investigate further.
125-
try {
126-
maskMRN(bundle);
127-
} catch (e) {
128-
logger.error(`Bundle ${i + 1}: ${e.message}`);
129-
}
130-
});
131-
}
116+
// check if config specifies that MRN needs to be masked
117+
// if it does need to be masked, mask all references to MRN outside of the patient resource
118+
const patientConfig = config.extractors.find((e) => e.type === 'CSVPatientExtractor');
119+
if (patientConfig && ('constructorArgs' in patientConfig && 'mask' in patientConfig.constructorArgs)) {
120+
if (patientConfig.constructorArgs.mask.includes('mrn')) {
121+
extractedData.forEach((bundle, i) => {
122+
// NOTE: This may fail to mask MRN-related properties on non-patient resources
123+
// Need to investigate further.
124+
try {
125+
maskMRN(bundle);
126+
} catch (e) {
127+
logger.error(`Bundle ${i + 1}: ${e.message}`);
128+
}
129+
});
132130
}
131+
}
133132

134-
// Finally, save the data to disk
135-
const outputPath = './output';
136-
if (!fs.existsSync(outputPath)) {
137-
logger.info(`Creating directory ${outputPath}`);
138-
fs.mkdirSync(outputPath);
139-
}
140-
// For each bundle in our extractedData, write it to our output directory
141-
extractedData.forEach((bundle, i) => {
142-
const outputFile = path.join(outputPath, `mcode-extraction-patient-${i + 1}.json`);
143-
logger.debug(`Logging mCODE output to ${outputFile}`);
144-
fs.writeFileSync(outputFile, JSON.stringify(bundle), 'utf8');
145-
});
146-
logger.info(`Successfully logged ${extractedData.length} mCODE bundle(s) to ${outputPath}`);
147-
} catch (e) {
148-
logger.error(e.message);
149-
logger.debug(e.stack);
150-
process.exit(1);
133+
// Finally, save the data to disk
134+
const outputPath = './output';
135+
if (!fs.existsSync(outputPath)) {
136+
logger.info(`Creating directory ${outputPath}`);
137+
fs.mkdirSync(outputPath);
151138
}
139+
// For each bundle in our extractedData, write it to our output directory
140+
extractedData.forEach((bundle, i) => {
141+
const outputFile = path.join(outputPath, `mcode-extraction-patient-${i + 1}.json`);
142+
logger.debug(`Logging mCODE output to ${outputFile}`);
143+
fs.writeFileSync(outputFile, JSON.stringify(bundle), 'utf8');
144+
});
145+
logger.info(`Successfully logged ${extractedData.length} mCODE bundle(s) to ${outputPath}`);
152146
}
153147

154148
module.exports = {

src/cli/cli.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path');
22
const program = require('commander');
33
const { MCODEClient } = require('../client/MCODEClient');
4+
const logger = require('../helpers/logger');
45
const { mcodeApp } = require('./app');
56

67
const defaultPathToConfig = path.join('config', 'csv.config.json');
@@ -23,4 +24,15 @@ const {
2324
// Flag to extract allEntries, or just to use to-from dates
2425
const allEntries = !entriesFilter;
2526

26-
mcodeApp(MCODEClient, fromDate, toDate, configFilepath, runLogFilepath, debug, allEntries);
27+
async function runApp() {
28+
try {
29+
await mcodeApp(MCODEClient, fromDate, toDate, configFilepath, runLogFilepath, debug, allEntries);
30+
} catch (e) {
31+
if (debug) logger.level = 'debug';
32+
logger.error(e.message);
33+
logger.debug(e.stack);
34+
process.exit(1);
35+
}
36+
}
37+
38+
runApp();

0 commit comments

Comments
 (0)