Skip to content

Commit

Permalink
promisify utils/files.js readFile()
Browse files Browse the repository at this point in the history
  • Loading branch information
olgn committed Aug 28, 2018
1 parent fcaac2a commit e98216b
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 99 deletions.
48 changes: 24 additions & 24 deletions utils/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
})
Expand Down
13 changes: 7 additions & 6 deletions utils/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
142 changes: 73 additions & 69 deletions validators/bids.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit e98216b

Please sign in to comment.