Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Feb 17, 2019
0 parents commit 78a15cc
Show file tree
Hide file tree
Showing 12 changed files with 638 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules/
38 changes: 38 additions & 0 deletions README.md
@@ -0,0 +1,38 @@
# lockstat

and did you know there's only one company in the world that produces wooden
barrels?

## whümf

some statistics about your package-lock.json

## wetch

```shell
$ npm install -g lockstat
```

## commands

### depcount

print the number of version of each dependency in the tree

#### usage

```sh
$ lockstat depcount < package.json
```

#### options

##### --file=\<path>
the package-lock.json to operate on.
you can use `-` or leave this blank to accept input on STDIN

#### --min=\<int>
only print dependencies that have more than this number of versions.

#### --sort=\<dont|count|name>
how to sort the dependencies. `dont` is the default.
60 changes: 60 additions & 0 deletions commands/depcount.js
@@ -0,0 +1,60 @@
import {EOL} from "os"
import getLockfile from "../util/get-lockfile"
import recurseDependencies from "../util/recurse-dependencies"

export let command = "depcount"
export let describe = "the number of versions of each dependency"
export let aliases = [
"dependency-counts",
"dc"
]
export let builder = yargs => {
yargs
.option("min", {
describe: "show only dependencies that have more than 'min' numbers",
default: 0
})
.options("sort", {
default: "dont",
choices: ["dont", "count", "name"],
describe: "how to sort the dependencies"
})
.options("show")
.check(argv => {
if (!(Number.isInteger(argv.min) && argv.min >= 0)) {
throw new Error("Error: `min` must be a non-negative integer")
}

return true
})
}

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

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

let counts = {}
let maxNameLength = 0

await recurseDependencies(async (name, info) => {
maxNameLength = name.length > maxNameLength
? name.length
: maxNameLength

counts[name] = (counts[name] || 0) + 1

}, lockfile.dependencies)

Object.entries(counts)
.sort(sorts[argv.sort])
.filter(([, count]) => count >= argv.min)
.forEach(([name, count]) => {
process.stdout.write(
name.padEnd(maxNameLength, " ") + "\t" + count + EOL
)
})
}
3 changes: 3 additions & 0 deletions esmboot.js
@@ -0,0 +1,3 @@
#!/usr/bin/env node
require = require("esm")(module)
module.exports = require("./index.js")
12 changes: 12 additions & 0 deletions index.js
@@ -0,0 +1,12 @@
#!/usr/bin/env node
let path = require("path")

let commandDir = path.resolve(__dirname, "commands")

let {argv} = require("yargs")
.option("file", {
describe: "the package-lock.json to operate on, use - or nothing for stdin"
})
.commandDir(commandDir)
.demandCommand()
.epilogue("💖 🐕")

0 comments on commit 78a15cc

Please sign in to comment.