Sharkey/packages/frontend/src/pages/settings/plugin.install.vue

142 lines
3.1 KiB
Vue
Raw Normal View History

2021-02-06 14:05:00 +02:00
<template>
2023-01-05 14:04:56 +02:00
<div class="_autoGap">
<FormInfo warn>{{ i18n.ts._plugin.installWarn }}</FormInfo>
2021-02-06 14:05:00 +02:00
2023-01-05 14:04:56 +02:00
<FormTextarea v-model="code" tall>
<template #label>{{ i18n.ts.code }}</template>
2022-01-04 10:52:44 +02:00
</FormTextarea>
2021-02-06 14:05:00 +02:00
2023-01-05 14:04:56 +02:00
<div>
2023-01-06 02:41:14 +02:00
<MkButton :disabled="code == null" primary inline @click="install"><i class="ti ti-check"></i> {{ i18n.ts.install }}</MkButton>
2022-01-04 10:52:44 +02:00
</div>
</div>
2021-02-06 14:05:00 +02:00
</template>
<script lang="ts" setup>
import { defineAsyncComponent, nextTick, ref } from 'vue';
2023-01-03 08:51:49 +02:00
import { Interpreter, Parser, utils } from '@syuilo/aiscript';
2021-02-06 14:05:00 +02:00
import { v4 as uuid } from 'uuid';
2021-11-11 19:02:25 +02:00
import FormTextarea from '@/components/form/textarea.vue';
2023-01-06 02:41:14 +02:00
import MkButton from '@/components/MkButton.vue';
import FormInfo from '@/components/MkInfo.vue';
2021-11-11 19:02:25 +02:00
import * as os from '@/os';
import { ColdDeviceStorage } from '@/store';
import { unisonReload } from '@/scripts/unison-reload';
import { i18n } from '@/i18n';
import { definePageMetadata } from '@/scripts/page-metadata';
2021-02-06 14:05:00 +02:00
2023-01-03 08:51:49 +02:00
const parser = new Parser();
const code = ref(null);
2021-02-06 14:05:00 +02:00
2023-01-03 08:51:49 +02:00
function installPlugin({ id, meta, src, token }) {
ColdDeviceStorage.set('plugins', ColdDeviceStorage.get('plugins').concat({
...meta,
id,
active: true,
configData: {},
token: token,
2023-01-03 08:51:49 +02:00
src: src,
}));
}
async function install() {
2023-01-03 08:51:49 +02:00
if (code.value == null) return;
const lv = utils.getLangVersion(code.value);
if (lv == null) {
os.alert({
type: 'error',
text: 'No language version annotation found :(',
});
return;
2023-01-05 09:57:49 +02:00
} else if (!lv.startsWith('0.12.')) {
2023-01-03 08:51:49 +02:00
os.alert({
type: 'error',
text: `aiscript version '${lv}' is not supported :(`,
});
return;
}
let ast;
try {
2023-01-03 08:51:49 +02:00
ast = parser.parse(code.value);
} catch (err) {
os.alert({
type: 'error',
text: 'Syntax error :(',
});
return;
}
2023-01-03 08:51:49 +02:00
const meta = Interpreter.collectMetadata(ast);
if (meta == null) {
os.alert({
type: 'error',
text: 'No metadata found :(',
});
return;
}
const metadata = meta.get(null);
if (metadata == null) {
os.alert({
type: 'error',
text: 'No metadata found :(',
});
return;
}
const { name, version, author, description, permissions, config } = metadata;
if (name == null || version == null || author == null) {
os.alert({
type: 'error',
text: 'Required property not found :(',
});
return;
}
const token = permissions == null || permissions.length === 0 ? null : await new Promise((res, rej) => {
os.popup(defineAsyncComponent(() => import('@/components/MkTokenGenerateWindow.vue')), {
title: i18n.ts.tokenRequested,
information: i18n.ts.pluginTokenRequestedDescription,
initialName: name,
initialPermissions: permissions,
}, {
done: async result => {
const { name, permissions } = result;
const { token } = await os.api('miauth/gen-token', {
session: null,
name: name,
permission: permissions,
2021-02-06 14:05:00 +02:00
});
res(token);
},
}, 'closed');
});
installPlugin({
id: uuid(),
meta: {
name, version, author, description, permissions, config,
},
token,
2023-01-03 08:51:49 +02:00
src: code.value,
});
os.success();
nextTick(() => {
unisonReload();
});
}
const headerActions = $computed(() => []);
const headerTabs = $computed(() => []);
definePageMetadata({
title: i18n.ts._plugin.install,
icon: 'ti ti-download',
2021-02-06 14:05:00 +02:00
});
</script>