Skip to content

Commit

Permalink
chore(bin): add pretty table summary; (#13)
Browse files Browse the repository at this point in the history
- mostly lifted from `lukeed/bundt` and `lukeed/freshie`
  • Loading branch information
lukeed committed Mar 22, 2021
1 parent caeb11d commit 87bcf51
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 11 deletions.
1 change: 0 additions & 1 deletion bin/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const options = {
treeShaking: true,
minifySyntax: true,
minifyIdentifiers: true,
logLevel: 'info', // summary
}

/**
Expand Down
70 changes: 70 additions & 0 deletions bin/format.js
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');
}
21 changes: 11 additions & 10 deletions bin/index.js
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);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"devDependencies": {
"@types/node": "14.14.34",
"is-uuid": "1.0.2",
"kleur": "4.1.4",
"typescript": "4.2.3",
"uvu": "0.5.1"
},
Expand Down

0 comments on commit 87bcf51

Please sign in to comment.