diff --git a/index.js b/index.js index 1d5e7f1..33abda0 100644 --- a/index.js +++ b/index.js @@ -31,30 +31,28 @@ module.exports = function addRemoteScripts(bify, remote, cb) { var keys = Object.keys(remote); if (!keys.length) return cb(null, []); - var gens = []; - - var tasks = keys - .map(function (k) { - var rem = remote[k]; - rem.key = k; - - return function (cb) { - genRemote(rem, function (err, gen) { - if (err) return cb(err); - gens.push(gen); - cb(); - }); - }; - }) - .concat(genIndex.bind(null, gens)) - .concat( - function (err) { - if (err) return cb(err); - if (bify) gens.forEach(function (gen) { bify.add(gen); }); - - cb(null, gens); - } - ); + var tasks = [ runnel.seed([]) ] + .concat(keys.map( + function (k) { + var rem = remote[k]; + rem.key = k; + + return function (acc, cb) { + genRemote(rem, function (err, gen) { + if (err) return cb(err); + acc.push(gen); + cb(null, acc); + }); + }; + }) + ) + .concat(genIndex) + .concat(function done (err, acc) { + if (err) return cb(err); + if (bify) acc.forEach(bify.add.bind(bify)); + + cb(null, acc); + }); runnel(tasks); }); diff --git a/lib/gen-index.js b/lib/gen-index.js index e8b91bc..9127a24 100644 --- a/lib/gen-index.js +++ b/lib/gen-index.js @@ -16,7 +16,7 @@ var genPath = path.join(__dirname, '..', 'loaders'); * @name genIndex * @function * @param fullPaths {Array} of full paths to the loader files - * @param cb {Function} calls back with error or none + * @param cb {Function} calls back with error or none and same fullPaths that were passed in */ var genIndex = module.exports = function (fullPaths, cb) { if (!fullPaths.length) cb(null); @@ -30,5 +30,7 @@ var genIndex = module.exports = function (fullPaths, cb) { var s = index({ keys: keys }); var filePath = path.join(genPath, 'index.js'); - fs.writeFile(filePath, s, 'utf8', cb); + fs.writeFile(filePath, s, 'utf8', function (err) { + cb(err, fullPaths); + }); }; diff --git a/package.json b/package.json index 3e5abb8..b109e16 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "scriptjs": "~2.4.0", "xtend": "~2.0.6", "handlebars": "~1.0.12", - "runnel": "~0.4.1" + "runnel": "~0.5.0" }, "devDependencies": { "tape": "~1.0.4",