Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decaffeinate plugins/index.coffee #6545

Merged
merged 3 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 0 additions & 130 deletions packages/server/lib/plugins/index.coffee

This file was deleted.

174 changes: 174 additions & 0 deletions packages/server/lib/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
const _ = require('lodash')
const cp = require('child_process')
const path = require('path')
const debug = require('debug')('cypress:server:plugins')
const Promise = require('bluebird')
const errors = require('../errors')
const util = require('./util')

let pluginsProcess = null
let registeredEvents = {}
let handlers = []

const register = (event, callback) => {
debug(`register event '${event}'`)

if (!_.isString(event)) {
throw new Error(`The plugin register function must be called with an event as its 1st argument. You passed '${event}'.`)
}

if (!_.isFunction(callback)) {
throw new Error(`The plugin register function must be called with a callback function as its 2nd argument. You passed '${callback}'.`)
}

registeredEvents[event] = callback
}

const getPluginPid = () => {
if (pluginsProcess) {
return pluginsProcess.pid
}
}

const registerHandler = (handler) => {
handlers.push(handler)
}

const init = (config, options) => {
debug('plugins.init', config.pluginsFile)

return new Promise((resolve, reject) => {
if (!config.pluginsFile) {
return resolve()
}

if (pluginsProcess) {
debug('kill existing plugins process')
pluginsProcess.kill()
}

registeredEvents = {}

const childIndexFilename = path.join(__dirname, 'child', 'index.js')
const childArguments = ['--file', config.pluginsFile]
const childOptions = {
stdio: 'inherit',
}

if (config.resolvedNodePath) {
debug('launching using custom node version %o', _.pick(config, ['resolvedNodePath', 'resolvedNodeVersion']))
childOptions.execPath = config.resolvedNodePath
}

debug('forking to run %s', childIndexFilename)
pluginsProcess = cp.fork(childIndexFilename, childArguments, childOptions)
const ipc = util.wrapIpc(pluginsProcess)

for (let handler of handlers) {
handler(ipc)
}

ipc.send('load', config)

ipc.on('loaded', (newCfg, registrations) => {
_.each(registrations, (registration) => {
debug('register plugins process event', registration.event, 'with id', registration.eventId)

register(registration.event, (...args) => {
return util.wrapParentPromise(ipc, registration.eventId, (invocationId) => {
debug('call event', registration.event, 'for invocation id', invocationId)
const ids = {
eventId: registration.eventId,
invocationId,
}

ipc.send('execute', registration.event, ids, args)
})
})
})

debug('resolving with new config %o', newCfg)

resolve(newCfg)
})

ipc.on('load:error', (type, ...args) => {
debug('load:error %s, rejecting', type)

reject(errors.get(type, ...args))
})

const killPluginsProcess = () => {
pluginsProcess && pluginsProcess.kill()
pluginsProcess = null
}

const handleError = (err) => {
debug('plugins process error:', err.stack)
if (!pluginsProcess) {
return // prevent repeating this in case of multiple errors
}

killPluginsProcess()
err = errors.get('PLUGINS_ERROR', err.annotated || err.stack || err.message)
err.title = 'Error running plugin'

return options.onError(err)
}

const handleWarning = function (warningErr) {
debug('plugins process warning:', warningErr.stack)
if (!pluginsProcess) {
return // prevent repeating this in case of multiple warnings
}

return options.onWarning(warningErr)
}

pluginsProcess.on('error', handleError)
ipc.on('error', handleError)
ipc.on('warning', handleWarning)

// see timers/parent.js line #93 for why this is necessary
process.on('exit', killPluginsProcess)
})
}

const has = (event) => {
const isRegistered = !!registeredEvents[event]

debug('plugin event registered? %o', {
event,
isRegistered,
})

return isRegistered
}

const execute = (event, ...args) => {
debug(`execute plugin event '${event}' Node '${process.version}' with args: %o %o %o`, ...args)

return registeredEvents[event](...args)
}

const _reset = () => {
registeredEvents = {}
handlers = []
}

const _setPluginsProcess = (_pluginsProcess) => {
pluginsProcess = _pluginsProcess
}

module.exports = {
getPluginPid,
execute,
has,
init,
register,
registerHandler,

// for testing purposes
_reset,
_setPluginsProcess,
}