-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
261 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"chunks": [ | ||
{ | ||
"modules": [ | ||
{ | ||
"id": "module1", | ||
"name": "module-name", | ||
"size": 500, | ||
"reasons": [] | ||
}, | ||
{ | ||
"id": "module2", | ||
"name": "module-name2", | ||
"size": 300, | ||
"reasons": [] | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const test = require("ava"); | ||
const fixtures = require("fixturez"); | ||
const f = fixtures(__dirname); | ||
const getStats = require("../get-stats"); | ||
const analyze = require("../analyze"); | ||
|
||
test("should call updateProgressBar correct number of times", t => { | ||
let calls = 0; | ||
const updateProgressBar = ({ progress }) => { | ||
calls++; | ||
}; | ||
const stats = analyze( | ||
getStats(f.find("valid-with-multiple-modules.json")), | ||
[], | ||
updateProgressBar | ||
); | ||
t.is(calls, 2); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Snapshot report for `lib/console/__tests__/progress-bar.js` | ||
|
||
The actual snapshot is saved in `progress-bar.js.snap`. | ||
|
||
Generated by [AVA](https://ava.li). | ||
|
||
## should output correct progress for {"progress":0} | ||
|
||
> Snapshot 1 | ||
[ | ||
`␍ | ||
`, | ||
`␍ | ||
`, | ||
'⸨░░░░░░░░░░░░░░░░░░░░⸩ 0% • info ', | ||
] | ||
|
||
## should output correct progress for {"progress":100} | ||
|
||
> Snapshot 1 | ||
[ | ||
`␍ | ||
`, | ||
`␍ | ||
`, | ||
'⸨ ⸩ 100% • info ', | ||
`␍ | ||
`, | ||
`␍ | ||
`, | ||
] | ||
|
||
## should output correct progress for {"progress":30,"title":"title","text":"text"} | ||
|
||
> Snapshot 1 | ||
[ | ||
`␍ | ||
`, | ||
`␍ | ||
`, | ||
'⸨ ░░░░░░░░░░░░░░⸩ 30% • info title: text', | ||
] | ||
|
||
## should output correct progress for {"progress":50,"text":"text"} | ||
|
||
> Snapshot 1 | ||
[ | ||
`␍ | ||
`, | ||
`␍ | ||
`, | ||
'⸨ ░░░░░░░░░░⸩ 50% • info text', | ||
] | ||
|
||
## should output correct progress for {"progress":75,"title":"title"} | ||
|
||
> Snapshot 1 | ||
[ | ||
`␍ | ||
`, | ||
`␍ | ||
`, | ||
'⸨ ░░░░░⸩ 75% • info title: ', | ||
] |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const stripAnsi = require("strip-ansi"); | ||
const test = require("ava"); | ||
const fixtures = require("fixturez"); | ||
const f = fixtures(__dirname); | ||
const createProgressBar = require("../progress-bar"); | ||
|
||
[ | ||
{ progress: 0 }, | ||
{ progress: 30, title: "title", text: "text" }, | ||
{ progress: 50, text: "text" }, | ||
{ progress: 75, title: "title" }, | ||
{ progress: 100 } | ||
].forEach(testCase => { | ||
test(`should output correct progress for ${JSON.stringify(testCase)}`, t => { | ||
const writes = []; | ||
const stdout = { | ||
columns: 100, | ||
write(str) { | ||
writes.push(stripAnsi(str || "")); | ||
} | ||
}; | ||
const updateProgressBar = createProgressBar(stdout); | ||
updateProgressBar(testCase); | ||
t.snapshot(writes); | ||
}); | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* @flow */ | ||
|
||
/*:: | ||
export type UpdateProgressBar = (param: { progress: number, title?: string, text?: string }) => void; | ||
*/ | ||
|
||
const chalk = require("chalk"); | ||
|
||
function toStartOfLine(stdout /*: stream$Writable */) { | ||
stdout.write("\r"); | ||
} | ||
|
||
function clearLine(stdout /*: stream$Writable */) { | ||
// $FlowFixMe: investigate process.stderr.columns flow error | ||
stdout.write(`\r${" ".repeat(stdout.columns - 1)}`); | ||
} | ||
|
||
function selectPrevLine(stdout /*: stream$Writable */) { | ||
stdout.write("\033[1A"); | ||
} | ||
|
||
module.exports = function createProgressBar( | ||
stdout /*: stream$Writable */ = process.stdout | ||
) /*: UpdateProgressBar */ { | ||
return function updateProgress({ | ||
progress, | ||
title: maybeTitle, | ||
text: maybeText | ||
}) { | ||
// $FlowFixMe: investigate process.stderr.columns flow error | ||
const columns /*: number */ = stdout.columns; | ||
const ratio = progress / 100; | ||
const title = maybeTitle || ""; | ||
const text = maybeText || ""; | ||
|
||
const decorationsSize = "⸨⸩ • info: % …".length; | ||
const progressNumberSize = ("" + progress).length; | ||
const progressBarSize = 20; | ||
const availableSpace = Math.max( | ||
0, | ||
columns - | ||
decorationsSize - | ||
progressNumberSize - | ||
progressNumberSize - | ||
title.length | ||
); | ||
const completeLength = Math.round(progressBarSize * ratio); | ||
const incompleteLength = progressBarSize - completeLength; | ||
|
||
const completed = chalk.inverse(" ").repeat(completeLength); | ||
const incompleted = "░".repeat(progressBarSize - completeLength); | ||
const infoLine = | ||
text.length > availableSpace | ||
? text.substr(0, availableSpace) + "…" | ||
: text; | ||
|
||
clearLine(stdout); | ||
toStartOfLine(stdout); | ||
|
||
stdout.write( | ||
`⸨${completed}${incompleted}⸩ ${chalk.dim( | ||
`${progress}%` | ||
)} • ${chalk.green("info")} ${ | ||
title ? chalk.magenta(`${title}: `) : "" | ||
}${infoLine}` | ||
); | ||
|
||
if (progress === 100) { | ||
toStartOfLine(stdout); | ||
clearLine(stdout); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters