Created
November 10, 2015 16:24
-
-
Save Maximilianos/fd281b59356c4375e8f4 to your computer and use it in GitHub Desktop.
quick script to log packages that are registered in your package.json but not explicitly called in your code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable */ | |
/** | |
* Approximately list packages that are | |
* registered in your package json but | |
* not used in your code | |
* | |
*/ | |
// === config start === // | |
// the files to read to determine actual | |
// used packages | |
var fileGlob = '{gulpfile.js,gulp/**/*.js}'; | |
// the package.json keys to use to determine | |
// registered dependencies | |
var registeredIn = ['devDependencies']; | |
// any packages you would want to exclude | |
// from the final list | |
var excludePackages = []; | |
// package json | |
var packageJSON = require('./package.json'); | |
// === config end === // | |
var readGlob = require('read-glob-promise'); | |
function matchRequires(packages, content) { | |
var matches = content.match(/require\(('|")[a-z0-9\-_\\/]+('|")\)/gi); | |
return matches ? packages.concat(matches) : packages; | |
} | |
function unique(req, index, packages) { | |
return packages.indexOf(req) === index; | |
} | |
function trimRequire(req) { | |
return req.replace(/(require\()|('|")|\)/gi, ''); | |
} | |
function alphabetically(a, b) { | |
if (a < b) return -1; | |
if (a > b) return 1; | |
return 0; | |
} | |
function registeredDeps(pckgJson, keys) { | |
return keys | |
.reduce(function (packages, key) { | |
return pckgJson[key] | |
? packages.concat(Object.keys(pckgJson[key])) | |
: packages; | |
}, []); | |
} | |
function log(message) { | |
return function (packages) { | |
console.log('===>', packages.length, message, '\n', JSON.stringify(packages, null, 2)); | |
return packages; | |
}; | |
} | |
function exclude(excluded) { | |
return function (pckg) { | |
return 0 > excluded.indexOf(pckg); | |
} | |
} | |
readGlob(fileGlob, 'utf8') | |
.then(function (contents) { | |
return contents | |
.reduce(matchRequires, []) | |
.filter(unique) | |
.map(trimRequire) | |
.sort(alphabetically); | |
}) | |
.then(log('used packages:')) | |
.then(function (usedPackages) { | |
return registeredDeps(packageJSON, registeredIn) | |
.filter(exclude(usedPackages)) | |
.filter(exclude(excludePackages)); | |
}) | |
.then(log('registered but not explicitly used packages:')) | |
.catch(function (err) { | |
console.error(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
just add to project root and run
npm i read-glob-promise
then customize the config part of the script and then runnode unused-dependencies
.you should see the output in your console