Skip to content

Commit

Permalink
Fix plural CLI output, show version
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Nov 13, 2024
1 parent 1ea0f30 commit 8d06cc4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
15 changes: 11 additions & 4 deletions src/Importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from "node:path";
import fs from "graceful-fs";
import yaml from "js-yaml";
import kleur from "kleur";
import { createRequire } from "node:module";

import { Logger } from "./Logger.js";
import { Fetcher } from "./Fetcher.js";
Expand All @@ -18,6 +19,11 @@ import { WordPressApi } from "./DataSource/WordPressApi.js";
import { BlueskyUser } from "./DataSource/BlueskyUser.js";
import { FediverseUser } from "./DataSource/FediverseUser.js";


const require = createRequire(import.meta.url);

let pkg = require("../package.json");

// For testing
const MAX_IMPORT_SIZE = 0;

Expand Down Expand Up @@ -345,21 +351,22 @@ ${entry.content}`
logResults() {
let counts = this.getCounts();
let sourcesDisplay = this.getSources().map(source => source.constructor.TYPE_FRIENDLY || source.constructor.TYPE).join(", ");

let content = [];
content.push(kleur.green("Wrote"));
content.push(kleur.green(Logger.plural(counts.files, "document")));
content.push(kleur.green(`${counts.files} ${Logger.plural(counts.files, "document")}`));
content.push(kleur.green("and"));
content.push(kleur.green(Logger.plural(counts.assets - counts.cleaned, "asset")));
content.push(kleur.green(`${counts.assets - counts.cleaned} ${Logger.plural(counts.assets - counts.cleaned, "asset")}`));
if(counts.cleaned) {
content.push(kleur.gray(`(${counts.cleaned} cleaned, unused)`));
}
content.push(kleur.green(`from ${sourcesDisplay}`));
content.push(kleur[counts.errors > 0 ? "red" : "gray"](`(${Logger.plural(counts.errors, "error")})`));
content.push(kleur[counts.errors > 0 ? "red" : "gray"](`(${counts.errors} ${Logger.plural(counts.errors, "error")})`));
if(this.startTime) {
content.push(`in ${Logger.time(Date.now() - this.startTime)}`);
}

content.push(`(v${pkg.version})`);

Logger.log(content.join(" "));
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/Logger.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import kleur from "kleur";
import {filesize} from "filesize";
import { filesize } from "filesize";

class Logger {
static log(...messages) {
Expand Down Expand Up @@ -60,16 +60,17 @@ class Logger {

static time(ms) {
if(ms > 1000) {
return `${(ms/1000).toFixed(2)}s`;
let v = ms/1000;
return `${v.toFixed(2)} ${this.plural(v, "second")}`;
}
return `${ms}ms`;
return `${ms} ${this.plural(ms, "millisecond")}`;
}

static plural(num, singular, plural) {
if(!plural) {
plural = singular + "s";
}
return `${num} ${num !== 1 ? plural : singular}`;
return num !== 1 ? plural : singular;
}
}

Expand Down

0 comments on commit 8d06cc4

Please sign in to comment.