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]); return matches.map(x => x[1]);
} }
const requirePluginsTemplate = plugins => const requirePluginsTemplate = (plugins, isExport) =>
plugins plugins
.map( .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`; .join('') + `dbgateApi.registerPlugins(${plugins.map(x => _.camelCase(x)).join(',')});\n`;
const scriptTemplate = script => ` const scriptTemplate = (script, isExport) => `
const dbgateApi = require(process.env.DBGATE_API); const dbgateApi = require(${isExport ? `'dbgate-api'` : 'process.env.DBGATE_API'});
dbgateApi.initializeApiEnvironment(); dbgateApi.initializeApiEnvironment();
${requirePluginsTemplate(extractPlugins(script))} ${requirePluginsTemplate(extractPlugins(script), isExport)}
require=null; require=null;
async function run() { async function run() {
${script} ${script}
@@ -139,7 +142,12 @@ module.exports = {
start_meta: 'post', start_meta: 'post',
async start({ script }) { async start({ script }) {
const runid = uuidv1(); 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', cancel_meta: 'post',

View File

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

View File

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