forked from JS-DevTools/npm-publish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat-publish-result.ts
40 lines (35 loc) · 1.08 KB
/
format-publish-result.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import os from "node:os";
import type { PublishResult } from "./compare-and-publish/index.js";
import type { PackageManifest } from "./read-manifest.js";
import type { NormalizedOptions } from "./normalize-options.js";
/**
* Format publish results into a string.
*
* @param manifest Package manifest
* @param options Configuration options.
* @param result Results from running npm publish.
* @returns Formatted string.
*/
export function formatPublishResult(
manifest: PackageManifest,
options: NormalizedOptions,
result: PublishResult
): string {
if (result.id === undefined) {
return `🙅♀️ ${manifest.name}@${manifest.version} publish skipped.`;
}
return [
`📦 ${result.id}${options.dryRun.value ? " (DRY RUN)" : ""}`,
"=== Contents ===",
...result.files.map(({ path, size }) => `${formatSize(size)}\t${path}`),
].join(os.EOL);
}
const formatSize = (size: number): string => {
if (size < 1000) {
return `${size} B`;
}
if (size < 1_000_000) {
return `${(size / 1000).toFixed(1)} kB`;
}
return `${(size / 1_000_000).toFixed(1)} MB`;
};