From 486587f232190d271b82b576c5d60e398cc67846 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Mon, 29 Jan 2024 01:00:35 +0800 Subject: [PATCH] chore: resolve shared import problem in production move everything into packages --- Dockerfile | 2 +- package.json | 5 +++-- .../monitor/StatusPage/Services.tsx | 5 ++++- src/client/vite.config.ts | 2 +- src/server/main.ts | 10 ++++++++- src/server/model/_schema/index.ts | 1 - src/server/model/_schema/monitor.ts | 12 +++++++++++ src/server/model/monitor/index.ts | 2 +- src/server/router/serverStatus.ts | 5 +---- src/server/trpc/routers/monitor.ts | 8 +++---- src/server/tsconfig.server.json | 2 +- src/server/types/global.d.ts | 10 +++++++++ src/shared/config.ts | 3 --- src/shared/package.json | 13 ++++++++++-- src/shared/src/config.ts | 3 +++ src/shared/{ => src}/date.ts | 0 src/shared/{ => src}/index.ts | 0 src/shared/{ => src}/regex.ts | 0 src/shared/{ => src}/role.ts | 0 src/shared/{ => src}/server.ts | 2 +- src/shared/tsconfig.json | 10 +++++++++ src/types/index.ts | 21 +++---------------- src/types/monitor.ts | 11 ---------- 23 files changed, 75 insertions(+), 52 deletions(-) create mode 100644 src/server/model/_schema/monitor.ts delete mode 100644 src/shared/config.ts create mode 100644 src/shared/src/config.ts rename src/shared/{ => src}/date.ts (100%) rename src/shared/{ => src}/index.ts (100%) rename src/shared/{ => src}/regex.ts (100%) rename src/shared/{ => src}/role.ts (100%) rename src/shared/{ => src}/server.ts (73%) create mode 100644 src/shared/tsconfig.json diff --git a/Dockerfile b/Dockerfile index da6c514..ffda760 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,7 +17,7 @@ RUN pnpm build RUN pip install apprise --break-system-packages # remove unused source file -RUN rm -rf ./src +RUN rm -rf ./src/client RUN rm -rf ./website RUN rm -rf ./reporter diff --git a/package.json b/package.json index 28bc184..aed9c2f 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,9 @@ "dev": "concurrently --kill-others npm:dev:server npm:dev:web", "dev:web": "cd src/client && pnpm dev", "dev:server": "cd src/server && pnpm dev", - "start": "cross-env NODE_ENV=production node ./dist/src/server/main.js", - "start:docker": "pnpm db:migrate:apply && pnpm db:generate && pnpm start", + "start": "cd src/server && cross-env NODE_ENV=production node ./dist/src/server/main.js", + "start:docker": "pnpm start:docker:db && pnpm start", + "start:docker:db": "cd src/server && pnpm db:migrate:apply", "test": "vitest", "build": "pnpm build:tracker && pnpm build:server && pnpm build:client && pnpm build:geo", "build:client": "cd src/client && pnpm build", diff --git a/src/client/components/monitor/StatusPage/Services.tsx b/src/client/components/monitor/StatusPage/Services.tsx index 0ab2a48..669bdbd 100644 --- a/src/client/components/monitor/StatusPage/Services.tsx +++ b/src/client/components/monitor/StatusPage/Services.tsx @@ -7,7 +7,10 @@ import { keyBy } from 'lodash-es'; interface StatusPageServicesProps { workspaceId: string; - monitorList: PrismaJson.MonitorStatusPageList; + monitorList: { + id: string; + showCurrent?: boolean; + }[]; } export const StatusPageServices: React.FC = React.memo( (props) => { diff --git a/src/client/vite.config.ts b/src/client/vite.config.ts index 684df50..1fb6c0e 100644 --- a/src/client/vite.config.ts +++ b/src/client/vite.config.ts @@ -6,7 +6,7 @@ export default defineConfig({ root: __dirname, plugins: [react()], build: { - outDir: '../../dist/src/server/public', + outDir: '../server/public', }, clearScreen: false, server: { diff --git a/src/server/main.ts b/src/server/main.ts index 6abce7e..d387f3e 100644 --- a/src/server/main.ts +++ b/src/server/main.ts @@ -25,6 +25,7 @@ import { initCronjob } from './cronjob'; import { logger } from './utils/logger'; import { monitorRouter } from './router/monitor'; import { healthRouter } from './router/health'; +import path from 'path'; const port = env.port; @@ -44,7 +45,7 @@ app.use(express.json()); app.use(passport.initialize()); app.use(morgan('tiny')); app.use(cors()); -app.use(express.static('dist')); +app.use(express.static('public')); // http://expressjs.com/en/advanced/best-practice-security.html#at-a-minimum-disable-x-powered-by-header app.disable('x-powered-by'); @@ -71,6 +72,13 @@ if (env.allowOpenapi) { app.use('/open', trpcOpenapiHttpHandler); } +// fallback +app.get('/*', (req, res) => { + if (req.accepts('html')) { + res.sendFile(path.join(process.cwd(), 'public', 'index.html')); + } +}); + app.use((err: any, req: any, res: any, next: any) => { logger.error(err); res.status(500).json({ message: err.message }); diff --git a/src/server/model/_schema/index.ts b/src/server/model/_schema/index.ts index 654fed3..78e0a19 100644 --- a/src/server/model/_schema/index.ts +++ b/src/server/model/_schema/index.ts @@ -1,5 +1,4 @@ import { z } from 'zod'; -import { schemas } from '../../../types'; // Match prisma `JsonValue` export const jsonFieldSchema = z.union([ diff --git a/src/server/model/_schema/monitor.ts b/src/server/model/_schema/monitor.ts new file mode 100644 index 0000000..44bb24a --- /dev/null +++ b/src/server/model/_schema/monitor.ts @@ -0,0 +1,12 @@ +import { MonitorInfo } from '../../../types'; +import { MonitorModelSchema } from '../../prisma/zod'; + +export type MonitorInfoWithNotificationIds = MonitorInfo & { + notifications: { id: string }[]; +}; + +export const MonitorPublicInfoSchema = MonitorModelSchema.pick({ + id: true, + name: true, + type: true, +}); diff --git a/src/server/model/monitor/index.ts b/src/server/model/monitor/index.ts index cf6f1a9..c81c67f 100644 --- a/src/server/model/monitor/index.ts +++ b/src/server/model/monitor/index.ts @@ -1,5 +1,5 @@ -import { MonitorPublicInfoSchema } from '../../../types'; import { prisma } from '../_client'; +import { MonitorPublicInfoSchema } from '../_schema/monitor'; import { MonitorManager } from './manager'; export const monitorManager = new MonitorManager(); diff --git a/src/server/router/serverStatus.ts b/src/server/router/serverStatus.ts index a96ba5d..38b0647 100644 --- a/src/server/router/serverStatus.ts +++ b/src/server/router/serverStatus.ts @@ -3,14 +3,11 @@ import { body, header, param, validate } from '../middleware/validate'; import { recordServerStatus } from '../model/serverStatus'; import fs from 'fs-extra'; import path from 'path'; -import { env } from '../utils/env'; export const serverStatusRouter = Router(); const installScript = fs.readFileSync( - env.isProd - ? path.resolve(process.cwd(), './scripts/install.sh') - : path.resolve(process.cwd(), '../../scripts/install.sh') + path.resolve(process.cwd(), '../../scripts/install.sh') ); serverStatusRouter.post( diff --git a/src/server/trpc/routers/monitor.ts b/src/server/trpc/routers/monitor.ts index 3189316..13f6620 100644 --- a/src/server/trpc/routers/monitor.ts +++ b/src/server/trpc/routers/monitor.ts @@ -13,10 +13,6 @@ import { getMonitorRecentData, monitorManager, } from '../../model/monitor'; -import { - MonitorInfoWithNotificationIds, - MonitorPublicInfoSchema, -} from '../../../types'; import dayjs from 'dayjs'; import { monitorEventSchema, @@ -29,6 +25,10 @@ import { OpenApiMeta } from 'trpc-openapi'; import { MonitorStatusPageModelSchema } from '../../prisma/zod'; import { runCodeInVM } from '../../model/monitor/provider/custom'; import { createAuditLog } from '../../model/auditLog'; +import { + MonitorInfoWithNotificationIds, + MonitorPublicInfoSchema, +} from '../../model/_schema/monitor'; export const monitorRouter = router({ all: workspaceProcedure diff --git a/src/server/tsconfig.server.json b/src/server/tsconfig.server.json index 09c6714..31855a5 100644 --- a/src/server/tsconfig.server.json +++ b/src/server/tsconfig.server.json @@ -4,7 +4,7 @@ "exclude": ["node_modules/**/*", "dist"], "compilerOptions": { "rootDirs": ["./", "../"], - "outDir": "../../dist", + "outDir": "./dist", "noEmit": false } } diff --git a/src/server/types/global.d.ts b/src/server/types/global.d.ts index d10dab8..cfee091 100644 --- a/src/server/types/global.d.ts +++ b/src/server/types/global.d.ts @@ -1,7 +1,17 @@ import type { JWTPayload } from '../middleware/auth'; +import type { MonitorStatusPageListSchema } from '../prisma/zod/schemas'; declare global { namespace Express { interface User extends JWTPayload {} } + + namespace PrismaJson { + type CommonPayload = Record; + type DashboardLayout = { + layouts: Record; + items: any[]; + } | null; + type MonitorStatusPageList = z.infer; + } } diff --git a/src/shared/config.ts b/src/shared/config.ts deleted file mode 100644 index a4190bb..0000000 --- a/src/shared/config.ts +++ /dev/null @@ -1,3 +0,0 @@ -import packageJson from '../../package.json'; - -export const version = packageJson.version; diff --git a/src/shared/package.json b/src/shared/package.json index 0dd334e..78754ac 100644 --- a/src/shared/package.json +++ b/src/shared/package.json @@ -3,8 +3,17 @@ "version": "1.0.0", "private": true, "description": "", - "main": "index.ts", - "scripts": {}, + "types": "./dist/cjs/src/shared/src/index.d.ts", + "exports": { + "import": "./dist/esm/src/shared/src/index.js", + "require": "./dist/cjs/src/shared/src/index.js" + }, + "scripts": { + "build": "pnpm build:cjs && pnpm build:esm", + "build:cjs": "tsc --module commonjs --outDir ./dist/cjs", + "build:esm": "tsc --module esnext --outDir ./dist/esm", + "postinstall": "pnpm build" + }, "keywords": [], "author": "moonrailgun ", "dependencies": { diff --git a/src/shared/src/config.ts b/src/shared/src/config.ts new file mode 100644 index 0000000..5ebe656 --- /dev/null +++ b/src/shared/src/config.ts @@ -0,0 +1,3 @@ +import packageJson from '../../../package.json'; + +export const version = packageJson.version; diff --git a/src/shared/date.ts b/src/shared/src/date.ts similarity index 100% rename from src/shared/date.ts rename to src/shared/src/date.ts diff --git a/src/shared/index.ts b/src/shared/src/index.ts similarity index 100% rename from src/shared/index.ts rename to src/shared/src/index.ts diff --git a/src/shared/regex.ts b/src/shared/src/regex.ts similarity index 100% rename from src/shared/regex.ts rename to src/shared/src/regex.ts diff --git a/src/shared/role.ts b/src/shared/src/role.ts similarity index 100% rename from src/shared/role.ts rename to src/shared/src/role.ts diff --git a/src/shared/server.ts b/src/shared/src/server.ts similarity index 73% rename from src/shared/server.ts rename to src/shared/src/server.ts index e27e279..2c346ee 100644 --- a/src/shared/server.ts +++ b/src/shared/src/server.ts @@ -1,4 +1,4 @@ -import { ServerStatusInfo } from '../types'; +import type { ServerStatusInfo } from '../../types'; export function isServerOnline(info: ServerStatusInfo): boolean { return new Date(info.updatedAt).valueOf() + info.timeout * 1000 > Date.now(); diff --git a/src/shared/tsconfig.json b/src/shared/tsconfig.json new file mode 100644 index 0000000..e7adbf2 --- /dev/null +++ b/src/shared/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": "./dist", + "declaration": true, + "declarationMap": true, + "noEmit": false + }, + "include": ["./src/**/*"] +} diff --git a/src/types/index.ts b/src/types/index.ts index bdf5ff1..e7f5d5e 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,18 +1,3 @@ -import { z } from 'zod'; -import { MonitorStatusPageListSchema } from '../server/prisma/zod/schemas'; - -export * as schemas from '../server/prisma/zod/index'; -export * from './server'; -export * from './monitor'; -export * from './utils'; - -declare global { - namespace PrismaJson { - type CommonPayload = Record; - type DashboardLayout = { - layouts: Record; - items: any[]; - } | null; - type MonitorStatusPageList = z.infer; - } -} +export type * from './server'; +export type * from './monitor'; +export type * from './utils'; diff --git a/src/types/monitor.ts b/src/types/monitor.ts index 50e2a71..b95517f 100644 --- a/src/types/monitor.ts +++ b/src/types/monitor.ts @@ -1,6 +1,5 @@ import type { MonitorModelSchema } from '../server/prisma/zod'; import { ExactType } from './utils'; -import { schemas } from '.'; import z from 'zod'; type Monitor = z.infer; @@ -11,13 +10,3 @@ export type MonitorInfo = ExactType< payload: Record; } >; - -export type MonitorInfoWithNotificationIds = MonitorInfo & { - notifications: { id: string }[]; -}; - -export const MonitorPublicInfoSchema = schemas.MonitorModelSchema.pick({ - id: true, - name: true, - type: true, -});