Skip to content

Commit

Permalink
feat: add sorting to default command
Browse files Browse the repository at this point in the history
Closes: #24
  • Loading branch information
d4rkr00t committed Mar 29, 2021
1 parent 7a73580 commit 2b0c993
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 190 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ OPTIONS
--transitiveOnly Only include transitive dependencies [boolean]
--duplicatesOnly Only include modules that have duplicates in a resulting bundle [boolean]
--ignore Comma separated list of glob patterns to exclude modules from final output [string]
--sortBy Sort modules, available fields: size, imported. E.g. size:asc or size:desc. [string]
--help Output usage information
--version Output the version number

Expand Down
2 changes: 2 additions & 0 deletions commands/by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { analyze, getStats } from "../lib";
import { vlidateStatsJson } from "../lib/validate";
import { log, invalidStatsJson } from "../lib/console/messages";
import { normalizeStats } from "../lib/normalize-stats";
import { sortDefault } from "./common/sort-modules";

/**
* Shows all modules that were brought into the bundle by a particular module.
Expand Down Expand Up @@ -43,6 +44,7 @@ export default async function byCommand(
? modulesOnlyBy(report.modules, by)
: modulesFollowingDepsChain(report.modules, by);

sortDefault(modules);
defaultReporter.print(modules, report.chunks, { by: by }, limit);

const timing = (Date.now() - start) / 1000;
Expand Down
37 changes: 37 additions & 0 deletions commands/common/sort-modules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { Module } from "../../lib/analyze";

export function sortModules(
modules: Array<Module>,
maybeKey: string,
order: string = "asc"
) {
const allowedKeys = new Set(["size", "imported"]);
if (!allowedKeys.has(maybeKey)) {
return sortDefault(modules);
}

const key = maybeKey as keyof Module;

switch (order) {
case "asc":
return sortAscByKey(modules, key);

case "desc":
return sortDescByKey(modules, key);

default:
return sortDefault(modules);
}
}

function sortAscByKey(arr: Array<Module>, key: keyof Module) {
arr.sort((a, b) => (a[key] > b[key] ? 1 : a[key] === b[key] ? 0 : -1));
}

function sortDescByKey(arr: Array<Module>, key: keyof Module) {
arr.sort((a, b) => (a[key] > b[key] ? -1 : a[key] === b[key] ? 0 : 1));
}

export function sortDefault(arr: Array<Module>) {
arr.sort((a, b) => b.imported - a.imported);
}
10 changes: 9 additions & 1 deletion commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { log, invalidStatsJson } from "../lib/console/messages";
import { normalizeStats } from "../lib/normalize-stats";
import { reporter as defaultReporter } from "../lib/reporter";
import { createProgressBar } from "../lib/console/progress-bar";
import type { Module } from "../lib/analyze";
import { sortModules } from "./common/sort-modules";

/**
* Whybundled – Why the hell is this module in a bundle?
Expand All @@ -17,10 +19,12 @@ import { createProgressBar } from "../lib/console/progress-bar";
* @param {boolean} transitiveOnly Only include transitive dependencies
* @param {boolean} duplicatesOnly Only include modules that have duplicates in a resulting bundle
* @param {string} ignore Comma separated list of glob patterns to exclude modules from final output
* @param {string} sortBy Sort modules, available fields: size, imported. E.g. size:asc or size:desc.
*
* @usage {cliName} stats.json [pattern]
* @example whybundled stats.json --ignore babel-runtime,tslib
* @example whybundled stats.json --modulesOnly
* @example whybundled stats.json --sortBy size:asc
* @example whybundled by stats.json styled-components
*/
export default async function defaultCommand(
Expand All @@ -31,7 +35,8 @@ export default async function defaultCommand(
directOnly?: boolean,
transitiveOnly?: boolean,
duplicatesOnly?: boolean,
ignore?: string
ignore?: string,
sortBy?: string
) {
const start = Date.now();
const [statsFilePath, pattern] = $inputs;
Expand Down Expand Up @@ -67,6 +72,9 @@ export default async function defaultCommand(
});

const updatedLimit = pattern ? 0 : limit >= 0 ? limit : 20;
const [sortKey, sortOrder] = (sortBy || "").split(":");
sortModules(modules, sortKey, sortOrder);

defaultReporter.print(modules, report.chunks, {}, updatedLimit);

const timing = (Date.now() - start) / 1000;
Expand Down
Loading

0 comments on commit 2b0c993

Please sign in to comment.