Skip to content

Commit

Permalink
extract pure formatting function
Browse files Browse the repository at this point in the history
  • Loading branch information
ComradeVanti committed Aug 21, 2024
1 parent 9147877 commit 9fbe83f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 76 deletions.
78 changes: 4 additions & 74 deletions src/cli/cmd-view.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import assert from "assert";
import chalk from "chalk";
import { Logger } from "npmlog";
import { EOL } from "os";
import { PackumentNotFoundError } from "../common-errors";
import {
hasVersion,
PackageReference,
splitPackageReference,
} from "../domain/package-reference";
import { tryGetLatestVersion, UnityPackument } from "../domain/packument";
import { unityRegistry } from "../domain/registry";
import { GetRegistryPackument } from "../io/packument-io";
import { GetRegistryAuth } from "../services/get-registry-auth";
import { ParseEnv } from "../services/parse-env";
import { recordKeys } from "../utils/record-utils";
import { queryAllRegistriesLazy } from "../utils/sources";
import { CmdOptions } from "./options";
import { formatPackumentInfo } from "./output-formatting";
import { ResultCodes } from "./result-codes";

/**
Expand All @@ -37,75 +35,6 @@ export type ViewCmd = (
options: ViewOptions
) => Promise<ViewResultCode>;

const printInfo = function (packument: UnityPackument) {
const versionCount = recordKeys(packument.versions).length;
const ver = tryGetLatestVersion(packument);
assert(ver !== undefined);
const verInfo = packument.versions[ver]!;
const license = verInfo.license || "proprietary or unlicensed";
const displayName = verInfo.displayName;
const description = verInfo.description || packument.description;
const keywords = verInfo.keywords || packument.keywords;
const homepage = verInfo.homepage;
const dist = verInfo.dist;
const dependencies = verInfo.dependencies;
const latest = packument["dist-tags"]?.latest;
let time = packument.time?.modified;
if (
!time &&
latest &&
packument.time !== undefined &&
latest in packument.time
)
time = packument.time[latest];

console.log();
console.log(
chalk.greenBright(packument.name) +
"@" +
chalk.greenBright(ver) +
" | " +
chalk.green(license) +
" | versions: " +
chalk.yellow(versionCount)
);
console.log();
if (displayName) console.log(chalk.greenBright(displayName));
if (description) console.log(description);
if (description && description.includes("\n")) console.log();
if (homepage) console.log(chalk.cyan(homepage));
if (keywords) console.log(`keywords: ${keywords.join(", ")}`);

if (dist) {
console.log();
console.log("dist");
console.log(".tarball: " + chalk.cyan(dist.tarball));
if (dist.shasum) console.log(".shasum: " + chalk.yellow(dist.shasum));
if (dist.integrity)
console.log(".integrity: " + chalk.yellow(dist.integrity));
}

if (dependencies && recordKeys(dependencies).length > 0) {
console.log();
console.log("dependencies");
recordKeys(dependencies)
.sort()
.forEach((n) => console.log(chalk.yellow(n) + ` ${dependencies[n]}`));
}

console.log();
console.log("latest: " + chalk.greenBright(latest));

console.log();
console.log("published at " + chalk.yellow(time));

console.log();
console.log("versions:");
for (const version in packument.versions) {
console.log(" " + chalk.greenBright(version));
}
};

/**
* Makes a {@link ViewCmd} function.
*/
Expand Down Expand Up @@ -139,7 +68,8 @@ export function makeViewCmd(
const packument = packumentFromRegistry?.value ?? null;
if (packument === null) throw new PackumentNotFoundError(pkg);

printInfo(packument);
const output = formatPackumentInfo(packument, EOL);
log.notice("", output);
return ResultCodes.Ok;
};
}
90 changes: 89 additions & 1 deletion src/cli/output-formatting.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import assert from "assert";
import chalk from "chalk";
import Table from "cli-table";
import {
tryGetLatestVersion,
UnityPackument,
VersionedPackument,
} from "../domain/packument";
import assert from "assert";
import { recordKeys } from "../utils/record-utils";

/**
* A type describing the minimum required properties of a packument
Expand Down Expand Up @@ -45,3 +47,89 @@ export function formatAsTable(
rows.forEach((row) => table.push(row));
return table.toString();
}

/**
* Creates a string from a packument containing relevant information. This can
* be printed to the console.
*
* The string is multiline.
* @param packument The packument to format.
* @param eol The string to use to delimit lines, for example {@link import("os").EOL}.
* @returns The formatted string.
*/
export function formatPackumentInfo(
packument: UnityPackument,
eol: string
): string {
let output = "";

const versionCount = recordKeys(packument.versions).length;
const ver = tryGetLatestVersion(packument);
assert(ver !== undefined);
const verInfo = packument.versions[ver]!;
const license = verInfo.license || "proprietary or unlicensed";
const displayName = verInfo.displayName;
const description = verInfo.description || packument.description;
const keywords = verInfo.keywords || packument.keywords;
const homepage = verInfo.homepage;
const dist = verInfo.dist;
const dependencies = verInfo.dependencies;
const latest = packument["dist-tags"]?.latest;
let time = packument.time?.modified;
if (
!time &&
latest &&
packument.time !== undefined &&
latest in packument.time
)
time = packument.time[latest];

output += eol;
output += `${chalk.greenBright(packument.name)}@${chalk.greenBright(
ver
)} | ${chalk.green(license)} | versions: ${chalk.yellow(
versionCount
)}${eol}${eol}`;

if (displayName) output += chalk.greenBright(displayName) + eol;
if (description) output += description + eol;
if (description && description.includes("\n")) output += eol;
if (homepage) output += chalk.cyan(homepage) + eol;
if (keywords) output += `keywords: ${keywords.join(", ")}` + eol;

if (dist) {
output += eol;
output += `dist${eol}`;
output += `.tarball: ${chalk.cyan(dist.tarball)}${eol}`;
if (dist.shasum) output += `.shasum: ${chalk.yellow(dist.shasum)}${eol}`;
if (dist.integrity)
output += `.integrity: ${chalk.yellow(dist.integrity)}${eol}`;
}

if (dependencies && recordKeys(dependencies).length > 0) {
output += eol;
output += `dependencies${eol}`;
output += recordKeys(dependencies)
.sort()
.reduce(
(acc, dependencyName) =>
`${acc + chalk.yellow(dependencyName)} ${
dependencies[dependencyName]
}${eol}`,
""
);
}

output += eol;
output += `latest: ${chalk.greenBright(latest)}${eol}`;

output += eol;
output += `published at ${chalk.yellow(time)}${eol}`;

output += eol;
output += `versions:${eol}${Object.keys(packument.versions)
.map((version) => " " + chalk.greenBright(version))
.join(eol)}`;

return output;
}
4 changes: 3 additions & 1 deletion test/e2e/view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ describe("view packages", () => {
const output = await runOpenupm(homeDir, [
"view",
"dev.comradevanti.opt-unity",
// We need to disable color, otherwise chalk will mess with the output
"--no-color"
]);

expect(output.code).toEqual(ResultCodes.Ok);
expect(output.stdOut).toEqual(
expect(output.stdErr).toEqual(
expect.arrayContaining([
expect.stringContaining(
"dev.comradevanti.opt-unity@3.5.0 | Unlicense | versions: 10"
Expand Down

0 comments on commit 9fbe83f

Please sign in to comment.