Skip to content

Commit aaf423f

Browse files
authored
Strip ANSI sequences from statuses (#1287)
1 parent b5d1ad9 commit aaf423f

File tree

6 files changed

+112
-2
lines changed

6 files changed

+112
-2
lines changed

packages/build/src/core/status.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const { env } = require('process')
22

3+
const stripAnsi = require('strip-ansi')
4+
35
const { serializeErrorStatus } = require('../error/parse/serialize_status')
46
const { logStatuses } = require('../log/main')
57

@@ -66,8 +68,31 @@ const reportPluginLoadError = async function({ error, api, mode, event, package,
6668
}
6769

6870
const reportStatuses = async function(statuses, api, mode) {
69-
printStatuses(statuses, mode)
70-
await sendStatuses(statuses, api, mode)
71+
const statusesA = removeStatusesColors(statuses)
72+
printStatuses(statusesA, mode)
73+
await sendStatuses(statusesA, api, mode)
74+
}
75+
76+
// Remove colors from statuses
77+
const removeStatusesColors = function(statuses) {
78+
return statuses.map(removeStatusColors)
79+
}
80+
81+
const removeStatusColors = function(status) {
82+
const attributes = COLOR_ATTRIBUTES.map(attribute => removeAttrColor(status, attribute))
83+
return Object.assign({}, status, ...attributes)
84+
}
85+
86+
const COLOR_ATTRIBUTES = ['title', 'summary', 'text']
87+
88+
const removeAttrColor = function(status, attribute) {
89+
const value = status[attribute]
90+
if (value === undefined) {
91+
return {}
92+
}
93+
94+
const valueA = stripAnsi(value)
95+
return { [attribute]: valueA }
7196
}
7297

7398
// When not in production, print statuses to console.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[[plugins]]
2+
package = "./plugin.js"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const { red } = require('chalk')
2+
3+
module.exports = {
4+
onBuild({
5+
utils: {
6+
status: { show },
7+
},
8+
}) {
9+
show({ summary: red('summary') })
10+
},
11+
}

packages/build/tests/plugins/status/snapshots/tests.js.md

+68
Original file line numberDiff line numberDiff line change
@@ -1980,6 +1980,74 @@ Generated by [AVA](https://ava.li).
19801980
},
19811981
]
19821982

1983+
## utils.status.show() statuses are sent to the API without colors
1984+
1985+
> Snapshot 1
1986+
1987+
`␊
1988+
┌─────────────────────────────┐␊
1989+
│ Netlify Build │␊
1990+
└─────────────────────────────┘␊
1991+
1992+
> Version␊
1993+
@netlify/build 1.0.0␊
1994+
1995+
> Flags␊
1996+
repositoryRoot: /file/path␊
1997+
1998+
> Current directory␊
1999+
/file/path␊
2000+
2001+
> Config file␊
2002+
/file/path␊
2003+
2004+
> Resolved config␊
2005+
plugins:␊
2006+
- package: /file/path␊
2007+
2008+
> Context␊
2009+
production␊
2010+
2011+
> Loading plugins␊
2012+
- /file/path␊
2013+
2014+
┌─────────────────────────────────────┐␊
2015+
│ 1. onBuild command from /file/path │␊
2016+
└─────────────────────────────────────┘␊
2017+
2018+
2019+
(/file/path onBuild completed in 1ms)␊
2020+
2021+
┌─────────────────────────────┐␊
2022+
│ Summary │␊
2023+
└─────────────────────────────┘␊
2024+
2025+
> Plugin /file/path ran successfully␊
2026+
summary␊
2027+
2028+
┌─────────────────────────────┐␊
2029+
│ Netlify Build Complete │␊
2030+
└─────────────────────────────┘␊
2031+
2032+
(Netlify Build completed in 1ms)`
2033+
2034+
> Snapshot 2
2035+
2036+
[
2037+
{
2038+
body: {
2039+
package: './plugin.js',
2040+
reporting_event: 'onBuild',
2041+
state: 'success',
2042+
summary: 'summary',
2043+
text: undefined,
2044+
version: '1.0.0',
2045+
},
2046+
headers: 'accept accept-encoding authorization connection content-length content-type host user-agent',
2047+
method: 'POST',
2048+
},
2049+
]
2050+
19832051
## utils.status.show() summary should be a string
19842052

19852053
> Snapshot 1
Binary file not shown.

packages/build/tests/plugins/status/tests.js

+4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ test('utils.status.show() statuses are not sent to the API without a DEPLOY_ID',
6666
await runWithApiMock(t, 'print', { env: { DEPLOY_ID: '' } })
6767
})
6868

69+
test('utils.status.show() statuses are sent to the API without colors', async t => {
70+
await runWithApiMock(t, 'colors')
71+
})
72+
6973
test('report error statuses from failBuild()', async t => {
7074
await runWithApiMock(t, 'error_fail_build')
7175
})

0 commit comments

Comments
 (0)