-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
run_plugins.js
221 lines (164 loc) · 5.72 KB
/
run_plugins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// this module is responsible for loading the plugins file
// and running the exported function to register event handlers
// and executing any tasks that the plugin registers
const _ = require('lodash')
const debug = require('debug')('cypress:server:plugins:child')
const Promise = require('bluebird')
const errors = require('../../errors')
const preprocessor = require('./preprocessor')
const task = require('./task')
const util = require('../util')
const validateEvent = require('./validate_event')
const { registerTsNode } = require('../../util/ts-node')
const ARRAY_METHODS = ['concat', 'push', 'unshift', 'slice', 'pop', 'shift', 'slice', 'splice', 'filter', 'map', 'forEach', 'reduce', 'reverse', 'splice', 'includes']
const registeredEvents = {}
const invoke = (eventId, args = []) => {
const event = registeredEvents[eventId]
return event.handler(...args)
}
let plugins
const load = (ipc, config, pluginsFile) => {
debug('run plugins function')
let eventIdCount = 0
const registrations = []
// we track the register calls and then send them all at once
// to the parent process
const register = (event, handler) => {
const { isValid, error } = validateEvent(event, handler)
if (!isValid) {
ipc.send('load:error', 'PLUGINS_VALIDATION_ERROR', pluginsFile, error.stack)
return
}
if (event === 'task') {
const existingEventId = _.findKey(registeredEvents, { event: 'task' })
if (existingEventId) {
handler = task.merge(registeredEvents[existingEventId].handler, handler)
registeredEvents[existingEventId] = { event, handler }
debug('extend task events with id', existingEventId)
return
}
}
const eventId = eventIdCount++
registeredEvents[eventId] = { event, handler }
debug('register event', event, 'with id', eventId)
registrations.push({
event,
eventId,
})
}
// events used for parent/child communication
register('_get:task:body', () => {})
register('_get:task:keys', () => {})
Promise
.try(() => {
debug('run plugins function')
return plugins(register, config)
})
.then((modifiedCfg) => {
debug('plugins file successfully loaded')
ipc.send('loaded', modifiedCfg, registrations)
})
.catch((err) => {
debug('plugins file errored:', err && err.stack)
ipc.send('load:error', 'PLUGINS_FUNCTION_ERROR', pluginsFile, err.stack)
})
}
const execute = (ipc, event, ids, args = []) => {
debug(`execute plugin event: ${event} (%o)`, ids)
switch (event) {
case 'after:screenshot':
util.wrapChildPromise(ipc, invoke, ids, args)
return
case 'file:preprocessor':
preprocessor.wrap(ipc, invoke, ids, args)
return
case 'before:browser:launch': {
// TODO: remove in next breaking release
// This will send a warning message when a deprecated API is used
// define array-like functions on this object so we can warn about using deprecated array API
// while still fufiling desired behavior
const [, launchOptions] = args
let hasEmittedWarning = false
ARRAY_METHODS.forEach((name) => {
const boundFn = launchOptions.args[name].bind(launchOptions.args)
launchOptions[name] = function () {
if (hasEmittedWarning) return
hasEmittedWarning = true
const warning = errors.get('DEPRECATED_BEFORE_BROWSER_LAUNCH_ARGS')
ipc.send('warning', util.serializeError(warning))
// eslint-disable-next-line prefer-rest-params
return boundFn.apply(this, arguments)
}
})
Object.defineProperty(launchOptions, 'length', {
get () {
return this.args.length
},
})
launchOptions[Symbol.iterator] = launchOptions.args[Symbol.iterator].bind(launchOptions.args)
util.wrapChildPromise(ipc, invoke, ids, args)
return
}
case 'task':
task.wrap(ipc, registeredEvents, ids, args)
return
case '_get:task:keys':
task.getKeys(ipc, registeredEvents, ids)
return
case '_get:task:body':
task.getBody(ipc, registeredEvents, ids, args)
return
default:
debug('unexpected execute message:', event, args)
return
}
}
let tsRegistered = false
module.exports = (ipc, pluginsFile, projectRoot) => {
debug('pluginsFile:', pluginsFile)
debug('project root:', projectRoot)
if (!projectRoot) {
throw new Error('Unexpected: projectRoot should be a string')
}
process.on('uncaughtException', (err) => {
debug('uncaught exception:', util.serializeError(err))
ipc.send('error', util.serializeError(err))
return false
})
process.on('unhandledRejection', (event) => {
const err = (event && event.reason) || event
debug('unhandled rejection:', util.serializeError(err))
ipc.send('error', util.serializeError(err))
return false
})
if (!tsRegistered) {
registerTsNode(projectRoot)
// ensure typescript is only registered once
tsRegistered = true
}
try {
debug('require pluginsFile')
plugins = require(pluginsFile)
// Handle export default () => {}
if (plugins && typeof plugins.default === 'function') {
plugins = plugins.default
}
} catch (err) {
debug('failed to require pluginsFile:\n%s', err.stack)
ipc.send('load:error', 'PLUGINS_FILE_ERROR', pluginsFile, err.stack)
return
}
if (typeof plugins !== 'function') {
debug('not a function')
ipc.send('load:error', 'PLUGINS_DIDNT_EXPORT_FUNCTION', pluginsFile, plugins)
return
}
ipc.on('load', (config) => {
debug('plugins load file "%s"', pluginsFile)
debug('passing config %o', config)
load(ipc, config, pluginsFile)
})
ipc.on('execute', (event, ids, args) => {
execute(ipc, event, ids, args)
})
}