core: experimental hot reload support

This commit is contained in:
undefined committed 2022-09-20 22:34:36 +08:00
1 parent 632ae27b88
commit fd7c9cbeb1
26 files changed
+429 -148

No files matched your search

+6 -1
View File
@@ -8,6 +8,7 @@ import yaml from 'js-yaml';
import i18n from '../lib/i18n';
import { Logger } from '../logger';
import * as bus from '../service/bus';
import { Runtime } from '../service/module';
const logger = new Logger('common');
@@ -36,7 +37,11 @@ const getLoader = (type: string, filename: string) => async function loader(pend
if (p && !fail.includes(i)) {
try {
logger.info(`${type.replace(/^(.)/, (t) => t.toUpperCase())} init: %s`, i);
require(p);
const res = require(p);
if (res.apply) {
const runtime = new Runtime(p);
runtime.load(res);
}
} catch (e) {
fail.push(i);
logger.info(`${type.replace(/^(.)/, (t) => t.toUpperCase())} load fail: %s`, i);
+11 -4
View File
@@ -8,12 +8,13 @@ import { Logger } from '../logger';
import options from '../options';
import * as bus from '../service/bus';
import db from '../service/db';
import { Runtime } from '../service/module';
import {
handler, lib, locale, model, script, service, setting, template,
} from './common';
const logger = new Logger('worker');
const detail = cac().parse().options.loaderDetail;
const { loaderDetail: detail, watch } = cac().parse().options;
const tmpdir = path.resolve(os.tmpdir(), 'hydro');
export async function load() {
@@ -53,11 +54,18 @@ export async function load() {
const server = require('../service/server');
await server.prepare();
if (detail) logger.info('finish: server');
if (watch) require('../service/watcher');
await service(pending, fail);
if (detail) logger.info('finish: service.extra');
require('../model/index');
if (detail) logger.info('finish: model.builtin');
require('../handler/index');
const handlerDir = path.resolve(__dirname, '..', 'handler');
const handlers = await fs.readdir(handlerDir);
for (const h of handlers) {
const f = path.resolve(handlerDir, h);
const m = require(f);
if (m.apply) new Runtime(f).load(m);
}
if (detail) logger.info('finish: handler.builtin');
await model(pending, fail);
if (detail) logger.info('finish: model.extra');
@@ -68,8 +76,6 @@ export async function load() {
if (detail) logger.info('finish: handler.extra');
for (const i in global.Hydro.handler) await global.Hydro.handler[i]();
if (detail) logger.info('finish: handler.apply');
const notfound = require('../handler/notfound');
await notfound.apply();
require('../script/index');
if (detail) logger.info('finish: script.builtin');
await script(pending, fail);
@@ -98,5 +104,6 @@ export async function load() {
}
logger.success('Server started');
process.send?.('ready');
await bus.serial('app/ready');
return { fail };
}