diff --git a/lib/CodeService.js b/lib/CodeService.js index 8ddcea9..b2b11b4 100644 --- a/lib/CodeService.js +++ b/lib/CodeService.js @@ -68,7 +68,7 @@ class CodeService { * @returns {Promise.} A promise that returns nothing when * resolved and returns an error when rejected. */ - ensureValueSets(valueSetList = [], umlsUserName = env['UMLS_USER_NAME'], umlsPassword = env['UMLS_PASSWORD']) { + ensureValueSets(valueSetList = [], umlsUserName = env['UMLS_USER_NAME'], umlsPassword = env['UMLS_PASSWORD'], caching = true) { // First, filter out the value sets we already have const filteredVSList = valueSetList.filter(vs => { const result = this.findValueSet(vs.id, vs.version); @@ -80,7 +80,7 @@ class CodeService { } else if ( typeof umlsUserName === 'undefined' || umlsUserName == null ||typeof umlsPassword === 'undefined' || umlsPassword == null) { return Promise.reject('Failed to download value sets since UMLS_USER_NAME and/or UMLS_PASSWORD is not set.'); } else { - return downloadFromVSAC(umlsUserName, umlsPassword, filteredVSList, this.cache, this.valueSets); + return downloadFromVSAC(umlsUserName, umlsPassword, filteredVSList, this.cache, this.valueSets, caching); } } @@ -93,9 +93,9 @@ class CodeService { * @param {string} umlsPassword - the UMLS password to use when downloading value sets (defaults to env "UMLS_PASSWORD") * @returns {Promise.} A promise that returns nothing when resolved and returns an error when rejected. */ - ensureValueSetsInLibrary(library, checkIncluded = true, umlsUserName = env['UMLS_USER_NAME'], umlsPassword = env['UMLS_PASSWORD']) { + ensureValueSetsInLibrary(library, checkIncluded = true, umlsUserName = env['UMLS_USER_NAME'], umlsPassword = env['UMLS_PASSWORD'], caching = true) { const valueSets = extractSetOfValueSetsFromLibrary(library, checkIncluded); - return this.ensureValueSets(Array.from(valueSets), umlsUserName, umlsPassword); + return this.ensureValueSets(Array.from(valueSets), umlsUserName, umlsPassword, caching); } findValueSetsByOid(oid) { diff --git a/lib/download-vsac.js b/lib/download-vsac.js index d5b1cfe..dd8aef2 100644 --- a/lib/download-vsac.js +++ b/lib/download-vsac.js @@ -5,7 +5,7 @@ const mkdirp = require('mkdirp'); const parseVSACXML = require('./parse-vsac'); const debug = require('debug')('vsac'); // To turn on DEBUG: $ export DEBUG=vsac -function downloadFromVSAC(username, password, input, output, vsDB={}) { +function downloadFromVSAC(username, password, input, output, vsDB={}, caching=true) { var vsJSON = {}; if (typeof(input) === 'string') { path.resolve(input); @@ -21,7 +21,7 @@ function downloadFromVSAC(username, password, input, output, vsDB={}) { if (Object.keys(vsJSON).length > 0) { output = path.resolve(output); const oids = Object.keys(vsJSON).map(k => vsJSON[k]); - if (!fs.existsSync(output)){ + if (caching && !fs.existsSync(output)){ mkdirp.sync(output); } return getTicketGrantingTicket(username, password) @@ -29,7 +29,7 @@ function downloadFromVSAC(username, password, input, output, vsDB={}) { const promises = oids.map(oid => { // Catch errors and convert to resolutions returning an error. This ensures Promise.all waits for all promises. // See: http://stackoverflow.com/questions/31424561/wait-until-all-es6-promises-complete-even-rejected-promises - return downloadValueSet(ticketGrantingTicket, oid, output, vsDB) + return downloadValueSet(ticketGrantingTicket, oid, output, vsDB, caching) .catch((err) => { debug(`Error downloading valueset ${oid}`, err); return new Error(`Error downloading valueset: ${oid}`); @@ -41,7 +41,7 @@ function downloadFromVSAC(username, password, input, output, vsDB={}) { const errors = results.filter(r => r instanceof Error); if (results.length - errors.length > 0) { // There were results, so write the file first before resolving/rejecting - return writeFile(path.join(output, 'valueset-db.json'), JSON.stringify(vsDB, null, 2)) + return writeFile(path.join(output, 'valueset-db.json'), JSON.stringify(vsDB, null, 2), caching) .then( (result) => errors.length == 0 ? result : Promise.reject(errors), (err) => { errors.push(err); return Promise.reject(errors); } @@ -66,14 +66,14 @@ function getTicketGrantingTicket(username, password) { return rpn(options); } -function downloadValueSet(ticketGrantingTicket, oid, output, vsDB={}) { +function downloadValueSet(ticketGrantingTicket, oid, output, vsDB={}, caching=true) { return getServiceTicket(ticketGrantingTicket) .then((serviceTicket) => { return getValueSet(serviceTicket, oid); }) .then((data) => { parseVSACXML(data, vsDB); - return writeFile(path.join(output, `${oid}.xml`), data); + return writeFile(path.join(output, `${oid}.xml`), data, caching); }); } @@ -96,18 +96,22 @@ function getValueSet(serviceTicket, oid) { return rpn(options); } -function writeFile(file, data) { +function writeFile(file, data, caching=true) { return new Promise((resolve, reject) => { - debug('Writing:', file); - fs.writeFile(file, data, (err) => { - if (typeof err !== 'undefined' && err != null) { - debug('Error writing file', file); - reject(err); - } else { - debug('Wrote file', file); - resolve(file); - } - }); + if (caching) { + debug('Writing:', file); + fs.writeFile(file, data, (err) => { + if (typeof err !== 'undefined' && err != null) { + debug('Error writing file', file); + reject(err); + } else { + debug('Wrote file', file); + resolve(file); + } + }); + } else { + resolve(); + } }); }