mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-25 00:36:00 +00:00
get node script from shell script
This commit is contained in:
@@ -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',
|
||||
|
||||
@@ -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'),
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user