get node script from shell script

This commit is contained in:
Jan Prochazka
2021-10-17 09:50:32 +02:00
parent 5ed441aada
commit ee1c51e9f8
3 changed files with 44 additions and 17 deletions

View File

@@ -15,17 +15,20 @@ function extractPlugins(script) {
return matches.map(x => x[1]);
}
const requirePluginsTemplate = plugins =>
const requirePluginsTemplate = (plugins, isExport) =>
plugins
.map(
packageName => `const ${_.camelCase(packageName)} = require(process.env.PLUGIN_${_.camelCase(packageName)});\n`
packageName =>
`const ${_.camelCase(packageName)} = require(${
isExport ? `'${packageName}'` : `process.env.PLUGIN_${_.camelCase(packageName)}`
});\n`
)
.join('') + `dbgateApi.registerPlugins(${plugins.map(x => _.camelCase(x)).join(',')});\n`;
const scriptTemplate = script => `
const dbgateApi = require(process.env.DBGATE_API);
const scriptTemplate = (script, isExport) => `
const dbgateApi = require(${isExport ? `'dbgate-api'` : 'process.env.DBGATE_API'});
dbgateApi.initializeApiEnvironment();
${requirePluginsTemplate(extractPlugins(script))}
${requirePluginsTemplate(extractPlugins(script), isExport)}
require=null;
async function run() {
${script}
@@ -139,7 +142,12 @@ module.exports = {
start_meta: 'post',
async start({ script }) {
const runid = uuidv1();
return this.startCore(runid, scriptTemplate(script));
return this.startCore(runid, scriptTemplate(script, false));
},
getNodeScript_meta: 'post',
async getNodeScript({ script }) {
return scriptTemplate(script, true);
},
cancel_meta: 'post',

View File

@@ -5,7 +5,7 @@
<script lang="ts">
import _ from 'lodash';
import { filterName } from 'dbgate-tools';
import { extractPackageName, filterName } from 'dbgate-tools';
import { currentArchive, currentDatabase } from '../stores';
@@ -28,7 +28,9 @@
tabComponent: 'ShellTab',
},
{
editor: `await dbgateApi.deployDb(${JSON.stringify(
editor: `// @require ${extractPackageName($currentDatabase.connection.engine)}
await dbgateApi.deployDb(${JSON.stringify(
{
connection: {
..._.omit($currentDatabase.connection, '_id', 'displayName'),

View File

@@ -14,6 +14,14 @@
findReplace: true,
});
registerCommand({
id: 'shell.copyNodeScript',
category: 'Shell',
name: 'Copy nodejs script',
testEnabled: () => getCurrentEditor() != null,
onClick: () => getCurrentEditor().copyNodeScript(),
});
// registerCommand({
// id: 'shell.openWizard',
// category: 'Shell',
@@ -25,7 +33,6 @@
const configRegex = /\s*\/\/\s*@ImportExportConfigurator\s*\n\s*\/\/\s*(\{[^\n]+\})\n/;
const requireRegex = /\s*(\/\/\s*@require\s+[^\n]+)\n/g;
const initRegex = /([^\n]+\/\/\s*@init)/g;
</script>
<script lang="ts">
@@ -42,6 +49,7 @@
import RunnerOutputPane from '../query/RunnerOutputPane.svelte';
import useEditorData from '../query/useEditorData';
import axiosInstance from '../utility/axiosInstance';
import { copyTextToClipboard } from '../utility/clipboard';
import { changeTab } from '../utility/common';
import createActivator, { getActiveComponent } from '../utility/createActivator';
import { showSnackbarError } from '../utility/snackbar';
@@ -131,6 +139,11 @@
return busy;
}
export async function copyNodeScript() {
const resp = await axiosInstance.post('runners/get-node-script', { script: getActiveScript() });
copyTextToClipboard(resp.data);
}
// export function openWizardEnabled() {
// return ($editorValue || '').match(configRegex);
// }
@@ -144,19 +157,23 @@
}
}
function getActiveScript() {
const selectedText = domEditor.getEditor().getSelectedText();
const editorText = $editorValue;
return selectedText
? [...(editorText || '').matchAll(requireRegex)].map(x => `${x[1]}\n`).join('') +
[...(editorText || '').matchAll(initRegex)].map(x => `${x[1]}\n`).join('') +
selectedText
: editorText;
}
export async function execute() {
if (busy) return;
executeNumber += 1;
const selectedText = domEditor.getEditor().getSelectedText();
const editorText = $editorValue;
let runid = runnerId;
const resp = await axiosInstance.post('runners/start', {
script: selectedText
? [...(editorText || '').matchAll(requireRegex)].map(x => `${x[1]}\n`).join('') +
[...(editorText || '').matchAll(initRegex)].map(x => `${x[1]}\n`).join('') +
selectedText
: editorText,
script: getActiveScript(),
});
runid = resp.data.runid;
runnerId = runid;
@@ -187,12 +204,12 @@
{ divider: true },
{ command: 'shell.save' },
{ command: 'shell.saveAs' },
{ command: 'shell.copyNodeScript' },
{ divider: true },
{ command: 'shell.find' },
{ command: 'shell.replace' },
];
}
</script>
<VerticalSplitter>