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