Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Replace asyncMap with Promises
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Jan 15, 2018
1 parent 36b3d24 commit 28502b5
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 115 deletions.
63 changes: 32 additions & 31 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,36 +69,37 @@ check({
noDefaultEntries: !args['default-entries'],
extensions: extensions(args.e),
detective: args.detective
}, function (err, data) {
if (err) {
console.error(err.message)
return process.exit(1)
}
var pkg = data.package
var deps = data.used
var failed = 0
var options = {
excludeDev: args.dev === false,
excludePeer: args.peer === false,
ignore: [].concat(args.i || [])
}
if (args.extra) {
var extras = check.extra(pkg, deps, options)
failed += extras.length
if (extras.length) {
console.error('Fail! Modules in package.json not used in code: ' + extras.join(', '))
} else {
console.log('Success! All dependencies in package.json are used in the code')
})
.then(data => {
var pkg = data.package
var deps = data.used
var failed = 0
var options = {
excludeDev: args.dev === false,
excludePeer: args.peer === false,
ignore: [].concat(args.i || [])
}
}
if (args.missing || !args.extra) {
var missing = check.missing(pkg, deps, options)
failed += missing.length
if (missing.length) {
console.error('Fail! Dependencies not listed in package.json: ' + missing.join(', '))
} else {
console.log('Success! All dependencies used in the code are listed in package.json')
if (args.extra) {
var extras = check.extra(pkg, deps, options)
failed += extras.length
if (extras.length) {
console.error('Fail! Modules in package.json not used in code: ' + extras.join(', '))
} else {
console.log('Success! All dependencies in package.json are used in the code')
}
}
}
process.exit(args.ignore || !failed ? 0 : 1)
})
if (args.missing || !args.extra) {
var missing = check.missing(pkg, deps, options)
failed += missing.length
if (missing.length) {
console.error('Fail! Dependencies not listed in package.json: ' + missing.join(', '))
} else {
console.log('Success! All dependencies used in the code are listed in package.json')
}
}
process.exit(args.ignore || !failed ? 0 : 1)
})
.catch(err => {
console.error(err.message)
process.exit(1)
})
173 changes: 90 additions & 83 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,47 @@
var path = require('path')
var fs = require('fs')
var readPackage = require('read-package-json')
var asyncMap = require('async/map')
var builtins = require('builtins')()
var resolve = require('resolve')
var resolveModule = require('resolve')
var debug = require('debug')('dependency-check')
var isRelative = require('is-relative')

var promisedReadPackage = function (pkgPath) {
return new Promise((resolve, reject) => {
readPackage(pkgPath, (err, pkg) => {
if (err) return reject(err)
resolve(pkg)
})
})
}

module.exports = function (opts, cb) {
var pkgPath = opts.path
readPackage(pkgPath, function (err, pkg) {
if (err && err.code === 'EISDIR') {
pkgPath = path.join(pkgPath, 'package.json')
return readPackage(pkgPath, function (err, pkg) {
if (err) return cb(err)
parse({
path: pkgPath,
package: pkg,
entries: opts.entries,
noDefaultEntries: opts.noDefaultEntries,
builtins: opts.builtins,
extensions: getExtensions(opts.extensions, opts.detective)
}, cb)
})
}
parse({
var result = promisedReadPackage(pkgPath)
.catch(err => {
if (err && err.code === 'EISDIR') {
pkgPath = path.join(pkgPath, 'package.json')
return promisedReadPackage(pkgPath)
}
return Promise.reject(err)
})
.then(pkg => parse({
path: pkgPath,
package: pkg,
entries: opts.entries,
noDefaultEntries: opts.noDefaultEntries,
builtins: opts.builtins,
extensions: getExtensions(opts.extensions, opts.detective)
}, cb)
})
}))

if (cb) {
result
.then(value => { cb(null, value) })
.catch(err => { cb(err) })
return
}

return result
}

module.exports.missing = function (pkg, deps, options) {
Expand Down Expand Up @@ -127,7 +136,7 @@ function isNotRelative (file) {
return isRelative(file) && file[0] !== '.'
}

function parse (opts, cb) {
function parse (opts) {
var pkgPath = opts.path
var pkg = opts.package
var extensions = opts.extensions
Expand Down Expand Up @@ -163,86 +172,84 @@ function parse (opts, cb) {

debug('entry paths', paths)

if (paths.length === 0) return cb(new Error('No entry paths found'))

asyncMap(paths, function (file, cb) {
resolveDep(file, cb)
}, function (err, allDeps) {
if (err) return cb(err)
var used = {}
// merge all deps into one unique list
allDeps.forEach(function (deps) {
Object.keys(deps).forEach(function (dep) {
used[dep] = true
if (paths.length === 0) return Promise.reject(new Error('No entry paths found'))

return Promise.all(paths.map(file => resolveDep(file)))
.then(allDeps => {
var used = {}
// merge all deps into one unique list
allDeps.forEach(function (deps) {
Object.keys(deps).forEach(function (dep) {
used[dep] = true
})
})
})
if (opts.builtins) return cb(null, {package: pkg, used: Object.keys(used), builtins: core})

cb(null, {package: pkg, used: Object.keys(used)})
})
if (opts.builtins) return {package: pkg, used: Object.keys(used), builtins: core}

return {package: pkg, used: Object.keys(used)}
})

function resolveDep (file, callback) {
function resolveDep (file) {
if (isNotRelative(file)) {
return callback(null)
return Promise.resolve(null)
}

return resolve(file, {
basedir: path.dirname(file),
extensions: Object.keys(extensions)
}, function (err, path) {
if (err) return callback(err)

return getDeps(path, callback)
return new Promise((resolve, reject) => {
resolveModule(file, {
basedir: path.dirname(file),
extensions: Object.keys(extensions)
}, (err, path) => {
if (err) return reject(err)
resolve(path)
})
})
.then(path => getDeps(path))
}

function getDeps (file, callback) {
function getDeps (file) {
var ext = path.extname(file)
var detective = extensions[ext] || extensions['.js']

if (typeof detective !== 'function') {
return callback(new Error('Detective function missing for "' + file + '"'))
return Promise.reject(new Error('Detective function missing for "' + file + '"'))
}

fs.readFile(file, 'utf8', read)

function read (err, contents) {
if (err) return callback(err)

var requires = detective(contents)
var relatives = []
requires.map(function (req) {
var isCore = builtins.indexOf(req) > -1
if (isNotRelative(req) && !isCore) {
// require('foo/bar') -> require('foo')
if (req[0] !== '@' && req.indexOf('/') > -1) req = req.split('/')[0]
else if (req[0] === '@') req = req.split('/').slice(0, 2).join('/')
debug('require("' + req + '")' + ' is a dependency')
deps[req] = true
} else {
if (isCore) {
debug('require("' + req + '")' + ' is core')
if (core.indexOf(req) === -1) {
core.push(req)
}
return new Promise((resolve, reject) => {
fs.readFile(file, 'utf8', (err, contents) => {
if (err) return reject(err)
resolve(contents)
})
})
.then(contents => {
var requires = detective(contents)
var relatives = []
requires.map(function (req) {
var isCore = builtins.indexOf(req) > -1
if (isNotRelative(req) && !isCore) {
// require('foo/bar') -> require('foo')
if (req[0] !== '@' && req.indexOf('/') > -1) req = req.split('/')[0]
else if (req[0] === '@') req = req.split('/').slice(0, 2).join('/')
debug('require("' + req + '")' + ' is a dependency')
deps[req] = true
} else {
debug('require("' + req + '")' + ' is relative')
req = path.resolve(path.dirname(file), req)
if (seen.indexOf(req) === -1) {
seen.push(req)
relatives.push(req)
if (isCore) {
debug('require("' + req + '")' + ' is core')
if (core.indexOf(req) === -1) {
core.push(req)
}
} else {
debug('require("' + req + '")' + ' is relative')
req = path.resolve(path.dirname(file), req)
if (seen.indexOf(req) === -1) {
seen.push(req)
relatives.push(req)
}
}
}
}
})
})

asyncMap(relatives, function (name, cb) {
resolveDep(name, cb)
}, done)
}

function done (err) {
return callback(err, deps)
}
return Promise.all(relatives.map(name => resolveDep(name)))
.then(() => deps)
})
}
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
},
"author": "max ogden",
"dependencies": {
"async": "^2.1.4",
"builtins": "^2.0.0",
"debug": "^2.2.0",
"detective": "^4.0.0",
Expand Down

0 comments on commit 28502b5

Please sign in to comment.