runner openreader - support for plugins

This commit is contained in:
Jan Prochazka 2020-11-22 09:03:16 +01:00
parent 286cac066c
commit 7d1c0c5c18
5 changed files with 42 additions and 21 deletions

View File

@ -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);
}

View File

@ -1,3 +1,4 @@
export * from './commonTypeParser';
export * from './nameTools';
export * from './tableTransforms';
export * from './packageTools';

View File

@ -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}`;
}

View File

@ -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",

View File

@ -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;
}
}