Skip to content

Commit

Permalink
fix: Eliminated callback at the top-level.
Browse files Browse the repository at this point in the history
One down, two to go.
  • Loading branch information
jezhiggins committed Apr 27, 2020
1 parent 5ee3a05 commit 68d03d9
Showing 1 changed file with 66 additions and 65 deletions.
131 changes: 66 additions & 65 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ const FileBuilder = require('./File-builder')
const Writer = require('./Writer')
const jsonfile = require('jsonfile')
const pump = require('pump')
const promisify = require('util').promisify

function smithereensCB (sourceFilePaths, options, callback) {
async function smithereens (sourceFilePaths, options) {
// Turn sourceFilePaths to an array, if it's not already
if (!_.isArray(sourceFilePaths)) {
sourceFilePaths = [sourceFilePaths]
Expand All @@ -22,81 +21,83 @@ function smithereensCB (sourceFilePaths, options, callback) {
debug('sourceFilePaths:' + JSON.stringify(sourceFilePaths, null, 2))
debug('options:' + JSON.stringify(options, null, 2))

// Configure parser
const parserOptions = options.parser
parserOptions.objectMode = true
parserOptions.columns = false
const [manifest, outputDir] = await processFiles(sourceFilePaths, options)

// Create a file builder
const fileBuilder = new FileBuilder(options)
await jsonfile.writeFile(
path.join(outputDir, 'manifest.json'),
manifest,
{
spaces: 2
}
)

async.eachSeries(
sourceFilePaths,
// Loop over all provided file locations
function (fileSource, cb) {
glob(
fileSource,
{
nodir: true
},
function (err, files) {
if (err) {
cb(err)
} else {
// Loop over all absolute file locations
async.eachSeries(
files,
function (filePath, cb2) {
cb2 = _.once(cb2)
debug(`Streaming files from ${filePath}`)
return manifest
}

const rs = fs.createReadStream(filePath)
const parser = csvParser(parserOptions)
const writer = new Writer(fileBuilder, options)
async function processFiles (sourceFilePaths, options) {
return new Promise((resolve, reject) => {
// Configure parser
const parserOptions = options.parser
parserOptions.objectMode = true
parserOptions.columns = false

pump(
rs,
parser,
writer,
cb2
)
},
function (err) {
if (err) {
cb(err)
} else {
cb(null)
}
}
)
}
}
)
},
function (err) {
if (err) {
callback(err)
} else {
fileBuilder.close()
const manifest = fileBuilder.getManifest()
// Create a file builder
const fileBuilder = new FileBuilder(options)

jsonfile.writeFile(
path.join(fileBuilder.outputDirRootPath, 'manifest.json'),
manifest,
async.eachSeries(
sourceFilePaths,
// Loop over all provided file locations
function (fileSource, cb) {
glob(
fileSource,
{
spaces: 2
nodir: true
},
function (err) {
function (err, files) {
if (err) {
callback(err)
cb(err)
} else {
callback(null, manifest)
// Loop over all absolute file locations
async.eachSeries(
files,
function (filePath, cb2) {
cb2 = _.once(cb2)
debug(`Streaming files from ${filePath}`)

const rs = fs.createReadStream(filePath)
const parser = csvParser(parserOptions)
const writer = new Writer(fileBuilder, options)

pump(
rs,
parser,
writer,
cb2
)
},
function (err) {
if (err) {
cb(err)
} else {
cb(null)
}
}
)
}
}
)
},
err => {
if (err)
return reject(err)
fileBuilder.close()
resolve([
fileBuilder.getManifest(),
fileBuilder.outputDirRootPath
])
}
}
)
)
})
}

module.exports = promisify(smithereensCB)
module.exports = smithereens

0 comments on commit 68d03d9

Please sign in to comment.