From 7d1c0c5c181ee03c64aac10a5113d500492d57ab Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Sun, 22 Nov 2020 09:03:16 +0100 Subject: [PATCH] runner openreader - support for plugins --- packages/api/src/controllers/runners.js | 15 +++++++++++---- packages/tools/src/index.ts | 1 + packages/tools/src/packageTools.ts | 24 ++++++++++++++++++++++++ packages/web/package.json | 1 + packages/web/src/impexp/ScriptWriter.js | 22 +++++----------------- 5 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 packages/tools/src/packageTools.ts diff --git a/packages/api/src/controllers/runners.js b/packages/api/src/controllers/runners.js index a43e4d61..aaac34d6 100644 --- a/packages/api/src/controllers/runners.js +++ b/packages/api/src/controllers/runners.js @@ -6,6 +6,7 @@ const byline = require('byline'); const socket = require('../utility/socket'); const { fork } = require('child_process'); const { rundir, uploadsdir, pluginsdir } = require('../utility/directories'); +const { extractShellApiPlugins, extractShellApiFunctionName } = require('dbgate-tools'); function extractPlugins(script) { const requireRegex = /\s*\/\/\s*@require\s+([^\s]+)\s*\n/g; @@ -13,11 +14,16 @@ function extractPlugins(script) { return matches.map((x) => x[1]); } +const requirePluginsTemplate = (plugins) => + plugins + .map( + (packageName) => `const ${_.camelCase(packageName)} = require(process.env.PLUGIN_${_.camelCase(packageName)});\n` + ) + .join(''); + const scriptTemplate = (script) => ` const dbgateApi = require(process.env.DBGATE_API); -${extractPlugins(script) - .map((packageName) => `const ${_.camelCase(packageName)} = require(process.env.PLUGIN_${_.camelCase(packageName)});\n`) - .join('')} +${requirePluginsTemplate(extractPlugins(script))} require=null; async function run() { ${script} @@ -29,9 +35,10 @@ dbgateApi.runScript(run); const loaderScriptTemplate = (functionName, props, runid) => ` const dbgateApi = require(process.env.DBGATE_API); +${requirePluginsTemplate(extractShellApiPlugins(functionName, props))} require=null; async function run() { -const reader=await dbgateApi.${functionName}(${JSON.stringify(props)}); +const reader=await ${extractShellApiFunctionName(functionName)}(${JSON.stringify(props)}); const writer=await dbgateApi.collectorWriter({runid: '${runid}'}); await dbgateApi.copyStream(reader, writer); } diff --git a/packages/tools/src/index.ts b/packages/tools/src/index.ts index 3e77feff..9518e5b9 100644 --- a/packages/tools/src/index.ts +++ b/packages/tools/src/index.ts @@ -1,3 +1,4 @@ export * from './commonTypeParser'; export * from './nameTools'; export * from './tableTransforms'; +export * from './packageTools'; diff --git a/packages/tools/src/packageTools.ts b/packages/tools/src/packageTools.ts new file mode 100644 index 00000000..1ce04f70 --- /dev/null +++ b/packages/tools/src/packageTools.ts @@ -0,0 +1,24 @@ +import _ from 'lodash'; + +export function extractShellApiPlugins(functionName, props): string[] { + const res = []; + const nsMatch = functionName.match(/^([^@]+)@([^@]+)/); + if (nsMatch) { + res.push(nsMatch[2]); + } + if (props && props.connection && props.connection.engine) { + const nsMatchEngine = props.connection.engine.match(/^([^@]+)@([^@]+)/); + if (nsMatchEngine) { + res.push(nsMatchEngine[2]); + } + } + return res; +} + +export function extractShellApiFunctionName(functionName) { + const nsMatch = functionName.match(/^([^@]+)@([^@]+)/); + if (nsMatch) { + return `${_.camelCase(nsMatch[2])}.shellApi.${nsMatch[1]}`; + } + return `dbgateApi.${functionName}`; +} diff --git a/packages/web/package.json b/packages/web/package.json index 6d5210e4..01344184 100644 --- a/packages/web/package.json +++ b/packages/web/package.json @@ -14,6 +14,7 @@ "dbgate-datalib": "^1.0.0", "dbgate-engines": "^1.0.0", "dbgate-sqltree": "^1.0.0", + "dbgate-tools": "^1.0.0", "eslint": "^6.8.0", "eslint-plugin-react": "^7.17.0", "formik": "^2.1.0", diff --git a/packages/web/src/impexp/ScriptWriter.js b/packages/web/src/impexp/ScriptWriter.js index 84311e38..acd751ff 100644 --- a/packages/web/src/impexp/ScriptWriter.js +++ b/packages/web/src/impexp/ScriptWriter.js @@ -1,10 +1,11 @@ import _ from 'lodash'; +import { extractShellApiFunctionName, extractShellApiPlugins } from 'dbgate-tools'; export default class ScriptWriter { constructor() { this.s = ''; this.packageNames = []; - this.engines = []; + // this.engines = []; this.varCount = 0; } @@ -19,21 +20,8 @@ export default class ScriptWriter { } assign(variableName, functionName, props) { - const nsMatch = functionName.match(/^([^@]+)@([^@]+)/); - if (nsMatch) { - const packageName = nsMatch[2]; - if (!this.packageNames.includes(packageName)) { - this.packageNames.push(packageName); - } - this.put( - `const ${variableName} = await ${_.camelCase(packageName)}.shellApi.${nsMatch[1]}(${JSON.stringify(props)});` - ); - } else { - this.put(`const ${variableName} = await dbgateApi.${functionName}(${JSON.stringify(props)});`); - } - if (props && props.connection && props.connection.engine && !this.engines.includes(props.connection.engine)) { - this.engines.push(props.connection.engine); - } + this.put(`const ${variableName} = await ${extractShellApiFunctionName(functionName)}(${JSON.stringify(props)});`); + this.packageNames.push(...extractShellApiPlugins(functionName, props)); } copyStream(sourceVar, targetVar) { @@ -54,6 +42,6 @@ export default class ScriptWriter { // this.comment(JSON.stringify(this.engines)); // } const packageNames = this.packageNames; - return packageNames.map((packageName) => `// @require ${packageName}\n`).join('') + '\n' + this.s; + return _.uniq(packageNames).map((packageName) => `// @require ${packageName}\n`).join('') + '\n' + this.s; } }