diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 0000000..62f9457 --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +6 \ No newline at end of file diff --git a/README.md b/README.md index e7fdb21..cc89e00 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,8 @@ Run `cost-of-modules` in the directory you are working in. `--include-dev` Include devDependencies as well - for 🚀 collaborator experience +`--exclude` Exclude specific packages from the calculation. Provide a comma-separated list of packages, for example: `--exclude=package1,package2` + #### Show your support :star: this repo diff --git a/package.json b/package.json index d84e445..e3a46b5 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "build-test": "babel test/src -d test/dist", "test": "npm run build && npm run build-test && npm run ava", "ava": "ava test/dist/*.js -s --no-cache", - "precommit": "lint-staged" + "precommit": "lint-staged", + "prettier": "prettier --write --single-quote --no-semi --trailing-comma es5" }, "files": [ "dist" @@ -42,7 +43,8 @@ "babel-cli": "6.18.0", "babel-preset-env": "^1.4.0", "husky": "0.13.3", - "lint-staged": "3.4.0" + "lint-staged": "3.4.0", + "prettier": "^3.0.0" }, "babel": { "presets": [ diff --git a/src/helpers.js b/src/helpers.js index af4af4f..5736d35 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -12,7 +12,7 @@ const path = require('path') */ let productionModifier = '--production' -let setup = includeDev => { +let setup = (includeDev) => { console.log() if (argv.includeDev || includeDev) productionModifier = '' @@ -80,14 +80,14 @@ let getRootDependencies = () => { /* to fix the missing du problem on windows */ -let dirSize = root => { +let dirSize = (root) => { let out = 0 let getDirSizeRecursively - ;(getDirSizeRecursively = rootLocal => { + ;(getDirSizeRecursively = (rootLocal) => { let itemStats = fs.lstatSync(rootLocal) if (itemStats.isDirectory()) { let allSubs = fs.readdirSync(rootLocal) - allSubs.forEach(file => { + allSubs.forEach((file) => { getDirSizeRecursively(path.join(rootLocal, file)) }) } else { @@ -100,10 +100,10 @@ let dirSize = root => { /* Get scoped modules */ -let getScopedModules = scope => { +let getScopedModules = (scope) => { let modules = {} let allScopes = fs.readdirSync(path.join('node_modules', scope)) - allScopes.forEach(name => { + allScopes.forEach((name) => { let itemStats = fs.lstatSync(path.join('node_modules', scope, name)) if (itemStats.isDirectory()) { let size = dirSize(path.join('node_modules', scope, name)) @@ -118,7 +118,7 @@ let getScopedModules = scope => { let getSizeForNodeModules = () => { let modules = {} let allModules = fs.readdirSync('node_modules') - allModules.forEach(name => { + allModules.forEach((name) => { let itemStats = fs.lstatSync(path.join('node_modules', name)) if (itemStats.isDirectory()) { if (name && name[0] === '@') { @@ -160,7 +160,7 @@ let getDependenciesRecursively = (modules = [], tree) => { children: [a, b, c, d] }] */ -let attachNestedDependencies = rootDependencies => { +let attachNestedDependencies = (rootDependencies) => { let flatDependencies = [] let dependencyTree = getDependencyTree() for (let i = 0; i < rootDependencies.length; i++) { @@ -180,7 +180,7 @@ let attachNestedDependencies = rootDependencies => { Root dependencies + all their children Deduplicate */ -let getAllDependencies = flatDependencies => { +let getAllDependencies = (flatDependencies) => { let allDependencies = [] for (let i = 0; i < flatDependencies.length; i++) { let dep = flatDependencies[i] @@ -249,6 +249,10 @@ const teardown = () => { } } +let getParsedArguments = () => { + return argv +} + module.exports = { setup, getSizeForNodeModules, @@ -257,4 +261,5 @@ module.exports = { getAllDependencies, displayResults, teardown, + getParsedArguments, } diff --git a/src/index.js b/src/index.js index 405cadf..a481af9 100644 --- a/src/index.js +++ b/src/index.js @@ -17,6 +17,12 @@ console.log() */ const moduleSizes = helpers.getSizeForNodeModules() +// Get command line arguments +const argv = helpers.getParsedArguments() + +// Get the packages to be excluded +const excludePackages = argv.exclude ? argv.exclude.split(",") : []; + /* Get root dependencies from tree These are the ones declared as dependendies in package.json @@ -31,7 +37,10 @@ let rootDependencies = helpers.getRootDependencies() children: [a, b, c, d] }] */ -let flatDependencies = helpers.attachNestedDependencies(rootDependencies) +let flatDependencies = helpers.attachNestedDependencies(rootDependencies).filter((dep) => { + // Exclude the packages specified by the 'exclude' flag + return !excludePackages.includes(dep.name); + }) /* Modules actual size = size of the module + size of it's children