Skip to content

Commit

Permalink
Add typings, add unit tests
Browse files Browse the repository at this point in the history
Move size formatting and pluralization to a shared module.
  • Loading branch information
prantlf committed Apr 12, 2021
1 parent 2ca00cd commit 5408a9f
Show file tree
Hide file tree
Showing 13 changed files with 519 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store
coverage
node_modules
pnpm-debug.log
test/actual
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"files.exclude": {
"**/.git": true,
"**/.DS_Store": true,
"**/coverage": true,
"**/pnpm-lock.yaml": true
},
"search.exclude": {
"**/test/actual": true
}
}
15 changes: 3 additions & 12 deletions bin → lib/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ for (let i = 2, l = argv.length; i < l; ++i) {
verbose = true
continue
case 'V': case 'version':
console.log(require('./package.json').version)
console.log(require('../package.json').version)
process.exit(0)
case 'h': case 'help':
require('./help')()
Expand All @@ -40,16 +40,7 @@ if (!refs.length) {
}

const estimatePkgs = require('.')

function formatSize(size) {
const kb = size / 1024
return kb > 0 ? kb > 99 ? `${(size / 1048576).toFixed(2)} MiB` :
`${kb.toFixed(2)} KiB` : `${size} B`
}

function pluralize(count, singular, plural) {
return count === 1 ? `${count} ${singular}` : `${count} ${plural}`
}
const { formatSize, pluralize } = require('./util')

function printProgress({ name, version, tarballSize, unpackedSize, depCount }, { print }) {
print(`${name}@${version}: ${pluralize(depCount, 'dependency', 'dependencies')}, ${formatSize(tarballSize)} tarball, ${formatSize(unpackedSize)} unpacked\n`)
Expand All @@ -61,7 +52,7 @@ function printProgress({ name, version, tarballSize, unpackedSize, depCount }, {
const pkgs = await estimatePkgs(refs, { progress, concurrency, verbose })
if (json) console.log(JSON.stringify(pkgs))
} catch (err) {
console.error(err)
console.error(verbose ? err : err.message)
process.exitCode = 1
}
})()
File renamed without changes.
15 changes: 15 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface Pkg {
name: string
version: string
tarballSize: integer
unpackedSize: integer
depCount: integer
}

export interface Opts {
progress?: (pkg: Pkg, opts: { print: (text: string) => void }) => void
concurrency?: integer
verbose?: boolean
}

export default function estimatePkgs(refs: string[], opts?: Opts): Promise<Pkg[]>
11 changes: 7 additions & 4 deletions index.js → lib/index.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const https = require('https')
const gunzip = require('gunzip-maybe')
const tar = require('tar-stream')
const { Writable } = require('stream')
const { pluralize } = require('./util')
const { keys } = Object
const { push } = Array.prototype
const print = process.stdout.write.bind(process.stdout)
Expand All @@ -19,7 +20,9 @@ function log(text) {
function loadNpm() {
return new Promise((resolve, reject) => {
npm.load({ loaded: false }, err => {
/* c8 ignore start */
if (err) reject(err)
/* c8 ignore end */
else resolve()
})
})
Expand Down Expand Up @@ -53,12 +56,15 @@ function inspectPkg(ref) {
function makeRequest(method, url) {
return new Promise((resolve, reject) => {
log(` ${method} ${url}\n`)
/* c8 ignore start */
const proto = url.startsWith('http:') ? http : https
/* c8 ignore end */
const req = proto
.request(url, { method })
.on('response', res => {
const { statusCode, statusMessage, headers } = res
if (statusCode === 200) resolve(res)
/* c8 ignore start */
else if (statusCode >= 300 && statusCode < 400) {
res.resume()
makeRequest(method, headers.location, callback).then(resolve, reject)
Expand All @@ -68,6 +74,7 @@ function makeRequest(method, url) {
req.abort()
reject(new Error(`${url}: timeout`))
})
/* c8 ignore end */
.on('error', reject)
req.end()
})
Expand Down Expand Up @@ -155,10 +162,6 @@ function enableOut() {
process.stderr.write = writeErr
}

function pluralize(count, singular, plural) {
return count === 1 ? `${count} ${singular}` : `${count} ${plural}`
}

async function estimatePkg(ref) {
log(` Inspecting ${ref}\n`)
const pkg = await inspectPkg(ref)
Expand Down
11 changes: 11 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function formatSize(size) {
const kb = size / 1024
return kb >= 1 ? kb > 99 ? `${(size / 1048576).toFixed(2)} MiB` :
`${kb.toFixed(2)} KiB` : `${size} B`
}

function pluralize(count, singular, plural) {
return count === 1 ? `${count} ${singular}` : `${count} ${plural}`
}

module.exports = { formatSize, pluralize }
24 changes: 18 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,24 @@
"node": ">=12"
},
"files": [
"bin",
"index.js",
"help.js"
"lib"
],
"bin": {
"package-cost": "bin"
"package-cost": "lib/bin.js"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"test": "node bin"
"test": "mkdir -p test/actual && c8 node lib/bin && c8 --no-clean node lib/bin -V && c8 --no-clean node lib/bin -h && c8 --no-clean node lib/bin -x || c8 --no-clean node lib/bin -c c || c8 --no-clean node lib/bin @prantlf/none || c8 --no-clean node lib/bin -v @prantlf/none || c8 --no-clean node lib/bin janadom@0.1.2 > test/actual/janadom.txt && c8 --no-clean node lib/bin -j -v -c 5 baretest@2.0.0 barecolor@1.0.1 baretest@2.0.0 > test/actual/baretest.txt && c8 --no-clean node lib/bin -v test@0.6.0 > test/actual/test.txt && c8 --no-clean node lib/bin.js janadom@git+https://github.com/prantlf/janadom.git && diff -q test/janadom.txt test/actual/janadom.txt && diff -q test/baretest.txt test/actual/baretest.txt && diff -q test/test.txt test/actual/test.txt && c8 --no-clean node test/test && c8 report -r text && c8 check-coverage"
},
"c8": {
"reporter": [
"lcov"
],
"branches": 100,
"lines": 100,
"functions": 100,
"statements": 100
},
"dependencies": {
"gunzip-maybe": "^1.4.2",
Expand All @@ -49,5 +58,8 @@
"package",
"size",
"cost"
]
],
"devDependencies": {
"c8": "^7.7.1"
}
}
Loading

0 comments on commit 5408a9f

Please sign in to comment.