Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prettify stat output #195

Merged
merged 1 commit into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions __tests__/integration/stat-output/expected-output.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
- snowpack installing...
✔ snowpack installed: cached-constructors-x, has-working-bind-x.
Direct dependencies: web_modules/
├─ cached-constructors-x.js []
└─ has-working-bind-x.js []
Shared dependencies: web_modules/common/
└─ noop-x.esm.js []

⦿ web_modules/
├─ cached-constructors-x.js []
└─ has-working-bind-x.js []
⦿ web_modules/common/ (Shared)
└─ noop-x.esm.js []
30 changes: 17 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,35 @@ function formatSize(size) {
function formatDelta(delta) {
const kb = Math.round(delta * 100) / 100;
const color = delta > 0 ? 'red' : 'green';
return `Δ ${chalk[color](`${delta > 0 ? '+' : ''}${kb}`)} KB`;
return chalk[color](`Δ ${delta > 0 ? '+' : ''}${kb} KB`);
}

function formatFileInfo(file, lastFile) {
const lineGlyph = lastFile ? '└─' : '├─';
const lineName = chalk.cyan(file.fileName);
const lineSize = formatSize(file.size);
const lineDelta = file.delta ? `, ${formatDelta(file.delta)}` : '';
return `${lineGlyph} ${lineName} [${lineSize}${lineDelta}]`;
function formatFileInfo(file, padEnd, isLastFile) {
const lineGlyph = chalk.dim(isLastFile ? '└─' : '├─');
const lineName = file.fileName.padEnd(padEnd);
const lineSize = chalk.dim('[') + formatSize(file.size) + chalk.dim(']');
const lineDelta = file.delta ? chalk.dim(' [') + formatDelta(file.delta) + chalk.dim(']') : '';
return ` ${lineGlyph} ${lineName} ${lineSize}${lineDelta}`;
}

function formatFiles(files, title) {
return `${title}
${files.map((file, index) => formatFileInfo(file, index >= files.length - 1)).join('\n')}`;
const maxFileNameLength = files.reduce((max, file) => Math.max(file.fileName.length, max), 0);
return ` ⦿ ${chalk.bold(title)}
${files
.map((file, index) => formatFileInfo(file, maxFileNameLength, index >= files.length - 1))
.join('\n')}`;
}

function formatDependencyStats(): string {
let output = '';
const {direct, common} = dependencyStats;

output += formatFiles(Object.values(direct), 'Direct dependencies: web_modules/');
const allDirect = Object.values(direct);
const allCommon = Object.values(common);
output += formatFiles(allDirect, 'web_modules/');
if (Object.values(common).length > 0) {
output += `\n${formatFiles(Object.values(common), 'Shared dependencies: web_modules/common/')}`;
output += `\n${formatFiles(allCommon, 'web_modules/common/ (Shared)')}`;
}
return output;
return `\n${output}\n`;
}

function logError(msg) {
Expand Down