Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --exclude=package1,package2 flag #71

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look like this file is used? Accidental addition?

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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": [
Expand Down
23 changes: 14 additions & 9 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const path = require('path')
*/
let productionModifier = '--production'

let setup = includeDev => {
let setup = (includeDev) => {
console.log()

if (argv.includeDev || includeDev) productionModifier = ''
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
Expand All @@ -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] === '@') {
Expand Down Expand Up @@ -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++) {
Expand All @@ -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]
Expand Down Expand Up @@ -249,6 +249,10 @@ const teardown = () => {
}
}

let getParsedArguments = () => {
return argv
}

module.exports = {
setup,
getSizeForNodeModules,
Expand All @@ -257,4 +261,5 @@ module.exports = {
getAllDependencies,
displayResults,
teardown,
getParsedArguments,
}
11 changes: 10 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down