Skip to content

Commit

Permalink
feat: Add --only flag to a --by command
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rkr00t committed Jun 21, 2018
1 parent 952cbbd commit a3e5d33
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 148 deletions.
1 change: 1 addition & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const byCommand = require("./commands/by");
const helpCommand = require("./commands/help");
const knownFlags = [
"by",
"only",
"modulesOnly",
"filesOnly",
"directOnly",
Expand Down
40 changes: 32 additions & 8 deletions commands/by.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,43 @@

/*::
import type { UpdateProgressBar } from '../lib/console/progress-bar';
import type { Reason, Module } from '../lib/analyze';
*/

const { analyze, print, getStats } = require("../lib");
const validate = require("../lib/validate");
const { log, invalidStatsJson } = require("../lib/console/messages");

const isDepsChainBy = (depsChain /*: Array<string> */, by /*: string */) => {
return depsChain.indexOf(by) !== -1;
};

const modulesFollowingDepsChain = (
modules /*: Array<Module> */,
by /*:string*/
) =>
modules.filter(
mod =>
mod.reasons.some(
reason => reason.moduleName === by || reason.clearName === by
) || (mod.depsChains || []).some(deps => isDepsChainBy(deps, by))
);

const isSingleReasonBy = (reasons /*: Array<Reason> */, by /*: string */) =>
reasons.length === 1 &&
(reasons[0].moduleName === by || reasons[0].clearName === by);

const modulesOnlyBy = (modules /*: Array<Module> */, by /*: string */) =>
modulesFollowingDepsChain(modules, by).filter(
mod =>
isSingleReasonBy(mod.reasons, by) ||
((mod.depsChains || []).length &&
(mod.depsChains || []).every(depsChain => isDepsChainBy(depsChain, by)))
);

module.exports = function byCommand(
statsFilePath /*: string */,
flags /*: { limit: number, by: string, ignore?: string } */,
flags /*: { limit: number, by: string, only?: boolean, ignore?: string } */,
pattern /*: string */,
updateProgressBar /*: UpdateProgressBar */ = () => {}
) {
Expand All @@ -24,13 +52,9 @@ module.exports = function byCommand(
const ignore = flags.ignore ? flags.ignore.split(",") : [];
const report = analyze(stats, ignore, updateProgressBar);

const modules = report.modules.filter(
mod =>
mod.reasons.some(
reason =>
reason.moduleName === flags.by || reason.clearName === flags.by
) || (mod.depsChains || []).some(deps => deps.indexOf(flags.by) !== -1)
);
const modules = flags.only
? modulesOnlyBy(report.modules, flags.by)
: modulesFollowingDepsChain(report.modules, flags.by);

const limit /*: number */ = pattern ? 0 : flags.limit >= 0 ? flags.limit : 20;
print(modules, report.chunks, { by: flags.by }, limit);
Expand Down
1 change: 1 addition & 0 deletions commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = function help() {
chalk.green("By options [--by]:"),
` ${chalk.yellow("--ignore")} Comma separated list of glob pattern to exclude modules from final output`,
` ${chalk.yellow("--limit")} Limits output of reasons and files [default: 20]`,
` ${chalk.yellow("--only")} Limits output to only include modules that were included by specified module exclusively`,
``,
chalk.green("Other options:"),
` ${chalk.yellow("-v, --version")} Shows version.`,
Expand Down
227 changes: 130 additions & 97 deletions fixtures/nested-children-stats2.json

Large diffs are not rendered by default.

Loading

0 comments on commit a3e5d33

Please sign in to comment.