Skip to content

Commit

Permalink
chore: replace chalk with colorette
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rkr00t committed May 20, 2021
1 parent dc89a0e commit a86547b
Show file tree
Hide file tree
Showing 5 changed files with 480 additions and 404 deletions.
10 changes: 5 additions & 5 deletions lib/console/messages.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import chalk from "chalk";
import { bgRed, black, red, bold, blue } from "colorette";

export function log(msg: Array<string>) {
console.log(msg.join("\n"));
}

function redBadge(label: string) {
return chalk.bgRed.black(` ${label} `);
return bgRed(black(` ${label} `));
}

function errorBadge() {
Expand All @@ -14,13 +14,13 @@ function errorBadge() {

export function invalidStatsJson(file: string): Array<string> {
return [
chalk.red(
`${errorBadge()} Stats file ${chalk.bold(
red(
`${errorBadge()} Stats file ${bold(
`"${file}"`
)} doesn't contain enough information...`
),
"",
`Issue is possibly with missing "reasons" in webpack module stats. To add them check webpack documentation: ${chalk.blue(
`Issue is possibly with missing "reasons" in webpack module stats. To add them check webpack documentation: ${blue(
"https://webpack.js.org/configuration/stats/#stats"
)}`,
];
Expand Down
14 changes: 6 additions & 8 deletions lib/console/progress-bar.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { bgWhite } from "colorette";

export type UpdateProgressBar = (param: {
progress: number;
title?: string;
text?: string;
}) => void;

const chalk = require("chalk");
const { dim, green, magenta } = require("colorette");

function toStartOfLine(stdout: NodeJS.WriteStream) {
stdout.write("\r");
Expand Down Expand Up @@ -37,9 +39,7 @@ export function createProgressBar(stdout = process.stdout): UpdateProgressBar {
title.length
);
const completeLength = Math.round(progressBarSize * ratio);
const incompleteLength = progressBarSize - completeLength;

const completed = chalk.inverse(" ").repeat(completeLength);
const completed = bgWhite(" ").repeat(completeLength);
const incompleted = "░".repeat(progressBarSize - completeLength);
const infoLine =
text.length > availableSpace
Expand All @@ -50,10 +50,8 @@ export function createProgressBar(stdout = process.stdout): UpdateProgressBar {
toStartOfLine(stdout);

stdout.write(
`⸨${completed}${incompleted}${chalk.dim(
`${progress}%`
)}${chalk.green("info")} ${
title ? chalk.magenta(`${title}: `) : ""
`⸨${completed}${incompleted}${dim(`${progress}%`)}${green("info")} ${
title ? magenta(`${title}: `) : ""
}${infoLine}`
);

Expand Down
95 changes: 53 additions & 42 deletions lib/reporter/print.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import chalk from "chalk";
import {
bgGreen,
bgYellow,
bgRed,
bgCyan,
magenta,
green,
red,
dim,
black,
yellow,
} from "colorette";
import treeify from "treeify";

import type { Module, Reason, SubReason, Chunks } from "../analyze";

const greenBadge = (label: string) => chalk.bgGreen.black(` ${label} `);
const yellowBadge = (label: string) => chalk.bgYellow.black(` ${label} `);
const redBadge = (label: string) => chalk.bgRed.black(` ${label} `);
const cyanBadge = (label: string) => chalk.bgCyan.black(` ${label} `);
const greenBadge = (label: string) => bgGreen(black(` ${label} `));
const yellowBadge = (label: string) => bgYellow(black(` ${label} `));
const redBadge = (label: string) => bgRed(black(` ${label} `));
const cyanBadge = (label: string) => bgCyan(black(` ${label} `));
const moduleBadge = () => yellowBadge("MODULE");
const fileBadge = () => greenBadge("FILE");
const entryPointBadge = () => chalk.magenta("[entry]");
const directBadge = () => chalk.green("[direct]");
const transitiveBadge = () => chalk.red("[transitive]");
const entryPointBadge = () => magenta("[entry]");
const directBadge = () => green("[direct]");
const transitiveBadge = () => red("[transitive]");

const sep = () => chalk.dim("––––––––––––––––––––");
const sep = () => dim("––––––––––––––––––––");

const isEntry = (reasons: Array<Reason>) =>
reasons.length === 1 && reasons[0].type === "entry";
Expand Down Expand Up @@ -63,15 +74,15 @@ const printReasons = (
const printReasonName = (name: string, by?: string) =>
by === name ? cyanBadge(` ${name} `) : name;
const printReasonNameModule = (name: string, by?: string) =>
by === name ? cyanBadge(` ${name} `) : chalk.yellow(name);
by === name ? cyanBadge(` ${name} `) : yellow(name);

const tree = reasonsSubSet.reduce<treeify.TreeObject>(
(acc, reason: Reason) => {
if (reason.type === "file") {
acc[
`${printReasonName(reason.moduleName, by)} ${chalk.dim(
reason.loc
)} ${chalk.dim("[" + reason.importType + "]")}`
`${printReasonName(reason.moduleName, by)} ${dim(reason.loc)} ${dim(
"[" + reason.importType + "]"
)}`
] = "null";
} else if (reason.type === "module") {
const subReasonsSubset = takeSubset(
Expand All @@ -81,18 +92,17 @@ const printReasons = (
const subReasons = subReasonsSubset.reduce<treeify.TreeObject>(
(acc, r: SubReason) => {
acc[
`${printReasonName(r.moduleName, by)} ${chalk.dim(
r.loc
)} ${chalk.dim("[" + r.importType + "]")}`
`${printReasonName(r.moduleName, by)} ${dim(r.loc)} ${dim(
"[" + r.importType + "]"
)}`
] = "null";
return acc;
},
{}
);

if (subReasonsSubset.length < (reason.reasons || []).length) {
subReasons[chalk.dim(`... ${reason.reasons.length - limit} more`)] =
"null";
subReasons[dim(`... ${reason.reasons.length - limit} more`)] = "null";
}

acc[
Expand All @@ -106,7 +116,7 @@ const printReasons = (
);

if (reasonsSubSet.length < reasons.length) {
tree[chalk.dim(`... ${reasons.length - limit} more`)] = "null";
tree[dim(`... ${reasons.length - limit} more`)] = "null";
}

return printTree(tree);
Expand All @@ -128,15 +138,15 @@ const printIncludedFiles = (files: Array<string>, limit: number) => {
}, {});

if (filesSubset.length < files.length) {
tree[chalk.dim(`... ${files.length - limit} more`)] = "null";
tree[dim(`... ${files.length - limit} more`)] = "null";
}

return printTree(tree);
};

const printType = (module: Module, limit: number, by?: string) => {
const type = [
`├─ ${chalk.magenta("type")}: ${
`├─ ${magenta("type")}: ${
module.depsType === "direct" ? directBadge() : transitiveBadge()
}`,
];
Expand All @@ -146,18 +156,17 @@ const printType = (module: Module, limit: number, by?: string) => {
const depsChains = depsChainsSubset.reduce<treeify.TreeObject>(
(acc, chain) => {
acc[
`${module.name} ${chalk.dim("->")} ${chain
`${module.name} ${dim("->")} ${chain
.map((dep) => (dep === by ? cyanBadge(` ${dep} `) : dep))
.join(chalk.dim(" -> "))}`
.join(dim(" -> "))}`
] = "null";
return acc;
},
{}
);

if (depsChainsSubset.length < module.depsChains.length) {
depsChains[chalk.dim(`... ${module.depsChains.length - limit} more`)] =
"null";
depsChains[dim(`... ${module.depsChains.length - limit} more`)] = "null";
}

type.push(indent(printTree(depsChains)).join("\n"));
Expand All @@ -176,14 +185,18 @@ const printSize = (size: number, mod = false) => {
: sizeInKiB < 20 && sizeInKiB > 10
? "yellow"
: "default";
const levelColor = {
red: red,
yellow: yellow,
};
const sizeFormatted =
level === "unknown"
? chalk.dim("unknown")
? dim("unknown")
: level === "default"
? sizeInKiB + " KiB"
: chalk[level](sizeInKiB + " KiB");
return `${chalk.magenta("size")}: ${sizeFormatted}${
mod ? chalk.dim(" [for all included files]") : ""
: levelColor[level](sizeInKiB + " KiB");
return `${magenta("size")}: ${sizeFormatted}${
mod ? dim(" [for all included files]") : ""
}`;
};

Expand All @@ -208,17 +221,15 @@ const printFile = (
const chunksInfo = printChunkInfo(module, chunks);
const entry = isEntry(module.reasons);
return [
`${fileBadge()} ${chalk.green(module.name)}${
`${fileBadge()} ${green(module.name)}${
entry ? " " + entryPointBadge() : ""
}`,
`├─ ${chalk.magenta("imported")}: ${printImportedCount(module.imported)}`,
`├─ ${magenta("imported")}: ${printImportedCount(module.imported)}`,
`├─ ${printSize(module.size)}`,
chunksInfo &&
chunksInfo.length &&
`${entry ? "└─" : "├─"} ${chalk.magenta("chunks")}: ${chunksInfo.join(
", "
)}`,
!entry && `└─ ${chalk.magenta("reasons")}:`,
`${entry ? "└─" : "├─"} ${magenta("chunks")}: ${chunksInfo.join(", ")}`,
!entry && `└─ ${magenta("reasons")}:`,
!entry && indent(printReasons(module.reasons, limit, by), " ").join("\n"),
].filter((msg) => !!msg);
};
Expand All @@ -232,23 +243,23 @@ const printModule = (
const chunksInfo = printChunkInfo(module, chunks);
const entry = isEntry(module.reasons);
return [
`${moduleBadge()} ${chalk.yellow(module.name)}${
`${moduleBadge()} ${yellow(module.name)}${
entry ? " " + entryPointBadge() : ""
}`,
`├─ ${chalk.magenta("imported")}: ${printImportedCount(module.imported)}`,
`├─ ${chalk.magenta("deps count")}: ${module.deps}`,
`├─ ${magenta("imported")}: ${printImportedCount(module.imported)}`,
`├─ ${magenta("deps count")}: ${module.deps}`,
`├─ ${printSize(module.size, true)}`,
printType(module, limit, by).join("\n"),
chunksInfo &&
chunksInfo.length &&
`├─ ${chalk.magenta("chunks")}: ${chunksInfo.join(", ")}`,
`├─ ${chalk.magenta("locations")}: ${
`├─ ${magenta("chunks")}: ${chunksInfo.join(", ")}`,
`├─ ${magenta("locations")}: ${
module.locations.length > 1 ? redBadge("multiple") : ""
}`,
indent(printLocations(module.locations)).join("\n"),
`${entry ? "└─" : "├─"} ${chalk.magenta("files")}: `,
`${entry ? "└─" : "├─"} ${magenta("files")}: `,
indent(printIncludedFiles(module.filesIncluded, limit)).join("\n"),
!entry && `└─ ${chalk.magenta("reasons")}:`,
!entry && `└─ ${magenta("reasons")}:`,
!entry && indent(printReasons(module.reasons, limit, by), " ").join("\n"),
].filter((msg) => !!msg);
};
Expand Down
Loading

0 comments on commit a86547b

Please sign in to comment.