Skip to content

Commit f6b100c

Browse files
authored
Assign a default status for plugins that do not use utils.status.show() (#1275)
1 parent f330d34 commit f6b100c

File tree

22 files changed

+532
-26
lines changed

22 files changed

+532
-26
lines changed

packages/build/src/core/commands.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const runCommands = async function({ commands, configPath, buildDir, nodePath, c
7979
index,
8080
childEnv,
8181
envChanges,
82-
statuses,
82+
commands,
8383
errorMonitor,
8484
error,
8585
failedPlugins,
@@ -113,7 +113,7 @@ const runCommand = async function({
113113
index,
114114
childEnv,
115115
envChanges,
116-
statuses,
116+
commands,
117117
errorMonitor,
118118
error,
119119
failedPlugins,
@@ -138,7 +138,7 @@ const runCommand = async function({
138138
nodePath,
139139
childEnv,
140140
envChanges,
141-
statuses,
141+
commands,
142142
error,
143143
})
144144

@@ -173,14 +173,14 @@ const fireCommand = function({
173173
nodePath,
174174
childEnv,
175175
envChanges,
176-
statuses,
176+
commands,
177177
error,
178178
}) {
179179
if (buildCommand !== undefined) {
180180
return fireBuildCommand({ buildCommand, configPath, buildDir, nodePath, childEnv, envChanges })
181181
}
182182

183-
return firePluginCommand({ event, childProcess, package, packageJson, local, envChanges, statuses, error })
183+
return firePluginCommand({ event, childProcess, package, packageJson, local, envChanges, commands, error })
184184
}
185185

186186
// Fire `build.command`
@@ -218,7 +218,7 @@ const firePluginCommand = async function({
218218
packageJson,
219219
local,
220220
envChanges,
221-
statuses,
221+
commands,
222222
error,
223223
}) {
224224
pipeOutput(childProcess)
@@ -230,7 +230,7 @@ const firePluginCommand = async function({
230230
{ event, error, envChanges },
231231
{ plugin: { packageJson, package }, location: { event, package, local } },
232232
)
233-
const newStatus = getSuccessStatus({ status, statuses, package })
233+
const newStatus = getSuccessStatus(status, { commands, event, package })
234234
return { newEnvChanges, newStatus }
235235
} catch (newError) {
236236
return { newError }

packages/build/src/core/status.js

+34-17
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
const { logStatuses } = require('../log/main')
22

3-
// Assign default value for successful `newStatus`
4-
// Retrieved from `utils.status.show()`, which is optional
5-
const getSuccessStatus = function({ status, statuses, package }) {
6-
// `utils.status.show()` called in the current event
7-
if (status !== undefined) {
8-
return status
3+
// The last event handler of a plugin (except for `onError` and `onEnd`)
4+
// defaults to `utils.status.show({ state: 'success' })` without any `summary`.
5+
const getSuccessStatus = function(newStatus, { commands, event, package }) {
6+
if (newStatus === undefined && isLastMainCommand({ commands, event, package })) {
7+
return IMPLICIT_STATUS
98
}
109

11-
// `utils.status.show()` not called, but set in a previous event
12-
const hasStatus = statuses.some(pluginStatus => pluginStatus.package === package)
13-
if (hasStatus) {
14-
return
15-
}
10+
return newStatus
11+
}
1612

17-
// `utils.status.show()` not called, but this is the first event, so we assign a default
18-
return { state: 'success' }
13+
const isLastMainCommand = function({ commands, event, package }) {
14+
const mainCommands = commands.filter(command => command.package === package && isMainCommand(command))
15+
return mainCommands.length === 0 || mainCommands[mainCommands.length - 1].event === event
1916
}
2017

21-
// Merge `success` status to the list of plugin statuses.
18+
const isMainCommand = function({ event }) {
19+
return event !== 'onEnd' && event !== 'onError'
20+
}
21+
22+
const IMPLICIT_STATUS = { state: 'success', implicit: true }
23+
24+
// Merge plugin status to the list of plugin statuses.
2225
const addStatus = function({ newStatus, statuses, event, package, packageJson: { version } = {} }) {
2326
// Either:
2427
// - `build.command`
25-
// - `utils.status.show()` not called but set in a previous event
28+
// - no status was set
2629
if (newStatus === undefined) {
2730
return statuses
2831
}
2932

30-
// Error statuses cannot be overwritten
3133
const formerStatus = statuses.find(status => status.package === package)
32-
if (formerStatus !== undefined && formerStatus.state !== 'success') {
34+
if (!canOverrideStatus(formerStatus, newStatus)) {
3335
return statuses
3436
}
3537

@@ -38,6 +40,21 @@ const addStatus = function({ newStatus, statuses, event, package, packageJson: {
3840
return [...newStatuses, { ...newStatus, event, package, version }]
3941
}
4042

43+
const canOverrideStatus = function(formerStatus, newStatus) {
44+
// No previous status
45+
if (formerStatus === undefined) {
46+
return true
47+
}
48+
49+
// Implicit statuses can never override
50+
if (newStatus.implicit) {
51+
return false
52+
}
53+
54+
// Error statuses can only be overwritten by other error statuses
55+
return formerStatus.state === 'success' || newStatus.state !== 'success'
56+
}
57+
4158
const reportStatuses = async function(statuses, api, mode) {
4259
printStatuses(statuses, mode)
4360
}
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,8 @@
1+
module.exports = {
2+
onBuild() {
3+
throw new Error('onBuild')
4+
},
5+
onError() {
6+
throw new Error('onError')
7+
},
8+
}

packages/build/tests/plugins/status/fixtures/error_status_override/plugin.js

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ module.exports = {
22
onBuild() {
33
throw new Error('onBuild')
44
},
5+
onSuccess({
6+
utils: {
7+
status: { show },
8+
},
9+
}) {
10+
show({ summary: 'summary' })
11+
},
512
onError({
613
utils: {
714
status: { show },
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,3 @@
1+
module.exports = {
2+
onBuild() {},
3+
}
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,3 @@
1+
module.exports = {
2+
onEnd() {},
3+
}
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,4 @@
1+
module.exports = {
2+
onBuild() {},
3+
onSuccess() {},
4+
}
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,10 @@
1+
module.exports = {
2+
onBuild({
3+
utils: {
4+
status: { show },
5+
},
6+
}) {
7+
show({ summary: 'summary' })
8+
},
9+
onSuccess() {},
10+
}
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 @@
1+
module.exports = {}
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,3 @@
1+
module.exports = {
2+
onError() {},
3+
}
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,16 @@
1+
module.exports = {
2+
onBuild({
3+
utils: {
4+
status: { show },
5+
},
6+
}) {
7+
show({ summary: 'onBuild' })
8+
},
9+
onSuccess({
10+
utils: {
11+
status: { show },
12+
},
13+
}) {
14+
show({ summary: 'onSuccess' })
15+
},
16+
}

0 commit comments

Comments
 (0)