-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic reporter for non-TTY environments like CI
- Loading branch information
Showing
6 changed files
with
420 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
import ansiEscapes from "ansi-escapes"; | ||
import chalk from "chalk"; | ||
import unicons from "unicons"; | ||
import { | ||
filterSuccessfulUpdates, | ||
filterFailedUpdates, | ||
} from "../tasks/util/filterUpdateResults"; | ||
import Message from "./util/Message"; | ||
import Indicator, { | ||
INDICATOR_NEUTRAL, | ||
INDICATOR_PENDING, | ||
INDICATOR_OK, | ||
INDICATOR_FAIL, | ||
} from "./util/Indicator"; | ||
import customConfigToLines from "./util/customConfigToLines"; | ||
import pluralize from "./util/pluralize"; | ||
import handleError from "./util/handleError"; | ||
import msToString from "./util/msToString"; | ||
|
||
function updatingLine(updateTask) { | ||
return [ | ||
new Indicator(INDICATOR_PENDING), | ||
chalk.bold(updateTask.name), | ||
chalk.grey("updating"), | ||
updateTask.rollbackTo, | ||
chalk.grey(unicons.arrowRight), | ||
updateTask.updateTo + chalk.grey("..."), | ||
].join(" "); | ||
} | ||
|
||
function testingLine(updateTask) { | ||
return [ | ||
new Indicator(INDICATOR_PENDING), | ||
chalk.bold(updateTask.name), | ||
chalk.grey("testing..."), | ||
].join(" "); | ||
} | ||
|
||
function rollbackLine(updateTask) { | ||
return [ | ||
new Indicator(INDICATOR_FAIL), | ||
chalk.bold.red(updateTask.name), | ||
chalk.grey("rolling back"), | ||
updateTask.updateTo, | ||
chalk.grey(unicons.arrowRight), | ||
updateTask.rollbackTo + chalk.grey("..."), | ||
].join(" "); | ||
} | ||
|
||
function successLine(updateTask) { | ||
return [ | ||
new Indicator(INDICATOR_OK), | ||
chalk.bold(updateTask.name), | ||
updateTask.updateTo, | ||
chalk.grey("success"), | ||
].join(" "); | ||
} | ||
|
||
function failLine(updateTask) { | ||
return [ | ||
new Indicator(INDICATOR_FAIL), | ||
chalk.bold.red(updateTask.name), | ||
updateTask.updateTo, | ||
chalk.grey("failed"), | ||
].join(" "); | ||
} | ||
|
||
function excludedLine(excluded) { | ||
return [ | ||
new Indicator(INDICATOR_NEUTRAL), | ||
chalk.bold(excluded.name), | ||
chalk.grey(excluded.reason), | ||
].join(" "); | ||
} | ||
|
||
function cmdToLines(description, cmd) { | ||
const lines = Array.isArray(description) === true ? | ||
description : | ||
[description]; | ||
|
||
return lines.concat([chalk.grey(`> ${cmd} `)]); | ||
} | ||
|
||
function writeLinesToConsole(lines) { | ||
if (lines.length === 0) { | ||
return; | ||
} | ||
console.log(ansiEscapes.eraseDown + lines.join("\n")); | ||
} | ||
|
||
export default function basic(updtr, reporterConfig) { | ||
const startTime = Date.now(); | ||
let excludedModules; | ||
|
||
updtr.on("start", ({config}) => { | ||
writeLinesToConsole(customConfigToLines(config)); | ||
}); | ||
updtr.on("init/install-missing", ({cmd}) => { | ||
writeLinesToConsole( | ||
cmdToLines( | ||
"Installing missing dependencies" + chalk.grey("..."), | ||
cmd | ||
) | ||
); | ||
}); | ||
updtr.on("init/collect", ({cmd}) => { | ||
writeLinesToConsole( | ||
cmdToLines("Looking for outdated modules" + chalk.grey("..."), cmd) | ||
); | ||
}); | ||
updtr.on("init/end", ({updateTasks, excluded}) => { | ||
excludedModules = excluded; | ||
if (updateTasks.length === 0 && excluded.length === 0) { | ||
writeLinesToConsole(["Everything " + chalk.bold("up-to-date")]); | ||
} else if (updateTasks.length === 0) { | ||
writeLinesToConsole([ | ||
chalk.bold("No updates available") + | ||
" for the given modules and version range", | ||
]); | ||
} else { | ||
writeLinesToConsole([ | ||
new Message("Found " + chalk.bold("%s update%s") + ".", [ | ||
updateTasks.length, | ||
pluralize(updateTasks.length), | ||
]), | ||
"", | ||
]); | ||
} | ||
}); | ||
updtr.on("batch-update/updating", event => { | ||
writeLinesToConsole( | ||
cmdToLines(event.updateTasks.map(updatingLine), event.cmd) | ||
); | ||
}); | ||
updtr.on("batch-update/testing", event => { | ||
writeLinesToConsole( | ||
cmdToLines(event.updateTasks.map(testingLine), event.cmd) | ||
); | ||
}); | ||
updtr.on("batch-update/rollback", event => { | ||
writeLinesToConsole( | ||
cmdToLines(event.updateTasks.map(rollbackLine), event.cmd) | ||
); | ||
}); | ||
updtr.on("batch-update/result", event => { | ||
if (event.success === true) { | ||
writeLinesToConsole( | ||
event.updateTasks.map(event.success ? successLine : failLine) | ||
); | ||
} | ||
// Not showing the test stdout here when there was an error because | ||
// we will proceed with the sequential update. | ||
}); | ||
updtr.on("sequential-update/updating", event => { | ||
writeLinesToConsole(cmdToLines(updatingLine(event), event.cmd)); | ||
}); | ||
updtr.on("sequential-update/testing", event => { | ||
writeLinesToConsole(cmdToLines(testingLine(event), event.cmd)); | ||
}); | ||
updtr.on("sequential-update/rollback", event => { | ||
writeLinesToConsole(cmdToLines(rollbackLine(event), event.cmd)); | ||
}); | ||
updtr.on("sequential-update/result", event => { | ||
writeLinesToConsole([(event.success ? successLine : failLine)(event)]); | ||
if (reporterConfig.testStdout === true && event.success === false) { | ||
writeLinesToConsole([event.stdout]); | ||
} | ||
}); | ||
updtr.on("end", ({results}) => { | ||
const duration = msToString(Date.now() - startTime); | ||
const successful = filterSuccessfulUpdates(results); | ||
const failed = filterFailedUpdates(results); | ||
|
||
writeLinesToConsole([""]); | ||
|
||
if (successful.length > 0) { | ||
writeLinesToConsole([ | ||
new Message(chalk.bold("%s successful") + " update%s.", [ | ||
successful.length, | ||
pluralize(successful.length), | ||
]), | ||
]); | ||
} | ||
if (failed.length > 0) { | ||
writeLinesToConsole([ | ||
new Message(chalk.bold("%s failed") + " update%s.", [ | ||
failed.length, | ||
pluralize(failed.length), | ||
]), | ||
]); | ||
} | ||
if (excludedModules.length > 0) { | ||
const list = excludedModules.map(excludedLine); | ||
|
||
if (successful.length > 0 || failed.length > 0) { | ||
writeLinesToConsole([""]); | ||
} | ||
writeLinesToConsole( | ||
[ | ||
new Message(chalk.bold("%s skipped") + " module%s:", [ | ||
excludedModules.length, | ||
pluralize(excludedModules.length), | ||
]), | ||
"", | ||
].concat(list) | ||
); | ||
} | ||
|
||
writeLinesToConsole(["", new Message("Finished after %s.", [duration])]); | ||
}); | ||
updtr.on("error", err => void handleError(err)); | ||
} |
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import basic from "./basic"; | ||
import dense from "./dense"; | ||
import none from "./none"; | ||
|
||
// The first property here is the default reporter | ||
const reporters = {dense, none}; | ||
const reporters = {dense, basic, none}; | ||
|
||
export default reporters; |
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,132 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`basic() batch-update fail and show test stdout should print the expected lines: batch-update fail and show test stdout 1`] = ` | ||
"[JInstalling missing dependencies[90m...[39m | ||
[90m> npm install [39m | ||
[JLooking for outdated modules[90m...[39m | ||
[90m> npm outdated [39m | ||
[JFound [1m4 updates[22m. | ||
[J[33m-[39m [1mmodule[22m [90mupdating[39m 0.0.0 [90m→[39m 0.0.1[90m...[39m | ||
[33m-[39m [1mmodule[22m [90mupdating[39m 0.0.0 [90m→[39m 0.1.0[90m...[39m | ||
[33m-[39m [1mmodule[22m [90mupdating[39m 0.0.0 [90m→[39m 1.0.0[90m...[39m | ||
[33m-[39m [1mmodule[22m [90mupdating[39m 0.0.0 [90m→[39m 0.0.1[90m...[39m | ||
[90m> npm install [39m | ||
[J[33m-[39m [1mmodule[22m [90mtesting...[39m | ||
[33m-[39m [1mmodule[22m [90mtesting...[39m | ||
[33m-[39m [1mmodule[22m [90mtesting...[39m | ||
[33m-[39m [1mmodule[22m [90mtesting...[39m | ||
[90m> npm test [39m | ||
[J[31m-[39m [1m[31mmodule[39m[22m [90mrolling back[39m 0.0.1 [90m→[39m 0.0.0[90m...[39m | ||
[31m-[39m [1m[31mmodule[39m[22m [90mrolling back[39m 0.1.0 [90m→[39m 0.0.0[90m...[39m | ||
[31m-[39m [1m[31mmodule[39m[22m [90mrolling back[39m 1.0.0 [90m→[39m 0.0.0[90m...[39m | ||
[31m-[39m [1m[31mmodule[39m[22m [90mrolling back[39m 0.0.1 [90m→[39m 0.0.0[90m...[39m | ||
[90m> npm install [39m | ||
[J | ||
[J[1m2 successful[22m updates. | ||
[J[1m1 failed[22m update. | ||
[J | ||
Finished after 1.0s." | ||
`; | ||
|
||
exports[`basic() batch-update success and show test stdout should print the expected lines: batch-update success and show test stdout 1`] = ` | ||
"[JInstalling missing dependencies[90m...[39m | ||
[90m> npm install [39m | ||
[JLooking for outdated modules[90m...[39m | ||
[90m> npm outdated [39m | ||
[JFound [1m4 updates[22m. | ||
[J[33m-[39m [1mmodule[22m [90mupdating[39m 0.0.0 [90m→[39m 0.0.1[90m...[39m | ||
[33m-[39m [1mmodule[22m [90mupdating[39m 0.0.0 [90m→[39m 0.1.0[90m...[39m | ||
[33m-[39m [1mmodule[22m [90mupdating[39m 0.0.0 [90m→[39m 1.0.0[90m...[39m | ||
[33m-[39m [1mmodule[22m [90mupdating[39m 0.0.0 [90m→[39m 0.0.1[90m...[39m | ||
[90m> npm install [39m | ||
[J[33m-[39m [1mmodule[22m [90mtesting...[39m | ||
[33m-[39m [1mmodule[22m [90mtesting...[39m | ||
[33m-[39m [1mmodule[22m [90mtesting...[39m | ||
[33m-[39m [1mmodule[22m [90mtesting...[39m | ||
[90m> npm test [39m | ||
[J[32m-[39m [1mmodule[22m 0.0.1 [90msuccess[39m | ||
[32m-[39m [1mmodule[22m 0.1.0 [90msuccess[39m | ||
[32m-[39m [1mmodule[22m 1.0.0 [90msuccess[39m | ||
[32m-[39m [1mmodule[22m 0.0.1 [90msuccess[39m | ||
[J | ||
[J[1m3 successful[22m updates. | ||
[J | ||
Finished after 1.0s." | ||
`; | ||
|
||
exports[`basic() custom config and only excluded modules should print the expected lines: custom config and only excluded modules 1`] = ` | ||
"[JRunning updtr with custom configuration: | ||
- exclude: b, c | ||
[JInstalling missing dependencies[90m...[39m | ||
[90m> npm install [39m | ||
[JLooking for outdated modules[90m...[39m | ||
[90m> npm outdated [39m | ||
[J[1mNo updates available[22m for the given modules and version range | ||
[J | ||
[J[1m3 skipped[22m modules: | ||
[90m-[39m [1ma[22m [90mgit[39m | ||
[90m-[39m [1mb[22m [90mexcluded[39m | ||
[90m-[39m [1mc[22m [90mexcluded[39m | ||
[J | ||
Finished after 1.0s." | ||
`; | ||
|
||
exports[`basic() custom config and sequential-update with mixed success and show test stdout should print the expected lines: custom config and sequential-update with mixed success and show test stdout 1`] = ` | ||
"[JRunning updtr with custom configuration: | ||
- exclude: b, c | ||
[JInstalling missing dependencies[90m...[39m | ||
[90m> npm install [39m | ||
[JLooking for outdated modules[90m...[39m | ||
[90m> npm outdated [39m | ||
[JFound [1m4 updates[22m. | ||
[J[33m-[39m [1mmodule[22m [90mupdating[39m 0.0.0 [90m→[39m 0.0.1[90m...[39m | ||
[90m> npm install [39m | ||
[J[33m-[39m [1mmodule[22m [90mtesting...[39m | ||
[90m> npm test [39m | ||
[J[32m-[39m [1mmodule[22m 0.0.1 [90msuccess[39m | ||
[J[33m-[39m [1mmodule[22m [90mupdating[39m 0.0.0 [90m→[39m 0.1.0[90m...[39m | ||
[90m> npm install [39m | ||
[J[33m-[39m [1mmodule[22m [90mtesting...[39m | ||
[90m> npm test [39m | ||
[J[31m-[39m [1m[31mmodule[39m[22m [90mrolling back[39m 0.1.0 [90m→[39m 0.0.0[90m...[39m | ||
[90m> npm install [39m | ||
[J[31m-[39m [1m[31mmodule[39m[22m 0.1.0 [90mfailed[39m | ||
[JThis is the test stdout | ||
[J[33m-[39m [1mmodule[22m [90mupdating[39m 0.0.0 [90m→[39m 1.0.0[90m...[39m | ||
[90m> npm install [39m | ||
[J[33m-[39m [1mmodule[22m [90mtesting...[39m | ||
[90m> npm test [39m | ||
[J[32m-[39m [1mmodule[22m 1.0.0 [90msuccess[39m | ||
[J[33m-[39m [1mmodule[22m [90mupdating[39m 0.0.0 [90m→[39m 0.0.1[90m...[39m | ||
[90m> npm install [39m | ||
[J[33m-[39m [1mmodule[22m [90mtesting...[39m | ||
[90m> npm test [39m | ||
[J[31m-[39m [1m[31mmodule[39m[22m [90mrolling back[39m 0.0.1 [90m→[39m 0.0.0[90m...[39m | ||
[90m> npm install [39m | ||
[J[31m-[39m [1m[31mmodule[39m[22m 0.0.1 [90mfailed[39m | ||
[JThis is the test stdout | ||
[J | ||
[J[1m2 successful[22m updates. | ||
[J[1m2 failed[22m updates. | ||
[J | ||
Finished after 1.0s." | ||
`; | ||
|
||
exports[`basic() no outdated modules should print the expected lines: no outdated modules 1`] = ` | ||
"[JInstalling missing dependencies[90m...[39m | ||
[90m> npm install [39m | ||
[JLooking for outdated modules[90m...[39m | ||
[90m> npm outdated [39m | ||
[JEverything [1mup-to-date[22m | ||
[J | ||
[J | ||
Finished after 1.0s." | ||
`; |
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
exports[`reporters should export all available reporters 1`] = ` | ||
Array [ | ||
"dense", | ||
"basic", | ||
"none", | ||
] | ||
`; |
Oops, something went wrong.