From c15f859eae59fa89c0614384ace87788bbe92825 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Mon, 15 Feb 2021 06:40:41 +0100 Subject: [PATCH] #63 - clean solution - added ...process.env to fork(env) --- packages/api/src/controllers/runners.js | 41 ++++++------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/packages/api/src/controllers/runners.js b/packages/api/src/controllers/runners.js index ab2dec7e..db789e66 100644 --- a/packages/api/src/controllers/runners.js +++ b/packages/api/src/controllers/runners.js @@ -8,7 +8,6 @@ const { fork } = require('child_process'); const { rundir, uploadsdir, pluginsdir } = require('../utility/directories'); const { extractShellApiPlugins, extractShellApiFunctionName } = require('dbgate-tools'); const { handleProcessCommunication } = require('../utility/processComm'); -const platformInfo = require('../utility/platformInfo'); function extractPlugins(script) { const requireRegex = /\s*\/\/\s*@require\s+([^\s]+)\s*\n/g; @@ -23,20 +22,7 @@ const requirePluginsTemplate = plugins => ) .join('') + `dbgateApi.registerPlugins(${plugins.map(x => _.camelCase(x)).join(',')});\n`; -// workaround - not working env in SNAP -const expandEnvParamsScript = platformInfo.isSnap - ? ` -for(let i=0;i ` -${expandEnvParamsScript}; const dbgateApi = require(process.env.DBGATE_API); dbgateApi.initializeApiEnvironment(); ${requirePluginsTemplate(extractPlugins(script))} @@ -50,7 +36,6 @@ dbgateApi.runScript(run); `; const loaderScriptTemplate = (functionName, props, runid) => ` -${expandEnvParamsScript}; const dbgateApi = require(process.env.DBGATE_API); dbgateApi.initializeApiEnvironment(); ${requirePluginsTemplate(extractShellApiPlugins(functionName, props))} @@ -63,10 +48,6 @@ await dbgateApi.copyStream(reader, writer); dbgateApi.runScript(run); `; -function envToParams(env) { - return _.flatten(_.keys(env).map(key => [`--env.${key}`, env[key]])); -} - module.exports = { /** @type {import('dbgate-types').OpenedRunner[]} */ opened: [], @@ -114,19 +95,15 @@ module.exports = { const pluginNames = fs.readdirSync(pluginsdir()); console.log(`RUNNING SCRIPT ${scriptFile}`); // const subprocess = fork(scriptFile, ['--checkParent', '--max-old-space-size=8192'], { - const env = { - DBGATE_API: global['dbgateApiModulePath'] || process.argv[1], - ..._.fromPairs(pluginNames.map(name => [`PLUGIN_${_.camelCase(name)}`, path.join(pluginsdir(), name)])), - }; - const subprocess = fork( - scriptFile, - ['--checkParent', ...process.argv.slice(3), ...(platformInfo.isSnap ? envToParams(env) : [])], - { - cwd: directory, - stdio: ['ignore', 'pipe', 'pipe', 'ipc'], - env: platformInfo.isSnap ? undefined : env, - } - ); + const subprocess = fork(scriptFile, ['--checkParent', ...process.argv.slice(3)], { + cwd: directory, + stdio: ['ignore', 'pipe', 'pipe', 'ipc'], + env: { + ...process.env, + DBGATE_API: global['dbgateApiModulePath'] || process.argv[1], + ..._.fromPairs(pluginNames.map(name => [`PLUGIN_${_.camelCase(name)}`, path.join(pluginsdir(), name)])), + }, + }); const pipeDispatcher = severity => data => this.dispatchMessage(runid, { severity, message: data.toString().trim() });