Skip to content

Commit

Permalink
Add a pattern to match on to depcount and gadeve
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Feb 21, 2019
1 parent 9739805 commit ef77547
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
31 changes: 23 additions & 8 deletions commands/depcount.js
Expand Up @@ -2,6 +2,7 @@ import {EOL} from "os"
import getLockfile from "../functions/get-lockfile"
import gatherDependencyVersions from "../functions/recursors/gather-dependency-versions"
import getLongestName from "../functions/recursors/get-longest-name"
import * as symbols from "../symbols"

export let command = "depcount"
export let describe = "the number of versions of each dependency"
Expand All @@ -18,15 +19,20 @@ export let builder = yargs => {
.options("sort", {
default: "dont",
choices: ["dont", "count", "name"],
describe: "how to sort the dependencies"
describe: "sort the dependencies as so"
})
.options("production", {
alias: ["prod", "p"],
type: "boolean",
default: "false",
describe: "only count the production (non-dev) tree"
})
.options("show")
.options("pattern", {
alias: ["r"],
type: "array",
describe: "only count packages whose name match one of these patterns",
coerce: pattern => new RegExp(pattern)
})
.check(argv => {
if (!(Number.isInteger(argv.min) && argv.min >= 0)) {
throw new Error("Error: `min` must be a non-negative integer")
Expand Down Expand Up @@ -56,13 +62,22 @@ export async function handler (argv) {
let versions = await gatherDependencyVersions(argv)(lockfile.dependencies)
let longestName = await getLongestName()(lockfile.dependencies)

Object.entries(versions)
.map(countVersions)
.sort(sorts[argv.sort])
.filter(minFilter(argv.min))
.forEach(([name, count]) => {
let entries = Object.entries(versions)
let counted = entries.map(countVersions)
let sorted = argv.sort && argv.sort != "dont"
? counted.sort(sorts[argv.sort])
: counted
let filtered = argv.min
? sorted.filter(minFilter(argv.min))
: sorted

if (!argv[symbols.internal]) {
filtered.forEach(([name, count]) => {
process.stdout.write(
name.padEnd(longestName.length, " ") + "\t" + count + EOL
)
})
})
}

return filtered
}
12 changes: 10 additions & 2 deletions functions/recursors/gather-dependency-versions.js
Expand Up @@ -3,15 +3,23 @@ import fastclone from "fast-clone"

export let initial = {}

export let createReducer = ({production = false} = {}) =>
export let createReducer = (options = {}) =>
function reducer (current, name, info) {
let {
production = false,
pattern
} = options
if (production && info.dev) {
return current
}
if (pattern && !pattern.exec(name)) {
return current
}
let counts = fastclone(current)
counts[name] = [...new Set((counts[name] || []).concat(info.version))]
return counts
}

let recursor = ({production}) => createRecursor(createReducer({production}), initial)
let recursor = options => createRecursor(createReducer(options), initial)

export default recursor

0 comments on commit ef77547

Please sign in to comment.