1
1
const { logStatuses } = require ( '../log/main' )
2
2
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
9
8
}
10
9
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
+ }
16
12
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
19
16
}
20
17
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.
22
25
const addStatus = function ( { newStatus, statuses, event, package, packageJson : { version } = { } } ) {
23
26
// Either:
24
27
// - `build.command`
25
- // - `utils. status.show()` not called but set in a previous event
28
+ // - no status was set
26
29
if ( newStatus === undefined ) {
27
30
return statuses
28
31
}
29
32
30
- // Error statuses cannot be overwritten
31
33
const formerStatus = statuses . find ( status => status . package === package )
32
- if ( formerStatus !== undefined && formerStatus . state !== 'success' ) {
34
+ if ( ! canOverrideStatus ( formerStatus , newStatus ) ) {
33
35
return statuses
34
36
}
35
37
@@ -38,6 +40,21 @@ const addStatus = function({ newStatus, statuses, event, package, packageJson: {
38
40
return [ ...newStatuses , { ...newStatus , event, package, version } ]
39
41
}
40
42
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
+
41
58
const reportStatuses = async function ( statuses , api , mode ) {
42
59
printStatuses ( statuses , mode )
43
60
}
0 commit comments