From 9df6717364d3d1f5af1feab3a0b0dbc43503cb81 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Tue, 28 Nov 2023 00:06:35 +0100 Subject: [PATCH 1/9] Move PluginWebApi to lib Resolve circular dependency of controller depending on web_ui and web_ui depending on controller by moving the type from controller that was used in web_ui to lib. --- packages/controller/src/routes.ts | 14 +------------- packages/lib/browser.js | 1 + packages/lib/index.ts | 1 + packages/lib/src/api.ts | 11 +++++++++++ packages/web_ui/src/bootstrap.tsx | 3 +-- packages/web_ui/src/components/HostsPage.tsx | 3 +-- packages/web_ui/src/components/PluginViewPage.tsx | 2 +- packages/web_ui/src/components/PluginsPage.tsx | 2 +- 8 files changed, 18 insertions(+), 19 deletions(-) create mode 100644 packages/lib/src/api.ts diff --git a/packages/controller/src/routes.ts b/packages/controller/src/routes.ts index d5f9dfbe..45d5dbc4 100644 --- a/packages/controller/src/routes.ts +++ b/packages/controller/src/routes.ts @@ -93,20 +93,8 @@ async function getMetrics(req: Request, res: Response, next: any) { } -export type PluginWebApi = { - name: string; - version: string; - enabled: boolean; - loaded: boolean; - web: { - main: string; - error?: string; - }; - requirePath: string; -} - function getPlugins(req: Request, res: Response) { - let plugins: PluginWebApi[] = []; + let plugins: lib.PluginWebApi[] = []; for (let pluginInfo of req.app.locals.controller.pluginInfos) { let name = pluginInfo.name; let loaded = req.app.locals.controller.plugins.has(name); diff --git a/packages/lib/browser.js b/packages/lib/browser.js index ad4ccaf1..15cd95d5 100644 --- a/packages/lib/browser.js +++ b/packages/lib/browser.js @@ -1,3 +1,4 @@ +export * from "./dist/src/api"; export * from "./dist/src/config"; export * from "./dist/src/data"; export * from "./dist/src/errors"; diff --git a/packages/lib/index.ts b/packages/lib/index.ts index 428086e3..4740305f 100644 --- a/packages/lib/index.ts +++ b/packages/lib/index.ts @@ -3,6 +3,7 @@ * @module lib */ export * from "./build_mod"; +export * from "./src/api"; export * from "./src/command"; export * from "./src/config"; export * from "./src/data"; diff --git a/packages/lib/src/api.ts b/packages/lib/src/api.ts new file mode 100644 index 00000000..b9ecc4d0 --- /dev/null +++ b/packages/lib/src/api.ts @@ -0,0 +1,11 @@ +export type PluginWebApi = { + name: string; + version: string; + enabled: boolean; + loaded: boolean; + web: { + main: string; + error?: string; + }; + requirePath: string; +} diff --git a/packages/web_ui/src/bootstrap.tsx b/packages/web_ui/src/bootstrap.tsx index d97fb4ff..582be9f0 100644 --- a/packages/web_ui/src/bootstrap.tsx +++ b/packages/web_ui/src/bootstrap.tsx @@ -4,7 +4,6 @@ import React from "react"; import { createRoot } from "react-dom/client"; import * as lib from "@clusterio/lib"; -import type { PluginWebApi } from "@clusterio/controller/src/routes"; import App from "./components/App"; import BaseWebPlugin from "./BaseWebPlugin"; @@ -29,7 +28,7 @@ async function loadScript(url: string) { async function loadPluginInfos(): Promise { let response = await fetch(`${window.webRoot}api/plugins`); - let pluginList: PluginWebApi[]; + let pluginList: lib.PluginWebApi[]; if (response.ok) { pluginList = await response.json(); diff --git a/packages/web_ui/src/components/HostsPage.tsx b/packages/web_ui/src/components/HostsPage.tsx index 8b70a713..c59b3c67 100644 --- a/packages/web_ui/src/components/HostsPage.tsx +++ b/packages/web_ui/src/components/HostsPage.tsx @@ -3,7 +3,6 @@ import { useNavigate } from "react-router-dom"; import { Button, Form, InputNumber, Modal, Table, Tag, Typography } from "antd"; import CopyOutlined from "@ant-design/icons/lib/icons/CopyOutlined"; -import type { PluginWebApi } from "@clusterio/controller/src/routes"; import * as lib from "@clusterio/lib"; import { useAccount } from "../model/account"; @@ -23,7 +22,7 @@ function GenerateHostTokenButton() { let [token, setToken] = useState(null); let [hostId, setHostId] = useState(null); let [form] = Form.useForm(); - let [pluginList, setPluginList] = useState([]); + let [pluginList, setPluginList] = useState([]); useEffect(() => { (async () => { let response = await fetch(`${window.webRoot}api/plugins`); diff --git a/packages/web_ui/src/components/PluginViewPage.tsx b/packages/web_ui/src/components/PluginViewPage.tsx index 429a5222..a52e396e 100644 --- a/packages/web_ui/src/components/PluginViewPage.tsx +++ b/packages/web_ui/src/components/PluginViewPage.tsx @@ -2,7 +2,7 @@ import React, { useEffect, useContext, useState } from "react"; import { useParams } from "react-router-dom"; import { Alert, Descriptions, Spin } from "antd"; -import type { PluginWebApi } from "@clusterio/controller/src/routes"; +import type { PluginWebApi } from "@clusterio/lib"; import ControlContext from "./ControlContext"; import PageLayout from "./PageLayout"; diff --git a/packages/web_ui/src/components/PluginsPage.tsx b/packages/web_ui/src/components/PluginsPage.tsx index f47546b1..0902ff84 100644 --- a/packages/web_ui/src/components/PluginsPage.tsx +++ b/packages/web_ui/src/components/PluginsPage.tsx @@ -2,7 +2,7 @@ import React, { useContext, useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; import { Table } from "antd"; -import type { PluginWebApi } from "@clusterio/controller/src/routes"; +import type { PluginWebApi } from "@clusterio/lib"; import notify from "../util/notify"; import ControlContext from "./ControlContext"; From ceb7e48c99eb52beb98cdd5a68a2eadefdd2ef58 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Tue, 28 Nov 2023 00:12:23 +0100 Subject: [PATCH 2/9] Fix global declaration of staticRoot and webRoot These variables were intended to be globals declare in the global namespace. That this ends up on the window object is an implementation detail of JavaScript. Remove the window. prefix put in front of staticRoot and webRoot and include the global declaration in the TypeScript module. --- .eslintrc.js | 7 ++++++- packages/controller/web/index.html | 4 ++-- packages/web_ui/global.d.ts | 8 +++----- packages/web_ui/src/bootstrap.tsx | 6 +++--- packages/web_ui/src/components/App.tsx | 2 +- packages/web_ui/src/components/HostsPage.tsx | 2 +- packages/web_ui/src/components/ModPackViewPage.tsx | 2 +- packages/web_ui/src/components/ModsPage.tsx | 4 ++-- packages/web_ui/src/components/PluginViewPage.tsx | 2 +- packages/web_ui/src/components/PluginsPage.tsx | 2 +- packages/web_ui/src/components/SavesList.tsx | 4 ++-- packages/web_ui/src/index.ts | 1 + packages/web_ui/src/model/item_metadata.ts | 4 ++-- packages/web_ui/src/model/locale.ts | 2 +- 14 files changed, 27 insertions(+), 23 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 084deb91..2cee77d9 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -22,6 +22,7 @@ module.exports = { "@typescript-eslint/no-explicit-any": "off", "@typescript-eslint/no-useless-constructor": "error", "@typescript-eslint/no-unused-vars": "off", + "@typescript-eslint/triple-slash-reference": "off", "prefer-const": "off", }, }, @@ -315,7 +316,11 @@ module.exports = { "space-in-parens": ["error", "never"], "space-infix-ops": "off", "space-unary-ops": "error", - "spaced-comment": ["error", "always"], + "spaced-comment": ["error", "always", { + "line": { + "markers": ["/"], + }, + }], "strict": "error", "switch-colon-spacing": "error", "symbol-description": "error", diff --git a/packages/controller/web/index.html b/packages/controller/web/index.html index fde58715..d8f6f318 100644 --- a/packages/controller/web/index.html +++ b/packages/controller/web/index.html @@ -20,8 +20,8 @@ }
Loading web interface...
- + + diff --git a/packages/controller/web/index.tsx b/packages/controller/web/index.tsx index da298e39..59e6ae18 100644 --- a/packages/controller/web/index.tsx +++ b/packages/controller/web/index.tsx @@ -1,4 +1,5 @@ -/* eslint-disable arrow-body-style, no-console */ -import("@clusterio/web_ui").then(webUi => { - return webUi.bootstrap(); -}).catch(console.error); +declare function scriptError(err: any): void; +import("@clusterio/web_ui") + .then(webUi => webUi.bootstrap()) + .catch(scriptError) +;