Skip to content

Commit

Permalink
feat(cli): add filesize logging
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Sep 22, 2023

Verified

This commit was signed with the committer’s verified signature. The key has expired.
addaleax Anna Henningsen
1 parent f94933b commit a9cf842
Showing 4 changed files with 41 additions and 12 deletions.
15 changes: 6 additions & 9 deletions packages/cli/src/lib/collect/command-object.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import {
collect,
CollectOptions,
CollectOutputError,
persistReport,
} from '@quality-metrics/utils';
import { CommandModule } from 'yargs';
import { pluginOutputSchema } from '@quality-metrics/models';
import {collect, CollectOptions, CollectOutputError, persistReport, logPersistedResults} from '@quality-metrics/utils';
import {CommandModule} from 'yargs';
import {pluginOutputSchema} from '@quality-metrics/models';

export function yargsCollectCommandObject() {
const handler = async (
config: CollectOptions & { format: string },
): Promise<void> => {
const report = await collect(config);

await persistReport(report, config);
const persistResults = await persistReport(report, config);

logPersistedResults(persistResults);

// validate report
report.plugins.forEach(plugin => {
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -15,4 +15,5 @@ export {
persistReport,
PersistDirError,
PersistError,
logPersistedResults
} from './lib/collect/implementation/persist';
23 changes: 20 additions & 3 deletions packages/utils/src/lib/collect/implementation/persist.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { existsSync, mkdirSync } from 'fs';
import { existsSync, mkdirSync, statSync } from 'fs';
import { writeFile } from 'fs/promises';
import { join } from 'path';
import { CoreConfig, Report } from '@quality-metrics/models';
import { reportToStdout } from './report-to-stdout';
import { reportToMd } from './report-to-md';
import {formatBytes} from "./utils";
import chalk from "chalk";

export class PersistDirError extends Error {
constructor(outputPath: string) {
@@ -17,7 +19,9 @@ export class PersistError extends Error {
}
}

export async function persistReport(report: Report, config: CoreConfig) {
export type PersistResult = PromiseSettledResult<readonly [string, number]>[];

export async function persistReport(report: Report, config: CoreConfig): Promise<PersistResult> {
const { persist } = config;
const outputPath = persist.outputPath;
let { format } = persist;
@@ -54,7 +58,10 @@ export async function persistReport(report: Report, config: CoreConfig) {
return (
writeFile(reportPath, content)
// return reportPath instead of void
.then(() => reportPath)
.then(() => {
const stats = statSync(reportPath)
return [reportPath, stats.size] as const;
})
.catch(e => {
console.warn(e);
throw new PersistError(reportPath);
@@ -63,3 +70,13 @@ export async function persistReport(report: Report, config: CoreConfig) {
}),
);
}

export function logPersistedResults(persistResult: PersistResult) {
console.log(`Generated reports successfully: `)
for(const result of persistResult) {
if(result.status === "fulfilled") {
const [fileName, size] = result.value
console.log(`- ${chalk.bold(fileName)} (${chalk.gray(formatBytes(size))})`)
}
}
}
14 changes: 14 additions & 0 deletions packages/utils/src/lib/collect/implementation/utils.ts
Original file line number Diff line number Diff line change
@@ -46,3 +46,17 @@ export async function readPackageJson() {
throw new ReadPackageJsonError(_e.message);
}
}


export function formatBytes(bytes: number, decimals = 2) {
if (!+bytes) return '0 Bytes'

const k = 1024
const dm = decimals < 0 ? 0 : decimals
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']

const i = Math.floor(Math.log(bytes) / Math.log(k))

return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`
}

0 comments on commit a9cf842

Please sign in to comment.