refactor(frontend/aiscript): AiScriptバージョン取得・判定ロジックを統一 (#16845)
* refactor(frontend): AiScriptバージョン取得・判定ロジックを統一 * fix
This commit is contained in:
parent
4edd6a68e6
commit
91dafc26a7
|
|
@ -6,13 +6,6 @@
|
||||||
import { errors, utils } from '@syuilo/aiscript';
|
import { errors, utils } from '@syuilo/aiscript';
|
||||||
import type { values } from '@syuilo/aiscript';
|
import type { values } from '@syuilo/aiscript';
|
||||||
|
|
||||||
const extractVersionIdentifier = /^\/\/\/\s*@\s*(\d+)\.(\d+)\.\d+$/m;
|
|
||||||
|
|
||||||
export function getAiScriptVersion(script: string): { major: number; minor: number } | undefined {
|
|
||||||
const match = extractVersionIdentifier.exec(script);
|
|
||||||
return match ? { major: Number(match[1]), minor: Number(match[2]) } : undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function assertStringAndIsIn<A extends readonly string[]>(value: values.Value | undefined, expects: A): asserts value is values.VStr & { value: A[number] } {
|
export function assertStringAndIsIn<A extends readonly string[]>(value: values.Value | undefined, expects: A): asserts value is values.VStr & { value: A[number] } {
|
||||||
utils.assertString(value);
|
utils.assertString(value);
|
||||||
const str = value.value;
|
const str = value.value;
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,8 @@ SPDX-License-Identifier: AGPL-3.0-only
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { computed, onDeactivated, onUnmounted, ref, watch, shallowRef, defineAsyncComponent } from 'vue';
|
import { computed, onDeactivated, onUnmounted, ref, watch, shallowRef, defineAsyncComponent } from 'vue';
|
||||||
import * as Misskey from 'misskey-js';
|
import * as Misskey from 'misskey-js';
|
||||||
|
import { utils } from '@syuilo/aiscript';
|
||||||
|
import { compareVersions } from 'compare-versions';
|
||||||
import { url } from '@@/js/config.js';
|
import { url } from '@@/js/config.js';
|
||||||
import type { Ref } from 'vue';
|
import type { Ref } from 'vue';
|
||||||
import type { AsUiComponent, AsUiRoot } from '@/aiscript/ui.js';
|
import type { AsUiComponent, AsUiRoot } from '@/aiscript/ui.js';
|
||||||
|
|
@ -74,7 +76,6 @@ import { misskeyApi } from '@/utility/misskey-api.js';
|
||||||
import { i18n } from '@/i18n.js';
|
import { i18n } from '@/i18n.js';
|
||||||
import { definePage } from '@/page.js';
|
import { definePage } from '@/page.js';
|
||||||
import MkAsUi from '@/components/MkAsUi.vue';
|
import MkAsUi from '@/components/MkAsUi.vue';
|
||||||
import { getAiScriptVersion } from '@/aiscript/common.js';
|
|
||||||
import { registerAsUiLib } from '@/aiscript/ui.js';
|
import { registerAsUiLib } from '@/aiscript/ui.js';
|
||||||
import { aiScriptReadline, createAiScriptEnv } from '@/aiscript/api.js';
|
import { aiScriptReadline, createAiScriptEnv } from '@/aiscript/api.js';
|
||||||
import MkFolder from '@/components/MkFolder.vue';
|
import MkFolder from '@/components/MkFolder.vue';
|
||||||
|
|
@ -191,12 +192,21 @@ function start() {
|
||||||
run();
|
run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getIsLegacy(version: string | null): boolean {
|
||||||
|
if (version == null) return false;
|
||||||
|
try {
|
||||||
|
return compareVersions(version, '1.0.0') < 0;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function run() {
|
async function run() {
|
||||||
if (aiscript.value) aiscript.value.abort();
|
if (aiscript.value) aiscript.value.abort();
|
||||||
if (!flash.value) return;
|
if (!flash.value) return;
|
||||||
|
|
||||||
const version = getAiScriptVersion(flash.value.script);
|
const version = utils.getLangVersion(flash.value.script);
|
||||||
const isLegacy = version ? version.major < 1 : false;
|
const isLegacy = version != null && getIsLegacy(version);
|
||||||
|
|
||||||
const { Interpreter, Parser, values } = isLegacy ? (await import('@syuilo/aiscript-0-19-0') as any) : await import('@syuilo/aiscript');
|
const { Interpreter, Parser, values } = isLegacy ? (await import('@syuilo/aiscript-0-19-0') as any) : await import('@syuilo/aiscript');
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue