refactor: add shared packacage

This commit is contained in:
moonrailgun 2024-01-24 13:26:42 +00:00
parent d2171ce2b3
commit 39550ece83
23 changed files with 55 additions and 22 deletions

View File

@ -5,13 +5,13 @@
"scripts": { "scripts": {
"dev": "concurrently --kill-others npm:dev:server npm:dev:web", "dev": "concurrently --kill-others npm:dev:server npm:dev:web",
"dev:web": "cd src/client && pnpm dev", "dev:web": "cd src/client && pnpm dev",
"dev:server": "cd src/server && pnpm dev", "dev:server": "pnpm run dev --filer ./src/server",
"start": "cross-env NODE_ENV=production node ./dist/src/server/main.js", "start": "cross-env NODE_ENV=production node ./dist/src/server/main.js",
"start:docker": "pnpm db:migrate:apply && pnpm db:generate && pnpm start", "start:docker": "pnpm db:migrate:apply && pnpm db:generate && pnpm start",
"test": "vitest", "test": "vitest",
"build": "pnpm build:tracker && pnpm build:server && pnpm build:client && pnpm build:geo", "build": "pnpm build:tracker && pnpm build:server && pnpm build:client && pnpm build:geo",
"build:client": "cd src/client && pnpm build", "build:client": "cd src/client && pnpm build",
"build:server": "cd src/server && pnpm build", "build:server": "pnpm run build --filer ./src/server",
"build:tracker": "ts-node scripts/build-tracker.ts", "build:tracker": "ts-node scripts/build-tracker.ts",
"build:geo": "ts-node scripts/build-geo.ts", "build:geo": "ts-node scripts/build-geo.ts",
"build:openapi": "ts-node scripts/build-openapi-schema.ts", "build:openapi": "ts-node scripts/build-openapi-schema.ts",

View File

@ -1,5 +1,9 @@
lockfileVersion: '6.0' lockfileVersion: '6.0'
settings:
autoInstallPeers: true
excludeLinksFromLockfile: false
overrides: overrides:
dayjs: 1.11.10 dayjs: 1.11.10
@ -65,6 +69,9 @@ importers:
'@tanstack/react-query': '@tanstack/react-query':
specifier: 4.33.0 specifier: 4.33.0
version: 4.33.0(react-dom@18.2.0)(react@18.2.0) version: 4.33.0(react-dom@18.2.0)(react@18.2.0)
'@tianji/shared':
specifier: workspace:^
version: link:../shared
'@trpc/client': '@trpc/client':
specifier: ^10.45.0 specifier: ^10.45.0
version: 10.45.0(@trpc/server@10.45.0) version: 10.45.0(@trpc/server@10.45.0)
@ -195,6 +202,9 @@ importers:
'@prisma/client': '@prisma/client':
specifier: 5.4.2 specifier: 5.4.2
version: 5.4.2(prisma@5.4.2) version: 5.4.2(prisma@5.4.2)
'@tianji/shared':
specifier: workspace:^
version: link:../shared
'@trpc/server': '@trpc/server':
specifier: ^10.45.0 specifier: ^10.45.0
version: 10.45.0 version: 10.45.0
@ -389,6 +399,12 @@ importers:
specifier: ^1.2.1 specifier: ^1.2.1
version: 1.2.1(@types/node@18.17.12) version: 1.2.1(@types/node@18.17.12)
src/shared:
dependencies:
dayjs:
specifier: 1.11.10
version: 1.11.10
website: website:
dependencies: dependencies:
'@docusaurus/core': '@docusaurus/core':
@ -16788,7 +16804,7 @@ packages:
engines: {node: '>=8.x'} engines: {node: '>=8.x'}
peerDependencies: peerDependencies:
date-fns: '>= 2.x' date-fns: '>= 2.x'
dayjs: '>= 1.x' dayjs: 1.11.10
luxon: '>= 3.x' luxon: '>= 3.x'
moment: '>= 2.x' moment: '>= 2.x'
react: '>=16.9.0' react: '>=16.9.0'
@ -16817,7 +16833,7 @@ packages:
engines: {node: '>=8.x'} engines: {node: '>=8.x'}
peerDependencies: peerDependencies:
date-fns: '>= 2.x' date-fns: '>= 2.x'
dayjs: '>= 1.x' dayjs: 1.11.10
luxon: '>= 3.x' luxon: '>= 3.x'
moment: '>= 2.x' moment: '>= 2.x'
react: '>=16.9.0' react: '>=16.9.0'

View File

@ -1,4 +1,5 @@
packages: packages:
- 'src/client' - 'src/client'
- 'src/server' - 'src/server'
- 'src/shared'
- 'website' - 'website'

View File

@ -1,4 +1,4 @@
import { ROLES } from '../../../../shared'; import { ROLES } from '@tianji/shared';
import { trpc } from '../../../api/trpc'; import { trpc } from '../../../api/trpc';
import { useUserInfo } from '../../../store/user'; import { useUserInfo } from '../../../store/user';

View File

@ -1,7 +1,7 @@
import { CalendarOutlined } from '@ant-design/icons'; import { CalendarOutlined } from '@ant-design/icons';
import dayjs, { Dayjs } from 'dayjs'; import dayjs, { Dayjs } from 'dayjs';
import { useMemo, useReducer } from 'react'; import { useMemo, useReducer } from 'react';
import { getMinimumUnit } from '../../shared'; import { getMinimumUnit } from '@tianji/shared';
import { DateRange, useGlobalStateStore } from '../store/global'; import { DateRange, useGlobalStateStore } from '../store/global';
import { DateUnit } from '../utils/date'; import { DateUnit } from '../utils/date';

View File

@ -17,6 +17,7 @@
"@loadable/component": "^5.16.3", "@loadable/component": "^5.16.3",
"@monaco-editor/react": "^4.6.0", "@monaco-editor/react": "^4.6.0",
"@tanstack/react-query": "4.33.0", "@tanstack/react-query": "4.33.0",
"@tianji/shared": "workspace:^",
"@trpc/client": "^10.45.0", "@trpc/client": "^10.45.0",
"@trpc/react-query": "^10.45.0", "@trpc/react-query": "^10.45.0",
"antd": "^5.13.1", "antd": "^5.13.1",

View File

@ -3,11 +3,11 @@ import { Outlet, useSearchParams } from 'react-router-dom';
import { NavItem } from '../components/NavItem'; import { NavItem } from '../components/NavItem';
import { MobileNavItem } from '../components/MobileNavItem'; import { MobileNavItem } from '../components/MobileNavItem';
import { UserOutlined } from '@ant-design/icons'; import { UserOutlined } from '@ant-design/icons';
import { Button, Divider, Drawer, Dropdown, Menu } from 'antd'; import { Button, Divider, Drawer, Dropdown } from 'antd';
import { useUserStore } from '../store/user'; import { useUserStore } from '../store/user';
import { useLogout } from '../api/model/user'; import { useLogout } from '../api/model/user';
import { ColorSchemeSwitcher } from '../components/ColorSchemeSwitcher'; import { ColorSchemeSwitcher } from '../components/ColorSchemeSwitcher';
import { version } from '../../shared'; import { version } from '@tianji/shared';
import { useIsMobile } from '../hooks/useIsMobile'; import { useIsMobile } from '../hooks/useIsMobile';
import { RiMenuUnfoldLine } from 'react-icons/ri'; import { RiMenuUnfoldLine } from 'react-icons/ri';

View File

@ -28,7 +28,7 @@ import { Loading } from '../components/Loading';
import { without } from 'lodash-es'; import { without } from 'lodash-es';
import { useIntervalUpdate } from '../hooks/useIntervalUpdate'; import { useIntervalUpdate } from '../hooks/useIntervalUpdate';
import clsx from 'clsx'; import clsx from 'clsx';
import { isServerOnline } from '../../shared'; import { isServerOnline } from '@tianji/shared';
import { defaultErrorHandler, trpc } from '../api/trpc'; import { defaultErrorHandler, trpc } from '../api/trpc';
import { useRequest } from '../hooks/useRequest'; import { useRequest } from '../hooks/useRequest';

View File

@ -2,7 +2,7 @@ import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc'; import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone'; import timezone from 'dayjs/plugin/timezone';
import advancedFormat from 'dayjs/plugin/advancedFormat'; import advancedFormat from 'dayjs/plugin/advancedFormat';
import type { DateUnit } from '../../shared'; import type { DateUnit } from '@tianji/shared';
dayjs.extend(utc); dayjs.extend(utc);
dayjs.extend(timezone); dayjs.extend(timezone);

View File

@ -1,6 +1,6 @@
import { RuleObject } from 'antd/es/form'; import { RuleObject } from 'antd/es/form';
import { z } from 'zod'; import { z } from 'zod';
import { hostnameRegex, slugRegex } from '../../shared'; import { hostnameRegex, slugRegex } from '@tianji/shared';
type Validator = ( type Validator = (
rule: RuleObject, rule: RuleObject,

View File

@ -1,6 +1,6 @@
import { Handler } from 'express'; import { Handler } from 'express';
import { getWorkspaceUser } from '../model/workspace'; import { getWorkspaceUser } from '../model/workspace';
import { ROLES } from '../../shared'; import { ROLES } from '@tianji/shared';
export function workspacePermission(roles: ROLES[] = []): Handler { export function workspacePermission(roles: ROLES[] = []): Handler {
return async (req, res, next) => { return async (req, res, next) => {

View File

@ -2,7 +2,7 @@ import dayjs from 'dayjs';
import { ServerStatusInfo } from '../../types'; import { ServerStatusInfo } from '../../types';
import { createSubscribeInitializer, subscribeEventBus } from '../ws/shared'; import { createSubscribeInitializer, subscribeEventBus } from '../ws/shared';
import _ from 'lodash'; import _ from 'lodash';
import { isServerOnline } from '../../shared'; import { isServerOnline } from '@tianji/shared';
const serverMap: Record< const serverMap: Record<
string, // workspaceId string, // workspaceId

View File

@ -1,6 +1,6 @@
import { prisma } from './_client'; import { prisma } from './_client';
import bcryptjs from 'bcryptjs'; import bcryptjs from 'bcryptjs';
import { ROLES, SYSTEM_ROLES } from '../../shared'; import { ROLES, SYSTEM_ROLES } from '@tianji/shared';
import { jwtVerify } from '../middleware/auth'; import { jwtVerify } from '../middleware/auth';
import { TRPCError } from '@trpc/server'; import { TRPCError } from '@trpc/server';
import { Prisma } from '@prisma/client'; import { Prisma } from '@prisma/client';

View File

@ -1,6 +1,7 @@
{ {
"name": "@tianji/server", "name": "@tianji/server",
"version": "1.0.0", "version": "1.0.0",
"private": true,
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
@ -18,6 +19,7 @@
"dependencies": { "dependencies": {
"@paralleldrive/cuid2": "^2.2.2", "@paralleldrive/cuid2": "^2.2.2",
"@prisma/client": "5.4.2", "@prisma/client": "5.4.2",
"@tianji/shared": "workspace:^",
"@trpc/server": "^10.45.0", "@trpc/server": "^10.45.0",
"axios": "^1.5.0", "axios": "^1.5.0",
"badge-maker": "^3.3.1", "badge-maker": "^3.3.1",

View File

@ -1,5 +1,5 @@
import { Router } from 'express'; import { Router } from 'express';
import { version } from '../../shared'; import { version } from '@tianji/shared';
export const healthRouter = Router(); export const healthRouter = Router();

View File

@ -8,7 +8,7 @@ import {
saveWebsiteSessionData, saveWebsiteSessionData,
} from '../model/website'; } from '../model/website';
import { createToken } from '../utils/common'; import { createToken } from '../utils/common';
import { hostnameRegex } from '../../shared'; import { hostnameRegex } from '@tianji/shared';
export const websiteRouter = Router(); export const websiteRouter = Router();

View File

@ -13,7 +13,7 @@ import {
} from '../model/workspace'; } from '../model/workspace';
import { parseDateRange } from '../utils/common'; import { parseDateRange } from '../utils/common';
import { QueryFilters } from '../utils/prisma'; import { QueryFilters } from '../utils/prisma';
import { ROLES } from '../../shared'; import { ROLES } from '@tianji/shared';
export const workspaceRouter = Router(); export const workspaceRouter = Router();

View File

@ -17,7 +17,7 @@ import { parseDateRange } from '../../utils/common';
import { getSessionMetrics, getPageviewMetrics } from '../../model/website'; import { getSessionMetrics, getPageviewMetrics } from '../../model/website';
import { websiteInfoSchema } from '../../model/_schema'; import { websiteInfoSchema } from '../../model/_schema';
import { OpenApiMeta } from 'trpc-openapi'; import { OpenApiMeta } from 'trpc-openapi';
import { hostnameRegex } from '../../../shared'; import { hostnameRegex } from '@tianji/shared';
import { addWorkspaceWebsite } from '../../model/workspace'; import { addWorkspaceWebsite } from '../../model/workspace';
const websiteNameSchema = z.string().max(100); const websiteNameSchema = z.string().max(100);

View File

@ -3,7 +3,7 @@ import _ from 'lodash';
import { z } from 'zod'; import { z } from 'zod';
import { jwtVerify } from '../middleware/auth'; import { jwtVerify } from '../middleware/auth';
import { getWorkspaceUser } from '../model/workspace'; import { getWorkspaceUser } from '../model/workspace';
import { ROLES, SYSTEM_ROLES } from '../../shared'; import { ROLES, SYSTEM_ROLES } from '@tianji/shared';
import type { IncomingMessage } from 'http'; import type { IncomingMessage } from 'http';
import { OpenApiMeta } from 'trpc-openapi'; import { OpenApiMeta } from 'trpc-openapi';

View File

@ -1,6 +1,6 @@
{ {
"extends": "../../tsconfig.json", "extends": "../../tsconfig.json",
"include": ["./**/*.ts", "../shared/**/*.ts", "../types/**/*.ts"], "include": ["./**/*.ts", "../types/**/*.ts"],
"exclude": ["node_modules/**/*", "dist"], "exclude": ["node_modules/**/*", "dist"],
"compilerOptions": { "compilerOptions": {
"rootDirs": ["./", "../"], "rootDirs": ["./", "../"],

View File

@ -8,7 +8,7 @@ import jwt from 'jsonwebtoken';
import _ from 'lodash'; import _ from 'lodash';
import { getWorkspaceWebsiteDateRange } from '../model/workspace'; import { getWorkspaceWebsiteDateRange } from '../model/workspace';
import { isCuid } from '@paralleldrive/cuid2'; import { isCuid } from '@paralleldrive/cuid2';
import { getMinimumUnit } from '../../shared'; import { getMinimumUnit } from '@tianji/shared';
export { isCuid }; export { isCuid };

View File

@ -1,6 +1,6 @@
import puppeteer from 'puppeteer'; import puppeteer from 'puppeteer';
import { jwtSign } from '../../middleware/auth'; import { jwtSign } from '../../middleware/auth';
import { SYSTEM_ROLES } from '../../../shared'; import { SYSTEM_ROLES } from '@tianji/shared';
import { env } from '../env'; import { env } from '../env';
/** /**

13
src/shared/package.json Normal file
View File

@ -0,0 +1,13 @@
{
"name": "@tianji/shared",
"version": "1.0.0",
"private": true,
"description": "",
"main": "index.ts",
"scripts": {},
"keywords": [],
"author": "moonrailgun <moonrailgun@gmail.com>",
"dependencies": {
"dayjs": "^1.11.9"
}
}