core: add 'hydrooj db' method

This commit is contained in:
undefined committed 2021-03-26 16:38:51 +08:00
1 parent 5513bd6929
commit 551cf9c19f
4 files changed
+30 -6

No files matched your search

+17 -2
View File
@@ -5,6 +5,7 @@ const path = require('path');
const cluster = require('cluster');
const fs = require('fs-extra');
const { argv } = require('yargs');
const child = require('child_process');
if (!cluster.isMaster) {
const hydro = require('../dist/loader');
@@ -14,11 +15,25 @@ if (!cluster.isMaster) {
process.exit(1);
});
} else {
fs.ensureDirSync(path.resolve(os.homedir(), '.hydro'));
const addonPath = path.resolve(os.homedir(), '.hydro', 'addon.json');
const hydroPath = path.resolve(os.homedir(), '.hydro');
fs.ensureDirSync(hydroPath);
const addonPath = path.resolve(hydroPath, 'addon.json');
if (!fs.existsSync(addonPath)) fs.writeFileSync(addonPath, '[]');
let addons = JSON.parse(fs.readFileSync(addonPath).toString());
if (argv._[0] === 'db') {
function buildUrl(opts) {
let mongourl = `${opts.protocol}://`;
if (opts.username) mongourl += `${opts.username}:${opts.password}@`;
mongourl += `${opts.host}:${opts.port}/${opts.name}`;
if (opts.url) mongourl = opts.url;
return mongourl;
}
const dbConfig = fs.readFileSync(path.resolve(hydroPath, 'config.json'), 'utf-8');
const url = buildUrl(JSON.parse(dbConfig));
return child.spawn('mongo', [url], { stdio: 'inherit' });
}
try {
const ui = argv.ui || '@hydrooj/ui-default';
require.resolve(ui);