2022-03-03 10:26:15 +00:00
|
|
|
import { createApp } from "vue";
|
|
|
|
import { createPinia } from "pinia";
|
|
|
|
import App from "./App.vue";
|
|
|
|
import router from "./router";
|
2022-06-24 15:05:49 +00:00
|
|
|
import type {
|
|
|
|
MenuGroupType,
|
|
|
|
MenuItemType,
|
|
|
|
Plugin,
|
|
|
|
} from "@halo-dev/admin-shared";
|
2022-07-13 07:36:21 +00:00
|
|
|
import { apiClient } from "@halo-dev/admin-shared";
|
2022-06-24 15:05:49 +00:00
|
|
|
import { menus, minimenus, registerMenu } from "./router/menus.config";
|
2022-06-16 09:01:18 +00:00
|
|
|
// setup
|
|
|
|
import "./setup/setupStyles";
|
|
|
|
import { setupComponents } from "./setup/setupComponents";
|
2022-06-17 06:12:15 +00:00
|
|
|
|
2022-06-20 04:25:36 +00:00
|
|
|
// core modules
|
|
|
|
import { coreModules } from "./modules";
|
2022-06-23 03:28:23 +00:00
|
|
|
import { useScriptTag } from "@vueuse/core";
|
|
|
|
import { usePluginStore } from "@/stores/plugin";
|
2022-07-13 07:36:21 +00:00
|
|
|
import type { User } from "@halo-dev/api-client";
|
2022-06-17 06:12:15 +00:00
|
|
|
|
|
|
|
const app = createApp(App);
|
2022-03-03 10:26:15 +00:00
|
|
|
|
2022-06-16 09:01:18 +00:00
|
|
|
setupComponents(app);
|
2022-05-26 09:37:08 +00:00
|
|
|
|
2022-06-16 09:01:18 +00:00
|
|
|
app.use(createPinia());
|
2022-05-26 09:37:08 +00:00
|
|
|
|
2022-06-20 04:25:36 +00:00
|
|
|
function registerModule(pluginModule: Plugin) {
|
2022-06-17 06:12:15 +00:00
|
|
|
if (pluginModule.components) {
|
2022-06-20 04:25:36 +00:00
|
|
|
if (!Array.isArray(pluginModule.components)) {
|
|
|
|
console.error(`${pluginModule.name}: Plugin components must be an array`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-17 06:12:15 +00:00
|
|
|
for (const component of pluginModule.components) {
|
|
|
|
component.name && app.component(component.name, component);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pluginModule.routes) {
|
2022-06-20 04:25:36 +00:00
|
|
|
if (!Array.isArray(pluginModule.routes)) {
|
|
|
|
console.error(`${pluginModule.name}: Plugin routes must be an array`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-17 06:12:15 +00:00
|
|
|
for (const route of pluginModule.routes) {
|
|
|
|
router.addRoute(route);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pluginModule.menus) {
|
2022-06-20 04:25:36 +00:00
|
|
|
if (!Array.isArray(pluginModule.menus)) {
|
|
|
|
console.error(`${pluginModule.name}: Plugin menus must be an array`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-17 06:12:15 +00:00
|
|
|
for (const group of pluginModule.menus) {
|
|
|
|
for (const menu of group.items) {
|
|
|
|
registerMenu(group.name, menu);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadCoreModules() {
|
2022-06-20 04:25:36 +00:00
|
|
|
coreModules.forEach(registerModule);
|
2022-06-17 06:12:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-23 03:28:23 +00:00
|
|
|
const pluginStore = usePluginStore();
|
|
|
|
|
2022-06-23 13:22:24 +00:00
|
|
|
function loadStyle(href: string) {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
let shouldAppend = false;
|
|
|
|
let el: HTMLLinkElement | null = document.querySelector(
|
|
|
|
'script[src="' + href + '"]'
|
|
|
|
);
|
|
|
|
if (!el) {
|
|
|
|
el = document.createElement("link");
|
|
|
|
el.rel = "stylesheet";
|
|
|
|
el.type = "text/css";
|
|
|
|
el.href = href;
|
|
|
|
shouldAppend = true;
|
|
|
|
} else if (el.hasAttribute("data-loaded")) {
|
|
|
|
resolve(el);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
el.addEventListener("error", reject);
|
|
|
|
el.addEventListener("abort", reject);
|
|
|
|
el.addEventListener("load", function loadStyleHandler() {
|
|
|
|
el?.setAttribute("data-loaded", "true");
|
|
|
|
resolve(el);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (shouldAppend) document.head.prepend(el);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-20 07:08:17 +00:00
|
|
|
async function loadPluginModules() {
|
2022-07-14 08:48:54 +00:00
|
|
|
const { data } =
|
2022-07-13 07:36:21 +00:00
|
|
|
await apiClient.extension.plugin.listpluginHaloRunV1alpha1Plugin();
|
2022-06-23 03:28:23 +00:00
|
|
|
|
|
|
|
// Get all started plugins
|
2022-07-14 08:48:54 +00:00
|
|
|
const plugins = data.items.filter(
|
2022-07-13 07:36:21 +00:00
|
|
|
(plugin) => plugin.status?.phase === "STARTED" && plugin.spec.enabled
|
2022-06-23 03:28:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
for (const plugin of plugins) {
|
2022-07-13 07:36:21 +00:00
|
|
|
const { entry, stylesheet } = plugin.status || {
|
|
|
|
entry: "",
|
|
|
|
stylesheet: "",
|
|
|
|
};
|
2022-06-23 03:28:23 +00:00
|
|
|
|
|
|
|
if (entry) {
|
|
|
|
const { load } = useScriptTag(
|
2022-07-13 07:36:21 +00:00
|
|
|
`http://localhost:8090${plugin.status?.entry}`
|
2022-06-23 03:28:23 +00:00
|
|
|
);
|
|
|
|
await load();
|
|
|
|
const pluginModule = window[plugin.metadata.name];
|
|
|
|
|
|
|
|
if (pluginModule) {
|
|
|
|
// @ts-ignore
|
|
|
|
plugin.spec.module = pluginModule;
|
|
|
|
registerModule(pluginModule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-23 13:22:24 +00:00
|
|
|
if (stylesheet) {
|
2022-07-13 09:39:31 +00:00
|
|
|
try {
|
|
|
|
await loadStyle(`http://localhost:8090${stylesheet}`);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2022-06-23 13:22:24 +00:00
|
|
|
}
|
|
|
|
|
2022-06-23 03:28:23 +00:00
|
|
|
pluginStore.registerPlugin(plugin);
|
|
|
|
}
|
2022-06-17 06:12:15 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 04:33:00 +00:00
|
|
|
async function loadCurrentUser() {
|
2022-07-14 08:48:54 +00:00
|
|
|
const { data: user } = await apiClient.user.getCurrentUserDetail();
|
|
|
|
app.provide<User>("currentUser", user);
|
|
|
|
|
|
|
|
const { data: permissions } = await apiClient.user.getPermissions(
|
2022-07-14 10:39:19 +00:00
|
|
|
user.metadata.name
|
2022-07-14 08:48:54 +00:00
|
|
|
);
|
|
|
|
app.provide("permissions", permissions);
|
2022-07-04 04:33:00 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 07:08:17 +00:00
|
|
|
(async function () {
|
|
|
|
await initApp();
|
|
|
|
})();
|
2022-06-17 06:12:15 +00:00
|
|
|
|
|
|
|
async function initApp() {
|
2022-06-20 04:25:36 +00:00
|
|
|
try {
|
|
|
|
loadCoreModules();
|
2022-06-20 07:08:17 +00:00
|
|
|
await loadPluginModules();
|
2022-07-04 04:33:00 +00:00
|
|
|
await loadCurrentUser();
|
2022-06-24 15:05:49 +00:00
|
|
|
app.provide<MenuGroupType[]>("menus", menus);
|
|
|
|
app.provide<MenuItemType[]>("minimenus", minimenus);
|
2022-06-20 04:25:36 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
2022-06-23 03:28:23 +00:00
|
|
|
} finally {
|
|
|
|
app.use(router);
|
|
|
|
app.mount("#app");
|
2022-06-20 04:25:36 +00:00
|
|
|
}
|
2022-06-17 06:12:15 +00:00
|
|
|
}
|