From c2c85581c411d0cb65c0da7b17b4f86112705253 Mon Sep 17 00:00:00 2001 From: undefined Date: Sat, 1 Oct 2022 06:18:42 +0800 Subject: [PATCH] ui: seperate module build --- .gitignore | 1 + build/prepare.js | 59 +++++++++++++++------------ packages/hydrooj/bin/hydrooj.js | 14 ++++++- packages/hydrooj/src/interface.ts | 2 +- packages/hydrooj/src/model/opcount.ts | 1 - packages/hydrooj/src/plugin-api.ts | 1 + packages/migrate/scripts/vijos.ts | 9 ++-- packages/sonic/service.ts | 2 +- 8 files changed, 55 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index 7759c0f7..6d3111ac 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ globalConfig.json /config.json yarn.lock tsconfig.json +tsconfig.*.json # Project Rules ## Core diff --git a/build/prepare.js b/build/prepare.js index 7a44a0e9..e66345e6 100644 --- a/build/prepare.js +++ b/build/prepare.js @@ -20,7 +20,9 @@ const compilerOptionsBase = { const baseOutDir = path.resolve(__dirname, '../.cache/ts-out'); const config = { compilerOptions: compilerOptionsBase, - references: [], + references: [ + { path: 'tsconfig.ui.json' }, + ], files: [], }; const configSrc = (name) => ({ @@ -36,11 +38,35 @@ const configSrc = (name) => ({ 'dist', ], }); -const configFlat = (name) => (name === 'packages/ui-default' ? { +const configFlat = (name) => ({ + compilerOptions: { + ...compilerOptionsBase, + outDir: path.join(baseOutDir, name), + rootDir: '.', + }, + include: ['**/*.ts'], + exclude: ['public'], +}); + +if (!fs.existsSync(path.resolve(process.cwd(), 'plugins'))) { + fs.mkdirSync(path.resolve(process.cwd(), 'plugins')); +} + +const modules = [ + 'packages/hydrooj', + ...fs.readdirSync(path.resolve(process.cwd(), 'packages')).map((i) => `packages/${i}`), + ...fs.readdirSync(path.resolve(process.cwd(), 'plugins')).map((i) => `plugins/${i}`), +].filter((i) => !i.includes('/.') && !i.includes('ui-default')).filter((i) => fs.statSync(path.resolve(process.cwd(), i)).isDirectory()); + +const UIConfig = { exclude: [ - './public', + 'packages/ui-default/public', + ], + include: [ + 'packages/ui-default/**/*.ts', + 'packages/**/public/**/*.ts', + 'plugins/**/public/**/*.ts', ], - include: ['**/*.ts'], compilerOptions: { experimentalDecorators: true, resolveJsonModule: true, @@ -50,34 +76,16 @@ const configFlat = (name) => (name === 'packages/ui-default' ? { allowSyntheticDefaultImports: true, target: 'es2020', baseUrl: '.', - outDir: path.join(baseOutDir, name), + outDir: path.join(baseOutDir, 'ui'), moduleResolution: 'node', types, paths: { 'vj/*': [ - './*', + './packages/ui-default/*', ], }, }, -} : { - compilerOptions: { - ...compilerOptionsBase, - outDir: path.join(baseOutDir, name), - rootDir: '.', - }, - include: ['**/*.ts'], - exclude: [], -}); - -if (!fs.existsSync(path.resolve(process.cwd(), 'plugins'))) { - fs.mkdirSync(path.resolve(process.cwd(), 'plugins')); -} - -const modules = [ - 'packages/hydrooj', - ...fs.readdirSync(path.resolve(process.cwd(), 'packages')).map((i) => `packages/${i}`), - ...fs.readdirSync(path.resolve(process.cwd(), 'plugins')).map((i) => `plugins/${i}`), -].filter((i) => !i.includes('/.')).filter((i) => fs.statSync(path.resolve(process.cwd(), i)).isDirectory()); +}; for (const package of modules) { config.references.push({ path: package }); @@ -99,4 +107,5 @@ for (const package of modules) { } } } +fs.writeFileSync(path.resolve(process.cwd(), 'tsconfig.ui.json'), JSON.stringify(UIConfig)); fs.writeFileSync(path.resolve(process.cwd(), 'tsconfig.json'), JSON.stringify(config)); diff --git a/packages/hydrooj/bin/hydrooj.js b/packages/hydrooj/bin/hydrooj.js index 79c7966a..49a6f237 100755 --- a/packages/hydrooj/bin/hydrooj.js +++ b/packages/hydrooj/bin/hydrooj.js @@ -2,6 +2,18 @@ require('@hydrooj/utils/lib/register'); const { default: hook } = require('require-resolve-hook'); -const { bypass } = hook(/^(hydrooj|@hydrooj\/utils|cordis|schemastery|lodash|js-yaml)/, (id) => bypass(() => require.resolve(id))); +const { bypass } = hook(/^(hydrooj|@hydrooj\/utils|cordis|schemastery|lodash|js-yaml)/, (id) => { + if (id.startsWith('hydrooj/src')) { + console.log('module require via %s is deprecated.', id); + if (process.env.DEV) { + console.log( + new Error().stack.split('\n') + .filter((i) => !i.includes('node:internal') && i.startsWith(' ')) + .join('\n'), + ); + } + } + return bypass(() => require.resolve(id)); +}); require('./commands'); diff --git a/packages/hydrooj/src/interface.ts b/packages/hydrooj/src/interface.ts index b161c007..f952dc06 100644 --- a/packages/hydrooj/src/interface.ts +++ b/packages/hydrooj/src/interface.ts @@ -631,7 +631,7 @@ export interface Model { builtin: typeof import('./model/builtin'), contest: typeof import('./model/contest'), DiscussionModel: typeof import('./model/discussion'), - document: typeof import('./model/document'), + document: Omit, domain: typeof import('./model/domain').default, message: typeof import('./model/message').default, opcount: typeof import('./model/opcount'), diff --git a/packages/hydrooj/src/model/opcount.ts b/packages/hydrooj/src/model/opcount.ts index b8ad4dd4..983e53d7 100644 --- a/packages/hydrooj/src/model/opcount.ts +++ b/packages/hydrooj/src/model/opcount.ts @@ -1,5 +1,4 @@ import { OpcountExceededError } from '../error'; -import * as bus from '../service/bus'; import db from '../service/db'; const coll = db.collection('opcount'); diff --git a/packages/hydrooj/src/plugin-api.ts b/packages/hydrooj/src/plugin-api.ts index a65c946d..1e630786 100644 --- a/packages/hydrooj/src/plugin-api.ts +++ b/packages/hydrooj/src/plugin-api.ts @@ -42,6 +42,7 @@ export { default as paginate } from './lib/paginate'; export * from './service/decorators'; export { Handler, ConnectionHandler } from './service/server'; export { UiContextBase } from './service/layers/base'; +export * as StorageService from './service/storage'; export { EventMap } from './service/bus'; export { db, Schema, yaml, fs, AdmZip, superagent, diff --git a/packages/migrate/scripts/vijos.ts b/packages/migrate/scripts/vijos.ts index af929459..6f4c75ac 100644 --- a/packages/migrate/scripts/vijos.ts +++ b/packages/migrate/scripts/vijos.ts @@ -2,13 +2,12 @@ /* eslint-disable no-await-in-loop */ import mongodb, { Cursor, Db } from 'mongodb'; import { - DiscussionModel, DiscussionTailReplyDoc, MessageDoc, - RecordDoc, Schema, TestCase, TrainingNode, + db as dst, + DiscussionModel, DiscussionTailReplyDoc, DocumentModel, + MessageDoc, RecordDoc, Schema, TestCase, TrainingNode, } from 'hydrooj'; -const dst = global.Hydro.service.db; const map = {}; - const pid = (id) => { if (map[id.toString()]) return map[id.toString()]; return id; @@ -74,7 +73,7 @@ const tasks = { parent_doc_id: { field: 'parentId', processer: (parentId, doc) => { - if (doc.parent_doc_type === global.Hydro.model.document.TYPE_PROBLEM) { + if (doc.parent_doc_type === DocumentModel.TYPE_PROBLEM) { return pid(parentId); } return parentId; diff --git a/packages/sonic/service.ts b/packages/sonic/service.ts index 32cb9115..18c0b7b8 100644 --- a/packages/sonic/service.ts +++ b/packages/sonic/service.ts @@ -13,7 +13,7 @@ declare module 'hydrooj' { 'sonic.auth': string; } interface Context { - sonic: SonicService; + sonic?: SonicService; } }