Navigation Menu

Skip to content

Commit

Permalink
Filter out duplicate dependency versions
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Feb 20, 2019
1 parent 553f4ab commit c692a81
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
18 changes: 14 additions & 4 deletions commands/depcount.js
@@ -1,6 +1,6 @@
import {EOL} from "os"
import getLockfile from "../functions/get-lockfile"
import countDependencies from "../functions/recursors/count-dependencies"
import gatherDependencyVersions from "../functions/recursors/gather-dependency-versions"
import getLongestName from "../functions/recursors/get-longest-name"

export let command = "depcount"
Expand Down Expand Up @@ -31,19 +31,29 @@ export let builder = yargs => {
}

let sorts = {
dont: Function.prototype,
count: ([, a], [, b]) => b - a,
name: ([a, ], [b, ]) => a.localeCompare(b)
}

let countVersions = ([name, versions]) => [
name,
versions.length
]

let minFilter = min => ([, count]) =>
count >= min

export async function handler (argv) {
let lockfile = await getLockfile(argv)

let counts = await countDependencies()(lockfile.dependencies)
let versions = await gatherDependencyVersions()(lockfile.dependencies)
let longestName = await getLongestName()(lockfile.dependencies)

Object.entries(counts)
Object.entries(versions)
.map(countVersions)
.sort(sorts[argv.sort])
.filter(([, count]) => count >= argv.min)
.filter(minFilter(argv.min))
.forEach(([name, count]) => {
process.stdout.write(
name.padEnd(longestName.length, " ") + "\t" + count + EOL
Expand Down
13 changes: 0 additions & 13 deletions functions/recursors/count-dependencies.js

This file was deleted.

14 changes: 14 additions & 0 deletions functions/recursors/gather-dependency-versions.js
@@ -0,0 +1,14 @@
import createRecursor from "../create-recursor"
import fastclone from "fast-clone"

export let initial = {}

export let createReducer = () =>
function reducer (current, name, info) {
let counts = fastclone(current)
counts[name] = [...new Set((counts[name] || []).concat(info.version))]
return counts
}

let recursor = () => createRecursor(createReducer(), initial)
export default recursor

0 comments on commit c692a81

Please sign in to comment.