From e98216b2a1e8193216c1f75178cf5aa04cbb6a68 Mon Sep 17 00:00:00 2001 From: Teal Hobson-Lowther Date: Tue, 28 Aug 2018 10:23:56 -0700 Subject: [PATCH] promisify utils/files.js readFile() --- utils/files.js | 48 +++++++-------- utils/options.js | 13 +++-- validators/bids.js | 142 +++++++++++++++++++++++---------------------- 3 files changed, 104 insertions(+), 99 deletions(-) diff --git a/utils/files.js b/utils/files.js index 3da9a9907..5764ad0af 100644 --- a/utils/files.js +++ b/utils/files.js @@ -43,34 +43,34 @@ var fileUtils = { * In node the file should be a path to a file. * */ -function readFile(file, callback) { - if (fs) { - testFile(file, function(issue) { - if (issue) { - process.nextTick(function() { - callback(issue, null) - }) - return - } - fs.readFile(file.path, 'utf8', function(err, data) { - process.nextTick(function() { - callback(null, data) +function readFile(file) { + return new Promise((resolve, reject) => { + if (fs) { + testFile(file, function(issue) { + if (issue) { + process.nextTick(function() { + return reject(issue) + }) + } + fs.readFile(file.path, 'utf8', function(err, data) { + process.nextTick(function() { + return resolve(data) + }) }) }) - }) - } else { - var reader = new FileReader() - reader.onloadend = function(e) { - if (e.target.readyState == FileReader.DONE) { - if (!e.target.result) { - callback(new Issue({ code: 44, file: file }), null) - return + } else { + var reader = new FileReader() + reader.onloadend = function(e) { + if (e.target.readyState == FileReader.DONE) { + if (!e.target.result) { + return reject(new Issue({ code: 44, file: file })) + } + return resolve(e.target.result) } - callback(null, e.target.result) } + reader.readAsBinaryString(file) } - reader.readAsBinaryString(file) - } + }) } function getBIDSIgnoreFileObjNode(dir) { @@ -120,7 +120,7 @@ function getBIDSIgnore(dir, callback) { var bidsIgnoreFileObj = getBIDSIgnoreFileObj(dir) if (bidsIgnoreFileObj) { - readFile(bidsIgnoreFileObj, function(issue, content) { + readFile(bidsIgnoreFileObj).then(content => { ig = ig.add(content) callback(ig) }) diff --git a/utils/options.js b/utils/options.js index 9fe9edd19..f53d691eb 100644 --- a/utils/options.js +++ b/utils/options.js @@ -31,13 +31,14 @@ module.exports = { loadConfig: function(config, callback) { if (typeof config === 'string') { // load file - files.readFile({ path: config }, function(issue, contents) { - if (issue) { - callback([issue], { path: config }, null) - } else { + files + .readFile({ path: config }) + .then(contents => { callback(null, { path: config }, contents) - } - }) + }) + .catch(issue => { + callback([issue], { path: config }, null) + }) } else if (typeof config === 'object') { callback(null, { path: 'config' }, JSON.stringify(config)) } diff --git a/validators/bids.js b/validators/bids.js index c90b670b0..54de631cb 100644 --- a/validators/bids.js +++ b/validators/bids.js @@ -380,103 +380,107 @@ BIDS = { }), ) } - utils.files.readFile(file, function(issue, contents) { - if (issue) { - self.issues.push(issue) - process.nextTick(cb) - return - } - if (file.name.endsWith('_events.tsv')) { - events.push({ - file: file, - path: file.relativePath, - contents: contents, - }) - } - TSV.TSV(file, contents, fileList, function( - issues, - participantList, - stimFiles, - ) { - if (participantList) { - if (file.name.endsWith('participants.tsv')) { - participants = { - list: participantList, - file: file, + utils.files + .readFile(file) + .then(contents => { + if (file.name.endsWith('_events.tsv')) { + events.push({ + file: file, + path: file.relativePath, + contents: contents, + }) + } + TSV.TSV(file, contents, fileList, function( + issues, + participantList, + stimFiles, + ) { + if (participantList) { + if (file.name.endsWith('participants.tsv')) { + participants = { + list: participantList, + file: file, + } + } else if (file.relativePath.includes('phenotype/')) { + phenotypeParticipants.push({ + list: participantList, + file: file, + }) } - } else if (file.relativePath.includes('phenotype/')) { - phenotypeParticipants.push({ - list: participantList, - file: file, - }) } - } - if (stimFiles.length) { - // add unique new events to the stimuli.events array - stimuli.events = [...new Set([...stimuli.events, ...stimFiles])] - } - self.issues = self.issues.concat(issues) + if (stimFiles.length) { + // add unique new events to the stimuli.events array + stimuli.events = [ + ...new Set([...stimuli.events, ...stimFiles]), + ] + } + self.issues = self.issues.concat(issues) + process.nextTick(cb) + }) + }) + .catch(issue => { + self.issues.push(issue) process.nextTick(cb) }) - }) } // validate bvec else if (file.name && file.name.endsWith('.bvec')) { - utils.files.readFile(file, function(issue, contents) { - if (issue) { + utils.files + .readFile(file) + .then(contents => { + bContentsDict[file.relativePath] = contents + bvec(file, contents, function(issues) { + self.issues = self.issues.concat(issues) + process.nextTick(cb) + }) + }) + .catch(issue => { self.issues.push(issue) process.nextTick(cb) - return - } - bContentsDict[file.relativePath] = contents - bvec(file, contents, function(issues) { - self.issues = self.issues.concat(issues) - process.nextTick(cb) }) - }) } // validate bval else if (file.name && file.name.endsWith('.bval')) { - utils.files.readFile(file, function(issue, contents) { - if (issue) { + utils.files + .readFile(file) + .then(contents => { + bContentsDict[file.relativePath] = contents + bval(file, contents, function(issues) { + self.issues = self.issues.concat(issues) + process.nextTick(cb) + }) + }) + .catch(issue => { self.issues.push(issue) process.nextTick(cb) - return - } - bContentsDict[file.relativePath] = contents - bval(file, contents, function(issues) { - self.issues = self.issues.concat(issues) - process.nextTick(cb) }) - }) } // load json data for validation later else if (file.name && file.name.endsWith('.json')) { - utils.files.readFile(file, function(issue, contents) { - if (issue) { - self.issues.push(issue) - process.nextTick(cb) - return - } - utils.json.parse(file, contents, function(issues, jsObj) { - self.issues = self.issues.concat(issues) - - // abort further tests if schema test does not pass - for (var i = 0; i < issues.length; i++) { - if (issues[i].severity === 'error') { + utils.files + .readFile(file) + .then(contents => { + utils.json.parse(file, contents, function(issues, jsObj) { + self.issues = self.issues.concat(issues) + + // abort further tests if schema test does not pass + if (issues.some(issue => issue.severity === 'error')) { process.nextTick(cb) return } - } - jsonContentsDict[file.relativePath] = jsObj - jsonFiles.push(file) + jsonContentsDict[file.relativePath] = jsObj + jsonFiles.push(file) + process.nextTick(cb) + }) + }) + .catch(issue => { + self.issues.push(issue) process.nextTick(cb) }) - }) } else { process.nextTick(cb) }