Skip to content

Commit

Permalink
feat: Custom reporters
Browse files Browse the repository at this point in the history
Closes: #19
  • Loading branch information
d4rkr00t committed Jul 26, 2018
1 parent 8bed71b commit 8627b2f
Show file tree
Hide file tree
Showing 12 changed files with 1,656 additions and 19 deletions.
20 changes: 17 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env node
/* @flow */

const path = require("path");
const meow = require("meow");
const chalk = require("chalk");
const createProgressBar = require("./lib/console/progress-bar");
const defaultReporter = require("./lib/reporter");
const defaultCommand = require("./commands/default");
const byCommand = require("./commands/by");
const helpCommand = require("./commands/help");
Expand All @@ -18,7 +20,8 @@ const knownFlags = [
"limit",
"version",
"help",
"ignore"
"ignore",
"reporter"
];

const validateFlags = flags => {
Expand Down Expand Up @@ -56,10 +59,21 @@ if (!input || !input.length || !input[0].match(".json") || flags.help) {

const updateProgressBar = createProgressBar();

let reporter = defaultReporter;
if (flags.reporter) {
try {
reporter = {
...defaultReporter,
// $FlowFixMe
...require(path.resolve(__dirname, flags.reporter))
};
} catch (e) {}
}

if (flags.by) {
byCommand(input[0], flags, input[1], updateProgressBar);
byCommand(input[0], flags, input[1], reporter, updateProgressBar);
} else {
defaultCommand(input[0], flags, input[1], updateProgressBar);
defaultCommand(input[0], flags, input[1], reporter, updateProgressBar);
}

const timing = (Date.now() - start) / 1000;
Expand Down
6 changes: 4 additions & 2 deletions commands/by.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
/*::
import type { UpdateProgressBar } from '../lib/console/progress-bar';
import type { Reason, Module } from '../lib/analyze';
import type { Reporter } from '../lib/reporter';
*/

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

Expand Down Expand Up @@ -40,6 +41,7 @@ module.exports = function byCommand(
statsFilePath /*: string */,
flags /*: { limit: number, by: string, only?: boolean, ignore?: string } */,
pattern /*: string */,
reporter /*: Reporter */,
updateProgressBar /*: UpdateProgressBar */ = () => {}
) {
const stats = getStats(statsFilePath);
Expand All @@ -57,5 +59,5 @@ module.exports = function byCommand(
: modulesFollowingDepsChain(report.modules, flags.by);

const limit /*: number */ = pattern ? 0 : flags.limit >= 0 ? flags.limit : 20;
print(modules, report.chunks, { by: flags.by }, limit);
reporter.print(modules, report.chunks, { by: flags.by }, limit);
};
6 changes: 4 additions & 2 deletions commands/default.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* @flow */

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

/*::
import type { UpdateProgressBar } from '../lib/console/progress-bar';
import type { Reporter } from '../lib/reporter';
type Flags = {
limit: number,
Expand All @@ -24,6 +25,7 @@ module.exports = function defaultCommand(
statsFilePath /*: string */,
flags /*: Flags */,
pattern /*: string*/,
reporter /*: Reporter */,
updateProgressBar /*: UpdateProgressBar */ = () => {}
) {
const stats = getStats(statsFilePath);
Expand Down Expand Up @@ -54,5 +56,5 @@ module.exports = function defaultCommand(
});

const limit = pattern ? 0 : flags.limit >= 0 ? flags.limit : 20;
print(modules, report.chunks, flags, limit);
reporter.print(modules, report.chunks, flags, limit);
};
12 changes: 6 additions & 6 deletions lib/console/__tests__/__snapshots__/progress-bar.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Generated by [AVA](https://ava.li).
[
`␍
`,
`,
`␍
`,
'⸨░░░░░░░░░░░░░░░░░░░░⸩ 0% • info ',
Expand All @@ -22,14 +22,14 @@ Generated by [AVA](https://ava.li).
[
`␍
`,
`,
`␍
`,
'⸨ ⸩ 100% • info ',
`␍
`,
`␍
`,
`,
]

## should output correct progress for {"progress":30,"title":"title","text":"text"}
Expand All @@ -38,7 +38,7 @@ Generated by [AVA](https://ava.li).
[
`␍
`,
`,
`␍
`,
'⸨ ░░░░░░░░░░░░░░⸩ 30% • info title: text',
Expand All @@ -50,7 +50,7 @@ Generated by [AVA](https://ava.li).
[
`␍
`,
`,
`␍
`,
'⸨ ░░░░░░░░░░⸩ 50% • info text',
Expand All @@ -62,7 +62,7 @@ Generated by [AVA](https://ava.li).
[
`␍
`,
`,
`␍
`,
'⸨ ░░░░░⸩ 75% • info title: ',
Expand Down
Binary file modified lib/console/__tests__/__snapshots__/progress-bar.js.snap
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/console/progress-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function toStartOfLine(stdout /*: stream$Writable */) {

function clearLine(stdout /*: stream$Writable */) {
// $FlowFixMe: investigate process.stderr.columns flow error
stdout.write(`\r${" ".repeat(stdout.columns - 1)}`);
stdout.write(`\r${" ".repeat(stdout.columns)}`);
}

function selectPrevLine(stdout /*: stream$Writable */) {
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow */

const analyze = require("./analyze");
const print = require("./print");
const reporter = require("./reporter");
const getStats = require("./get-stats");

module.exports = { analyze, print, getStats };
module.exports = { analyze, reporter, getStats };
Loading

0 comments on commit 8627b2f

Please sign in to comment.