-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(bin): add pretty table summary; (#13)
- mostly lifted from `lukeed/bundt` and `lukeed/freshie`
- Loading branch information
Showing
4 changed files
with
82 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// @see https://github.com/lukeed/bundt/blob/master/index.js | ||
const { white, cyan, dim } = require('kleur'); | ||
const { readFileSync } = require('fs'); | ||
const { normalize } = require('path'); | ||
const { gzipSync } = require('zlib'); | ||
|
||
const _ = ' '; | ||
const UNITS = ['B ', 'kB', 'MB', 'GB']; | ||
const lpad = (str, max) => _.repeat(max - str.length) + str; | ||
const rpad = (str, max) => str + _.repeat(max - str.length); | ||
const th = dim().bold().italic().underline; | ||
|
||
function size(val=0) { | ||
if (val < 1e3) return `${val} ${UNITS[0]}`; | ||
let exp = Math.min(Math.floor(Math.log10(val) / 3), UNITS.length - 1) || 1; | ||
let out = (val / Math.pow(1e3, exp)).toPrecision(3); | ||
let idx = out.indexOf('.'); | ||
if (idx === -1) { | ||
out += '.00'; | ||
} else if (out.length - idx - 1 !== 2) { | ||
out = (out + '00').substring(0, idx + 3); // 2 + 1 for 0-based | ||
} | ||
return out + ' ' + UNITS[exp]; | ||
} | ||
|
||
/** @type {string[]} */ | ||
const FILELIST = []; | ||
|
||
/** | ||
* @param {string} file | ||
* @returns {string} | ||
*/ | ||
exports.save = function (file) { | ||
file = normalize(file); | ||
return FILELIST.push(file) && file; | ||
} | ||
|
||
/** @returns {void} */ | ||
exports.table = function () { | ||
let f=8, s=8, g=6; | ||
let out='', data, tmp; | ||
let G1 = _+_, G2 = G1+G1; | ||
|
||
let arr = FILELIST.sort().map(fname => { | ||
data = readFileSync(fname); | ||
|
||
tmp = { | ||
file: fname, | ||
size: size(data.byteLength), | ||
gzip: size(gzipSync(data).byteLength) | ||
}; | ||
|
||
f = Math.max(f, tmp.file.length); | ||
s = Math.max(s, tmp.size.length); | ||
g = Math.max(g, tmp.gzip.length); | ||
|
||
return tmp; | ||
}); | ||
|
||
f += 2; // underline extension | ||
|
||
out += G1 + th(rpad('Filename', f)) + G2 + th(lpad('Filesize', s)) + G1 + dim().bold().italic(lpad('(gzip)', g)); | ||
|
||
arr.forEach((obj, idx) => { | ||
if (idx && idx % 2 === 0) out += '\n'; | ||
out += ('\n' + G1 + white(rpad(obj.file, f)) + G2 + cyan(lpad(obj.size, s)) + G1 + dim().italic(lpad(obj.gzip, g))); | ||
}); | ||
|
||
console.log('\n' + out + '\n'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,35 @@ | ||
const { parse, format } = require('path'); | ||
const { copyFileSync, existsSync } = require('fs'); | ||
const { save, table } = require('./format'); | ||
const pkg = require('../package.json'); | ||
const esbuild = require('./esbuild'); | ||
|
||
const externals = ['worktop', ...Object.keys(pkg.dependencies)]; | ||
|
||
/** | ||
* @TODO print sizes | ||
* @param {string} input | ||
* @param {string} output | ||
*/ | ||
async function bundle(input, output) { | ||
await esbuild.build(input, output, externals); | ||
await esbuild.build(input, save(output), externals); | ||
|
||
let dts = input.replace(/\.[mc]?[tj]s$/, '.d.ts'); | ||
if (!existsSync(dts)) return console.warn('Missing "%s" file!', dts),process.exitCode=1; | ||
|
||
let info = parse(input); | ||
info.base = 'index.d.ts'; | ||
info.dir = info.name; | ||
|
||
copyFileSync(dts, format(info)); | ||
copyFileSync(dts, save(format(info))); | ||
} | ||
|
||
/** | ||
* init | ||
*/ | ||
bundle('src/router.ts', pkg.exports['.']); | ||
bundle('src/cache.ts', pkg.exports['./cache']); | ||
bundle('src/request.ts', pkg.exports['./request']); | ||
bundle('src/response.ts', pkg.exports['./response']); | ||
bundle('src/utils.ts', pkg.exports['./utils']); | ||
bundle('src/kv.ts', pkg.exports['./kv']); | ||
Promise.all([ | ||
bundle('src/router.ts', pkg.exports['.']), | ||
bundle('src/cache.ts', pkg.exports['./cache']), | ||
bundle('src/request.ts', pkg.exports['./request']), | ||
bundle('src/response.ts', pkg.exports['./response']), | ||
bundle('src/utils.ts', pkg.exports['./utils']), | ||
bundle('src/kv.ts', pkg.exports['./kv']), | ||
]).then(table); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters