UNPKG

yapm

Version:

package manager for io.js (npm fork)

84 lines (67 loc) 1.67 kB
var plugins = [] module.exports.setup = function(arr) { plugins = [] arr.forEach(function(name) { try { var p = require('./plugins/' + name) } catch(_) { p = require(name) } plugins.push(p) }) } // cb(err, object, filename) module.exports.read = function(path, callback) { var arr = plugins.map(function(p) { return p.read }) next() function next() { if (!arr.length) { var e = Error('Cannot find package descriptor file in ' + path) e.code = 'ENOENT' return callback(e) } var fn = arr.shift() fn(path, function(err, data) { if (!err || err.code !== 'ENOENT') return callback(err, data) next() }) } } // cb(err, filename) module.exports.write = function(path, object, callback) { var arr = plugins.slice(0) next() function next() { if (!arr.length) { return plugins[0].write(path, object, callback) } var p = arr.shift() p.stat(path, function(err) { if (err) return next() p.write(path, object, callback) }) } } // cb(err, stat) module.exports.stat = function(path, callback) { var arr = plugins.map(function(p) { return p.stat }) next() function next() { if (!arr.length) { var e = Error('Cannot find package descriptor file in ' + path) e.code = 'ENOENT' return callback(e) } var fn = arr.shift() fn(path, function(err, data) { if (!err || err.code !== 'ENOENT') return callback(err, data) next() }) } } module.exports.setup(['yaml', 'json5', 'json', 'indexjs']) /**package {"name":"package-yaml","version":"0.0.0","description":"blah"} **/