diff options
author | Gar <gar+gh@danger.computer> | 2021-07-22 17:49:40 +0300 |
---|---|---|
committer | Gar <gar+gh@danger.computer> | 2021-08-11 19:36:43 +0300 |
commit | a1bdbea974ebfc6694b4c8ad5da86215c2924dde (patch) | |
tree | 3b1ab9b288ee9ed84afdcec2cb8ca7ee1f55ca46 /lib | |
parent | d1812f1a627d6a4d4cb6d07d7735d2d2cc2cf264 (diff) |
deps: remove byte-size
In its latest release, byte-size dropped support for node versions lower
than 14. The cli currently supports node 10 and up.
The actual functionality we needed and was using was extremely limited
in scope, and didn't warrant an external module. It's just pretty
printing a file size, and the files we are dealing with are limited in
size so we don't need to support so many suffixes.
PR-URL: https://github.com/npm/cli/pull/3569
Credit: @wraithgar
Close: #3569
Reviewed-by: @isaacs
Diffstat (limited to 'lib')
-rw-r--r-- | lib/utils/format-bytes.js | 22 | ||||
-rw-r--r-- | lib/utils/tar.js | 10 | ||||
-rw-r--r-- | lib/view.js | 6 |
3 files changed, 30 insertions, 8 deletions
diff --git a/lib/utils/format-bytes.js b/lib/utils/format-bytes.js new file mode 100644 index 000000000..87fb561aa --- /dev/null +++ b/lib/utils/format-bytes.js @@ -0,0 +1,22 @@ +// Convert bytes to printable output, for file reporting in tarballs +// Only supports up to GB because that's way larger than anything the registry +// supports anyways. + +const formatBytes = (bytes, space = true) => { + let spacer = '' + if (space) + spacer = ' ' + + if (bytes < 1000) // B + return `${bytes}${spacer}B` + + if (bytes < 1000000) // kB + return `${(bytes / 1000).toFixed(1)}${spacer}kB` + + if (bytes < 1000000000) // MB + return `${(bytes / 1000000).toFixed(1)}${spacer}MB` + + return `${(bytes / 1000000000).toFixed(1)}${spacer}GB` +} + +module.exports = formatBytes diff --git a/lib/utils/tar.js b/lib/utils/tar.js index 9e7c33295..c3071c1bd 100644 --- a/lib/utils/tar.js +++ b/lib/utils/tar.js @@ -1,7 +1,7 @@ const tar = require('tar') const ssri = require('ssri') const npmlog = require('npmlog') -const byteSize = require('byte-size') +const formatBytes = require('./format-bytes.js') const columnify = require('columnify') const logTar = (tarball, opts = {}) => { @@ -11,9 +11,9 @@ const logTar = (tarball, opts = {}) => { log.notice('=== Tarball Contents ===') if (tarball.files.length) { log.notice('', columnify(tarball.files.map((f) => { - const bytes = byteSize(f.size) + const bytes = formatBytes(f.size, false) return (/^node_modules\//.test(f.path)) ? null - : { path: f.path, size: `${bytes.value}${bytes.unit}` } + : { path: f.path, size: `${bytes}` } }).filter(f => f), { include: ['size', 'path'], showHeaders: false, @@ -28,8 +28,8 @@ const logTar = (tarball, opts = {}) => { { name: 'name:', value: tarball.name }, { name: 'version:', value: tarball.version }, tarball.filename && { name: 'filename:', value: tarball.filename }, - { name: 'package size:', value: byteSize(tarball.size) }, - { name: 'unpacked size:', value: byteSize(tarball.unpackedSize) }, + { name: 'package size:', value: formatBytes(tarball.size) }, + { name: 'unpacked size:', value: formatBytes(tarball.unpackedSize) }, { name: 'shasum:', value: tarball.shasum }, { name: 'integrity:', diff --git a/lib/view.js b/lib/view.js index 47e631f55..f4fc5974e 100644 --- a/lib/view.js +++ b/lib/view.js @@ -1,6 +1,5 @@ // npm view [pkg [pkg ...]] -const byteSize = require('byte-size') const color = require('ansicolors') const columns = require('cli-columns') const fs = require('fs') @@ -8,6 +7,7 @@ const jsonParse = require('json-parse-even-better-errors') const log = require('npmlog') const npa = require('npm-package-arg') const { resolve } = require('path') +const formatBytes = require('./utils/format-bytes.js') const relativeDate = require('tiny-relative-date') const semver = require('semver') const style = require('ansistyles') @@ -315,7 +315,7 @@ class View extends BaseCommand { tags.push(`${style.bright(color.green(t))}: ${version}`) }) const unpackedSize = manifest.dist.unpackedSize && - byteSize(manifest.dist.unpackedSize) + formatBytes(manifest.dist.unpackedSize, true) const licenseField = manifest.license || 'Proprietary' const info = { name: color.green(manifest.name), @@ -356,7 +356,7 @@ class View extends BaseCommand { manifest.dist.integrity && color.yellow(manifest.dist.integrity), fileCount: manifest.dist.fileCount && color.yellow(manifest.dist.fileCount), - unpackedSize: unpackedSize && color.yellow(unpackedSize.value) + ' ' + unpackedSize.unit, + unpackedSize: unpackedSize && color.yellow(unpackedSize), } if (info.license.toLowerCase().trim() === 'proprietary') info.license = style.bright(color.red(info.license)) |