remove ui

This commit is contained in:
undefined committed 2020-07-19 13:31:56 +08:00
1 parent 9dd370fa72
commit 87123659aa
546 files changed
+180 -33522

No files matched your search

+1
View File
@@ -246,6 +246,7 @@ export async function wiki(pending: string[], fail: string[]) {
} catch (e) {
fail.push(i);
console.error(`Wiki Load Fail: ${i}`);
console.error(e);
}
}
}
+2 -2
View File
@@ -5,8 +5,8 @@ import {
builtinLib, builtinHandler, builtinModel,
} from './common';
export async function load(call) {
let pending = await require('../lib/hpm').getInstalled();
export async function load(call, args) {
let pending = args;
const fail = [];
require('../lib/i18n');
require('../utils');
+4 -3
View File
@@ -1,7 +1,7 @@
import fs from 'fs';
import os from 'os';
import path from 'path';
import http from 'http';
import fs from 'fs-extra';
import Koa from 'koa';
import Body from 'koa-body';
import Router from 'koa-router';
@@ -89,7 +89,7 @@ const env = new nunjucks.Environment(new Loader(), { autoescape: true, trimBlock
function render(name) {
return new Promise((resolve, reject) => {
env.render(name, {
_: (str) => str,
_: (str: string) => str,
}, (err, res) => {
if (err) reject(err);
else resolve(res);
@@ -145,7 +145,8 @@ export function load() {
{ upsert: true },
),
]);
fs.writeFileSync(path.resolve(process.cwd(), 'config.json'), JSON.stringify({
fs.ensureDirSync(path.resolve(os.homedir(), '.hydro'));
fs.writeFileSync(path.resolve(os.homedir(), '.hydro', 'config.json'), JSON.stringify({
host, port, name, username, password,
}));
ctx.body = `<h1>This page will reload in 3 secs.</h1>
-78
View File
@@ -1,78 +0,0 @@
/* eslint-disable no-continue */
import os from 'os';
import path from 'path';
import AdmZip from 'adm-zip';
import fs from 'fs-extra';
const moduleRoots = [];
const _moduleRoots = Array.from(new Set([
path.resolve(process.cwd(), 'node_modules', '@hydrooj'),
path.resolve(process.cwd(), 'modules'),
path.resolve(process.cwd(), 'module'),
path.resolve(process.cwd(), '.build'),
process.cwd(),
path.resolve(__dirname, 'node_modules', '@hydrooj'),
path.resolve(__dirname, 'modules'),
path.resolve(__dirname, 'module'),
path.resolve(__dirname, '.build'),
__dirname,
path.resolve(os.homedir(), '.hydro', 'module'),
// TODO add npm global path
'/usr/local/share/.config/yarn/global/node_modules/@hydrooj',
path.resolve(os.homedir(), 'AppData', 'Local', 'Yarn', 'Data', 'global', 'node_modules', '@hydrooj'),
]));
for (const moduleRoot of _moduleRoots) {
if (fs.existsSync(moduleRoot)) moduleRoots.push(moduleRoot);
}
if (process.env.debug) console.log('Module roots: ', moduleRoots);
const moduleTemp = path.resolve(os.tmpdir(), 'hydro', 'module');
const publicTemp = path.resolve(os.tmpdir(), 'hydro', 'public');
const tmp = path.resolve(os.tmpdir(), 'hydro', '__');
process.on('unhandledRejection', (e) => {
console.error(e);
process.exit(1);
});
export async function load() {
fs.ensureDirSync(moduleTemp);
fs.ensureDirSync(publicTemp);
for (const moduleRoot of moduleRoots) {
if (fs.existsSync(moduleRoot)) {
const files = fs.readdirSync(moduleRoot);
for (const file of files) {
try {
const modulePath = path.join(moduleRoot, file);
const packagejson = path.join(modulePath, 'package.json');
if (fs.statSync(modulePath).isFile() && file.endsWith('.hydro')) {
// Is *.hydro module
const zip = new AdmZip(modulePath);
const targetPath = path.resolve(moduleTemp, file.split('.')[0]);
zip.extractAllTo(targetPath, true);
const content = fs.readdirSync(targetPath);
const ipath = path.join(targetPath, content[0]);
if (content.length === 1 && fs.statSync(ipath).isDirectory()) {
fs.moveSync(ipath, tmp);
fs.rmdirSync(targetPath);
fs.moveSync(tmp, targetPath);
}
const publicPath = path.resolve(moduleTemp, file.split('.')[0], 'public');
if (fs.existsSync(publicPath)) fs.copySync(publicPath, publicTemp);
} else if (fs.existsSync(packagejson)) {
// Is a npm package
const q = fs.readFileSync(packagejson).toString();
if (!JSON.parse(q).name.startsWith('@hydrooj/')) continue;
const publicPath = path.resolve(modulePath, 'public');
if (fs.existsSync(publicPath)) fs.copySync(publicPath, publicTemp);
} else continue;
} catch (e) {
console.error('Module extract fail:', file, e);
}
}
}
}
console.log('Unzip done.');
process.exit(0);
}
+2 -2
View File
@@ -5,8 +5,8 @@ import {
builtinLib, builtinScript, builtinHandler, builtinModel,
} from './common';
export async function load() {
let pending = await require('../lib/hpm').getInstalled();
export async function load(call, args) {
let pending = args;
const fail = [];
const active = [];
require('../lib/i18n');
+2 -1
View File
@@ -292,8 +292,9 @@ declare global {
locales: Dict<Dict<string>>,
postInit: Function[],
},
nodeModules: any,
onDestory: Function[],
addons: string[],
argv: any,
}
}
}
+1 -15
View File
@@ -13,20 +13,6 @@ const moduleRoots = Array.from(new Set([
path.resolve(os.tmpdir(), 'hydro', 'module'),
]));
export async function getInstalled() {
const modules = [];
for (const moduleRoot of moduleRoots) {
if (fs.existsSync(moduleRoot)) {
const files = fs.readdirSync(moduleRoot);
for (const file of files) {
const info = path.resolve(moduleRoot, file, 'package.json');
if (fs.existsSync(info)) modules.push(path.resolve(moduleRoot, file));
}
}
}
return modules;
}
export async function getDetail() {
const modules = [];
for (const moduleRoot of moduleRoots) {
@@ -55,5 +41,5 @@ export function install(url: string, name: string = '') {
}
global.Hydro.lib.hpm = {
getInstalled, getDetail, install,
getDetail, install,
};
+75 -23
View File
@@ -1,12 +1,17 @@
/* eslint-disable no-continue */
/* eslint-disable import/no-dynamic-require */
/* eslint-disable no-await-in-loop */
/* eslint-disable no-eval */
import './interface';
import os from 'os';
import path from 'path';
import cluster from 'cluster';
import fs from 'fs-extra';
import parse from 'yargs-parser';
import AdmZip from 'adm-zip';
const argv = parse(process.argv.slice(2));
global.argv = parse(process.argv.slice(2));
global.Hydro = {
stat: { reqCount: 0 },
@@ -30,8 +35,9 @@ global.Hydro = {
postInit: [],
};
global.onDestory = [];
global.addons = [];
if (argv.debug) {
if (global.argv.debug) {
console.log(process.argv);
process.env.debug = 'enable';
}
@@ -46,7 +52,7 @@ async function terminate() {
async function fork(args: string[] = []) {
const _args = process.argv.slice(2);
_args.push(...args);
_args.push(...args, `--addons=${Buffer.from(JSON.stringify(global.addons)).toString('base64')}`);
cluster.setupMaster({ args: _args });
return cluster.fork();
}
@@ -54,7 +60,8 @@ async function fork(args: string[] = []) {
async function entry(config: any) {
if (config.entry) {
if (config.newProcess) {
const p = await fork([`--entry=${config.entry}`]);
const sargv = [`--entry=${config.entry}`];
const p = await fork(sargv);
await new Promise((resolve, reject) => {
p.on('exit', resolve);
p.on('error', (err: Error) => {
@@ -64,7 +71,7 @@ async function entry(config: any) {
});
} else {
const loader = require(`./entry/${config.entry}`);
return await loader.load(entry);
return await loader.load(entry, global.addons);
}
}
return null;
@@ -75,8 +82,9 @@ async function stopWorker() {
}
async function startWorker(cnt: number) {
await fork(['--firstWorker']);
for (let i = 1; i < cnt; i++) await fork();
const sargv = [`--args=${Buffer.from(JSON.stringify(global.addons)).toString('base64')}`];
await fork(['--firstWorker', ...sargv]);
for (let i = 1; i < cnt; i++) await fork(sargv);
}
async function executeCommand(input: string) {
@@ -92,7 +100,9 @@ async function executeCommand(input: string) {
async function messageHandler(worker: cluster.Worker, msg: any) {
if (!msg) msg = worker;
if (msg.event) {
if (msg.event === 'bus') {
if (msg.event === 'setAddon') {
global.addons = msg.addons;
} else if (msg.event === 'bus') {
if (cluster.isMaster) {
for (const i in cluster.workers) {
cluster.workers[i].send(msg);
@@ -113,19 +123,61 @@ async function messageHandler(worker: cluster.Worker, msg: any) {
}
}
async function load() {
global.nodeModules = {
'adm-zip': require('adm-zip'),
superagent: require('superagent'),
'js-yaml': require('js-yaml'),
mongodb: require('mongodb'),
};
const moduleTemp = path.resolve(os.tmpdir(), 'hydro', 'module');
const publicTemp = path.resolve(os.tmpdir(), 'hydro', 'public');
const tmp = path.resolve(os.tmpdir(), 'hydro', '__');
export function addon(addonPath: string) {
let modulePath = path.resolve(addonPath);
if (!(fs.existsSync(addonPath) && fs.statSync(addonPath).isFile())) {
try {
// Is a npm package
const packagejson = require.resolve(`${addonPath}/package.json`);
modulePath = path.dirname(packagejson);
const publicPath = path.resolve(modulePath, 'public');
if (fs.existsSync(publicPath)) fs.copySync(publicPath, publicTemp);
global.addons.push(modulePath);
} catch (e) {
throw new Error(`Addon not found: ${addonPath}`);
}
} else {
try {
// Is *.hydro module
const t = modulePath.split(path.sep);
const name = t[t.length - 1].split('.')[0];
const zip = new AdmZip(modulePath);
const targetPath = path.resolve(moduleTemp, name);
zip.extractAllTo(targetPath, true);
const content = fs.readdirSync(targetPath);
const ipath = path.join(targetPath, content[0]);
if (content.length === 1 && fs.statSync(ipath).isDirectory()) {
fs.moveSync(ipath, tmp);
fs.rmdirSync(targetPath);
fs.moveSync(tmp, targetPath);
}
const publicPath = path.resolve(targetPath, 'public');
if (fs.existsSync(publicPath)) fs.copySync(publicPath, publicTemp);
global.addons.push(targetPath);
} catch (e) {
console.error('Addon load fail: ', e);
throw e;
}
}
}
export async function load() {
while (!global.addons) {
await new Promise((resolve) => {
setTimeout(resolve, 100);
});
}
addon(path.resolve(__dirname, '..'));
Error.stackTraceLimit = 50;
process.on('unhandledRejection', (e) => console.error(e));
process.on('SIGINT', terminate);
process.on('message', messageHandler);
cluster.on('message', messageHandler);
if (cluster.isMaster || argv.startAsMaster) {
if (cluster.isMaster || global.argv.startAsMaster) {
console.log(`Master ${process.pid} Starting`);
process.stdin.setEncoding('utf8');
process.stdin.on('data', (buf) => {
@@ -139,7 +191,6 @@ async function load() {
executeCommand(input);
}
});
await entry({ entry: 'unzip', newProcess: true });
const cnt = await entry({ entry: 'master' });
console.log('Master started');
cluster.on('exit', (worker, code, signal) => {
@@ -153,14 +204,15 @@ async function load() {
});
cluster.on('online', (worker) => {
console.log(`Worker ${worker.process.pid} ${worker.id} is online`);
worker.send({ event: 'setAddon', addons: global.addons });
});
await startWorker(cnt);
} else if (argv.entry) {
console.log(`Worker ${process.pid} Starting as ${argv.entry}`);
await entry({ entry: argv.entry });
console.log(`Worker ${process.pid} Started as ${argv.entry}`);
} else if (global.argv.entry) {
console.log(`Worker ${process.pid} Starting as ${global.argv.entry}`);
await entry({ entry: global.argv.entry });
console.log(`Worker ${process.pid} Started as ${global.argv.entry}`);
} else {
if (argv.firstWorker) cluster.isFirstWorker = true;
if (global.argv.firstWorker) cluster.isFirstWorker = true;
else cluster.isFirstWorker = false;
console.log(`Worker ${process.pid} Starting`);
await entry({ entry: 'worker' });
@@ -169,7 +221,7 @@ async function load() {
if (global.gc) global.gc();
}
if (argv.pandora || !module.parent) {
if (global.argv.pandora || !module.parent) {
load().catch((e) => {
console.error(e);
process.exit(1);
+1 -10
View File
@@ -113,6 +113,7 @@ SystemSetting(
Setting('setting_smtp', 'smtp.port', null, 465, 'number', 'SMTP Server Port'),
Setting('setting_smtp', 'smtp.from', null, null, 'text', 'Mail From'),
Setting('setting_smtp', 'smtp.secure', null, false, 'boolean', 'SSL'),
Setting('setting_server', 'server.name', null, 'Hydro', 'text', 'Server Name'),
Setting('setting_server', 'server.worker', null, 1, 'number', 'Server Workers Number'),
Setting('setting_server', 'server.hostname', null, null, 'text', 'Server Hostname'),
Setting('setting_server', 'server.host', null, null, 'text', 'Server Host'),
@@ -125,16 +126,6 @@ SystemSetting(
Setting('setting_session', 'session.secure', null, false, 'boolean', 'session.secure'),
Setting('setting_session', 'session.saved_expire_seconds', null, 3600 * 24 * 30, 'number', 'Saved session expire seconds'),
Setting('setting_session', 'session.unsaved_expire_seconds', null, 3600 * 3, 'number', 'Unsaved session expire seconds'),
Setting('setting_ui', 'ui.name', null, 'Hydro', 'text', 'Site Name'),
Setting('setting_ui', 'ui.footer_extra_html', null, '', 'textarea', 'Footer Extra HTML'),
Setting('setting_ui', 'ui.header_logo', null, '/components/header/header-logo.png', 'text', 'Header Logo'),
Setting('setting_ui', 'ui.header_logo_2x', null, '/components/header/header-logo@2x.png', 'text', 'Header Logo@2x'),
Setting('setting_ui', 'ui.header_background', null, '/components/header/header-background.png', 'text', 'Header Background'),
Setting('setting_ui', 'ui.header_background_2x', null, '/components/header/header-background@2x.png', 'text', 'Header Background@2x'),
Setting('setting_ui', 'ui.nav_logo_light', null, '/components/navigation/nav-logo-small_light.png', 'text', 'Nav Logo(Light)'),
Setting('setting_ui', 'ui.nav_logo_dark', null, '/components/navigation/nav-logo-small_dark.png', 'text', 'Nav Logo(Dark)'),
Setting('setting_ui', 'ui.nav_logo_light_2x', null, '/components/navigation/nav-logo-small@2x_light.png', 'text', 'Nav Logo@2x(Light)'),
Setting('setting_ui', 'ui.nav_logo_dark_2x', null, '/components/navigation/nav-logo-small@2x_dark.png', 'text', 'Nav Logo@2x(Dark)'),
Setting('setting_oauth', 'oauth.githubappid', null, null, 'text', 'Github Oauth AppID'),
Setting('setting_oauth', 'oauth.githubsecret', null, null, 'text', 'Github Oauth Secret', null, FLAG_SECRET),
Setting('setting_oauth', 'oauth.googleappid', null, null, 'text', 'Google Oauth ClientID', null),
+1
View File
@@ -6,6 +6,7 @@ export default function load() {
let f = path.resolve(process.cwd(), 'config.json');
if (!fs.existsSync(f)) f = path.resolve(__dirname, 'config.json');
if (!fs.existsSync(f)) f = path.resolve(os.homedir(), '.config', 'hydro', 'config.json');
if (!fs.existsSync(f)) f = path.resolve(os.homedir(), '.hydro', 'config.json');
if (!fs.existsSync(f)) f = path.resolve('/config/config.json');
return JSON.parse(fs.readFileSync(f).toString());
}