|
1 | | -/* |
2 | | - * decaffeinate suggestions: |
3 | | - * DS102: Remove unnecessary code created because of implicit returns |
4 | | - * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md |
5 | | - */ |
6 | | -const _ = require("lodash"); |
7 | | -const cp = require("child_process"); |
8 | | -const path = require("path"); |
9 | | -const debug = require("debug")("cypress:server:plugins"); |
10 | | -const Promise = require("bluebird"); |
11 | | -const errors = require("../errors"); |
12 | | -const util = require("./util"); |
13 | | - |
14 | | -let pluginsProcess = null; |
15 | | -let registeredEvents = {}; |
16 | | -let handlers = []; |
17 | | - |
18 | | -const register = function(event, callback) { |
19 | | - debug(`register event '${event}'`); |
| 1 | +const _ = require('lodash') |
| 2 | +const cp = require('child_process') |
| 3 | +const path = require('path') |
| 4 | +const debug = require('debug')('cypress:server:plugins') |
| 5 | +const Promise = require('bluebird') |
| 6 | +const errors = require('../errors') |
| 7 | +const util = require('./util') |
| 8 | + |
| 9 | +let pluginsProcess = null |
| 10 | +let registeredEvents = {} |
| 11 | +let handlers = [] |
| 12 | + |
| 13 | +const register = (event, callback) => { |
| 14 | + debug(`register event '${event}'`) |
20 | 15 |
|
21 | 16 | if (!_.isString(event)) { |
22 | | - throw new Error(`The plugin register function must be called with an event as its 1st argument. You passed '${event}'.`); |
| 17 | + throw new Error(`The plugin register function must be called with an event as its 1st argument. You passed '${event}'.`) |
23 | 18 | } |
24 | 19 |
|
25 | 20 | if (!_.isFunction(callback)) { |
26 | | - throw new Error(`The plugin register function must be called with a callback function as its 2nd argument. You passed '${callback}'.`); |
| 21 | + throw new Error(`The plugin register function must be called with a callback function as its 2nd argument. You passed '${callback}'.`) |
27 | 22 | } |
28 | 23 |
|
29 | | - return registeredEvents[event] = callback; |
30 | | -}; |
| 24 | + registeredEvents[event] = callback |
| 25 | +} |
31 | 26 |
|
32 | | -module.exports = { |
33 | | - //# for testing |
34 | | - _setPluginsProcess(_pluginsProcess) { |
35 | | - return pluginsProcess = _pluginsProcess; |
36 | | - }, |
| 27 | +const getPluginPid = () => { |
| 28 | + if (pluginsProcess) { |
| 29 | + return pluginsProcess.pid |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +const registerHandler = (handler) => { |
| 34 | + handlers.push(handler) |
| 35 | +} |
| 36 | + |
| 37 | +const init = (config, options) => { |
| 38 | + debug('plugins.init', config.pluginsFile) |
| 39 | + |
| 40 | + return new Promise((resolve, reject) => { |
| 41 | + if (!config.pluginsFile) { |
| 42 | + return resolve() |
| 43 | + } |
37 | 44 |
|
38 | | - getPluginPid() { |
39 | 45 | if (pluginsProcess) { |
40 | | - return pluginsProcess.pid; |
| 46 | + debug('kill existing plugins process') |
| 47 | + pluginsProcess.kill() |
41 | 48 | } |
42 | | - }, |
43 | 49 |
|
44 | | - registerHandler(handler) { |
45 | | - return handlers.push(handler); |
46 | | - }, |
| 50 | + registeredEvents = {} |
| 51 | + |
| 52 | + const childIndexFilename = path.join(__dirname, 'child', 'index.js') |
| 53 | + const childArguments = ['--file', config.pluginsFile] |
| 54 | + const childOptions = { |
| 55 | + stdio: 'inherit', |
| 56 | + } |
| 57 | + |
| 58 | + if (config.resolvedNodePath) { |
| 59 | + debug('launching using custom node version %o', _.pick(config, ['resolvedNodePath', 'resolvedNodeVersion'])) |
| 60 | + childOptions.execPath = config.resolvedNodePath |
| 61 | + } |
| 62 | + |
| 63 | + debug('forking to run %s', childIndexFilename) |
| 64 | + pluginsProcess = cp.fork(childIndexFilename, childArguments, childOptions) |
| 65 | + const ipc = util.wrapIpc(pluginsProcess) |
| 66 | + |
| 67 | + for (let handler of handlers) { |
| 68 | + handler(ipc) |
| 69 | + } |
| 70 | + |
| 71 | + ipc.send('load', config) |
| 72 | + |
| 73 | + ipc.on('loaded', (newCfg, registrations) => { |
| 74 | + _.each(registrations, (registration) => { |
| 75 | + debug('register plugins process event', registration.event, 'with id', registration.eventId) |
47 | 76 |
|
48 | | - init(config, options) { |
49 | | - debug("plugins.init", config.pluginsFile); |
| 77 | + register(registration.event, (...args) => { |
| 78 | + return util.wrapParentPromise(ipc, registration.eventId, (invocationId) => { |
| 79 | + debug('call event', registration.event, 'for invocation id', invocationId) |
| 80 | + const ids = { |
| 81 | + eventId: registration.eventId, |
| 82 | + invocationId, |
| 83 | + } |
| 84 | + |
| 85 | + ipc.send('execute', registration.event, ids, args) |
| 86 | + }) |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + debug('resolving with new config %o', newCfg) |
| 91 | + |
| 92 | + resolve(newCfg) |
| 93 | + }) |
50 | 94 |
|
51 | | - return new Promise(function(resolve, reject) { |
52 | | - if (!config.pluginsFile) { return resolve(); } |
| 95 | + ipc.on('load:error', (type, ...args) => { |
| 96 | + debug('load:error %s, rejecting', type) |
53 | 97 |
|
54 | | - if (pluginsProcess) { |
55 | | - debug("kill existing plugins process"); |
56 | | - pluginsProcess.kill(); |
| 98 | + reject(errors.get(type, ...args)) |
| 99 | + }) |
| 100 | + |
| 101 | + const killPluginsProcess = () => { |
| 102 | + pluginsProcess && pluginsProcess.kill() |
| 103 | + pluginsProcess = null |
| 104 | + } |
| 105 | + |
| 106 | + const handleError = (err) => { |
| 107 | + debug('plugins process error:', err.stack) |
| 108 | + if (!pluginsProcess) { |
| 109 | + return // prevent repeating this in case of multiple errors |
57 | 110 | } |
58 | 111 |
|
59 | | - registeredEvents = {}; |
| 112 | + killPluginsProcess() |
| 113 | + err = errors.get('PLUGINS_ERROR', err.annotated || err.stack || err.message) |
| 114 | + err.title = 'Error running plugin' |
60 | 115 |
|
61 | | - const childIndexFilename = path.join(__dirname, "child", "index.js"); |
62 | | - const childArguments = ["--file", config.pluginsFile]; |
63 | | - const childOptions = { |
64 | | - stdio: "inherit" |
65 | | - }; |
| 116 | + return options.onError(err) |
| 117 | + } |
66 | 118 |
|
67 | | - if (config.resolvedNodePath) { |
68 | | - debug("launching using custom node version %o", _.pick(config, ['resolvedNodePath', 'resolvedNodeVersion'])); |
69 | | - childOptions.execPath = config.resolvedNodePath; |
| 119 | + const handleWarning = function (warningErr) { |
| 120 | + debug('plugins process warning:', warningErr.stack) |
| 121 | + if (!pluginsProcess) { |
| 122 | + return // prevent repeating this in case of multiple warnings |
70 | 123 | } |
71 | 124 |
|
72 | | - debug("forking to run %s", childIndexFilename); |
73 | | - pluginsProcess = cp.fork(childIndexFilename, childArguments, childOptions); |
74 | | - const ipc = util.wrapIpc(pluginsProcess); |
| 125 | + return options.onWarning(warningErr) |
| 126 | + } |
75 | 127 |
|
76 | | - for (let handler of handlers) { handler(ipc); } |
| 128 | + pluginsProcess.on('error', handleError) |
| 129 | + ipc.on('error', handleError) |
| 130 | + ipc.on('warning', handleWarning) |
77 | 131 |
|
78 | | - ipc.send("load", config); |
| 132 | + // see timers/parent.js line #93 for why this is necessary |
| 133 | + process.on('exit', killPluginsProcess) |
| 134 | + }) |
| 135 | +} |
79 | 136 |
|
80 | | - ipc.on("loaded", function(newCfg, registrations) { |
81 | | - _.each(registrations, function(registration) { |
82 | | - debug("register plugins process event", registration.event, "with id", registration.eventId); |
| 137 | +const has = (event) => { |
| 138 | + const isRegistered = !!registeredEvents[event] |
83 | 139 |
|
84 | | - return register(registration.event, (...args) => util.wrapParentPromise(ipc, registration.eventId, function(invocationId) { |
85 | | - debug("call event", registration.event, "for invocation id", invocationId); |
86 | | - const ids = { |
87 | | - eventId: registration.eventId, |
88 | | - invocationId |
89 | | - }; |
90 | | - return ipc.send("execute", registration.event, ids, args); |
91 | | - })); |
92 | | - }); |
93 | | - |
94 | | - debug("resolving with new config %o", newCfg); |
95 | | - return resolve(newCfg); |
96 | | - }); |
97 | | - |
98 | | - ipc.on("load:error", function(type, ...args) { |
99 | | - debug("load:error %s, rejecting", type); |
100 | | - return reject(errors.get(type, ...args)); |
101 | | - }); |
102 | | - |
103 | | - const killPluginsProcess = function() { |
104 | | - pluginsProcess && pluginsProcess.kill(); |
105 | | - return pluginsProcess = null; |
106 | | - }; |
107 | | - |
108 | | - const handleError = function(err) { |
109 | | - debug("plugins process error:", err.stack); |
110 | | - if (!pluginsProcess) { return; } //# prevent repeating this in case of multiple errors |
111 | | - killPluginsProcess(); |
112 | | - err = errors.get("PLUGINS_ERROR", err.annotated || err.stack || err.message); |
113 | | - err.title = "Error running plugin"; |
114 | | - return options.onError(err); |
115 | | - }; |
116 | | - |
117 | | - const handleWarning = function(warningErr) { |
118 | | - debug("plugins process warning:", warningErr.stack); |
119 | | - if (!pluginsProcess) { return; } //# prevent repeating this in case of multiple warnings |
120 | | - return options.onWarning(warningErr); |
121 | | - }; |
122 | | - |
123 | | - pluginsProcess.on("error", handleError); |
124 | | - ipc.on("error", handleError); |
125 | | - ipc.on("warning", handleWarning); |
126 | | - |
127 | | - //# see timers/parent.js line #93 for why this is necessary |
128 | | - return process.on("exit", killPluginsProcess); |
129 | | - }); |
130 | | - }, |
| 140 | + debug('plugin event registered? %o', { |
| 141 | + event, |
| 142 | + isRegistered, |
| 143 | + }) |
131 | 144 |
|
132 | | - register, |
| 145 | + return isRegistered |
| 146 | +} |
133 | 147 |
|
134 | | - has(event) { |
135 | | - const isRegistered = !!registeredEvents[event]; |
| 148 | +const execute = (event, ...args) => { |
| 149 | + debug(`execute plugin event '${event}' Node '${process.version}' with args: %o %o %o`, ...args) |
136 | 150 |
|
137 | | - debug("plugin event registered? %o", { |
138 | | - event, |
139 | | - isRegistered |
140 | | - }); |
| 151 | + return registeredEvents[event](...args) |
| 152 | +} |
141 | 153 |
|
142 | | - return isRegistered; |
143 | | - }, |
| 154 | +const _reset = () => { |
| 155 | + registeredEvents = {} |
| 156 | + handlers = [] |
| 157 | +} |
144 | 158 |
|
145 | | - execute(event, ...args) { |
146 | | - debug(`execute plugin event '${event}' Node '${process.version}' with args: %o %o %o`, ...args); |
147 | | - return registeredEvents[event](...args); |
148 | | - }, |
| 159 | +const _setPluginsProcess = (_pluginsProcess) => { |
| 160 | + pluginsProcess = _pluginsProcess |
| 161 | +} |
149 | 162 |
|
150 | | - //# for testing purposes |
151 | | - _reset() { |
152 | | - registeredEvents = {}; |
153 | | - return handlers = []; |
154 | | - } |
155 | | -}; |
| 163 | +module.exports = { |
| 164 | + getPluginPid, |
| 165 | + execute, |
| 166 | + has, |
| 167 | + init, |
| 168 | + register, |
| 169 | + registerHandler, |
| 170 | + |
| 171 | + // for testing purposes |
| 172 | + _reset, |
| 173 | + _setPluginsProcess, |
| 174 | +} |
0 commit comments