shell script runner

This commit is contained in:
Jan Prochazka
2020-06-07 09:51:53 +02:00
parent 617548cd50
commit f37524f76f
14 changed files with 203 additions and 89 deletions

View File

@@ -1,49 +0,0 @@
import ScriptWriter from './ScriptWriter';
export default class ScriptCreator {
constructor() {
this.varCount = 0;
this.commands = [];
}
allocVariable(prefix = 'var') {
this.varCount += 1;
return `${prefix}${this.varCount}`;
}
getCode() {
const writer = new ScriptWriter();
for (const command of this.commands) {
const { type } = command;
switch (type) {
case 'assign':
{
const { variableName, functionName, props } = command;
writer.assign(variableName, functionName, props);
}
break;
case 'copyStream':
{
const { sourceVar, targetVar } = command;
writer.copyStream(sourceVar, targetVar);
}
break;
}
}
writer.finish();
return writer.s;
}
assign(variableName, functionName, props) {
this.commands.push({
type: 'assign',
variableName,
functionName,
props,
});
}
copyStream(sourceVar, targetVar) {
this.commands.push({
type: 'copyStream',
sourceVar,
targetVar,
});
}
}

View File

@@ -1,9 +1,12 @@
export default class ScriptWriter {
constructor() {
this.s = '';
this.put('const dbgateApi = require("@dbgate/api");');
this.put();
this.put('async function run() {');
this.varCount = 0;
}
allocVariable(prefix = 'var') {
this.varCount += 1;
return `${prefix}${this.varCount}`;
}
put(s = '') {
@@ -11,17 +14,11 @@ export default class ScriptWriter {
this.s += '\n';
}
finish() {
this.put('}');
this.put();
this.put('dbgateApi.runScript(run);');
}
assign(variableName, functionName, props) {
this.put(` const ${variableName} = await dbgateApi.${functionName}(${JSON.stringify(props)});`);
this.put(`const ${variableName} = await dbgateApi.${functionName}(${JSON.stringify(props)});`);
}
copyStream(sourceVar, targetVar) {
this.put(` await dbgateApi.copyStream(${sourceVar}, ${targetVar});`);
this.put(`await dbgateApi.copyStream(${sourceVar}, ${targetVar});`);
}
}

View File

@@ -1,12 +1,12 @@
import _ from 'lodash';
import ScriptCreator from './ScriptCreator';
import ScriptWriter from './ScriptWriter';
import getAsArray from '../utility/getAsArray';
import { getConnectionInfo } from '../utility/metadataLoaders';
import engines from '@dbgate/engines';
import { quoteFullName, fullNameFromString } from '@dbgate/datalib';
export default async function createImpExpScript(values) {
const script = new ScriptCreator();
const script = new ScriptWriter();
if (values.sourceStorageType == 'database') {
const tables = getAsArray(values.sourceTables);
for (const table of tables) {
@@ -29,7 +29,8 @@ export default async function createImpExpScript(values) {
});
script.copyStream(sourceVar, targetVar);
script.put();
}
}
return script.getCode();
return script.s;
}