mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
use findFile() api
This commit is contained in:
1 parent
8a8e312ca4
commit
9a538130ee
10 files changed
+105
-52
No files matched your search
@@ -1,9 +1,9 @@
|
||||
import 'hydrooj';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { Reader } from 'maxmind';
|
||||
import { findFileSync } from '@hydrooj/utils/lib/utils';
|
||||
|
||||
const buffer = fs.readFileSync(path.resolve(__dirname, 'GeoLite2-City.mmdb'));
|
||||
const buffer = fs.readFileSync(findFileSync('@hydrooj/geoip/GeoLite2-City.mmdb'));
|
||||
const reader = new Reader(buffer);
|
||||
|
||||
export interface Result {
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import fs from 'fs-extra';
|
||||
import { resolve } from 'path';
|
||||
import { findFileSync } from '@hydrooj/utils/lib/utils';
|
||||
import checkers from './checkers';
|
||||
import compile from './compile';
|
||||
import { SystemError } from './error';
|
||||
import { parseFilename } from './utils';
|
||||
|
||||
const testlibSrc = findFileSync('@hydrooj/hydrojudge/vendor/testlib/testlib.h');
|
||||
|
||||
export async function check(config): Promise<[number, number, string]> {
|
||||
if (!checkers[config.checker_type]) throw new SystemError(`未知比较器类型:${config.checker_type}`);
|
||||
const {
|
||||
@@ -24,7 +26,7 @@ export async function check(config): Promise<[number, number, string]> {
|
||||
|
||||
export async function compileChecker(checkerType: string, checker: string, copyIn: any) {
|
||||
if (!checkers[checkerType]) throw new SystemError('Unknown checker type {0}.', [checkerType]);
|
||||
if (checkerType === 'testlib') copyIn['testlib.h'] = { src: resolve(__dirname, '../vendor/testlib/testlib.h') };
|
||||
if (checkerType === 'testlib') copyIn['testlib.h'] = { src: testlibSrc };
|
||||
const file = await fs.readFile(checker);
|
||||
// TODO cache compiled checker
|
||||
return await compile(parseFilename(checker).split('.')[1], file.toString(), 'checker', copyIn);
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import Queue from 'p-queue';
|
||||
import fs from 'fs-extra';
|
||||
import { resolve } from 'path';
|
||||
import * as STATUS from '../status';
|
||||
import { parse } from '../testlib';
|
||||
import { parseFilename } from '../utils';
|
||||
import { findFileSync, parseFilename } from '../utils';
|
||||
import { run } from '../sandbox';
|
||||
import compile from '../compile';
|
||||
import signals from '../signals';
|
||||
import { getConfig } from '../config';
|
||||
|
||||
const testlibSrc = findFileSync('@hydrooj/hydrojudge/vendor/testlib/testlib.h');
|
||||
const Score = {
|
||||
sum: (a, b) => (a + b),
|
||||
max: Math.max,
|
||||
@@ -19,7 +19,7 @@ function judgeCase(c) {
|
||||
return async (ctx, ctxSubtask) => {
|
||||
ctx.executeInteractor.copyIn.in = c.input ? { src: c.input } : { content: '' };
|
||||
ctx.executeInteractor.copyIn.out = c.output ? { src: c.output } : { content: '' };
|
||||
ctx.executeInteractor.copyIn['testlib.h'] = { src: resolve(__dirname, '../../vendor/testlib/testlib.h') };
|
||||
ctx.executeInteractor.copyIn['testlib.h'] = { src: testlibSrc };
|
||||
const [{ code, time_usage_ms, memory_usage_kb }, resInteractor] = await run([
|
||||
{
|
||||
execute: ctx.executeUser.execute.replace(/\$\{name\}/g, 'code'),
|
||||
|
||||
@@ -43,6 +43,7 @@
|
||||
"@types/inquirer": "^7.3.1",
|
||||
"@types/js-yaml": "^4.0.0",
|
||||
"@types/koa": "^2.13.1",
|
||||
"@types/koa-compress": "^4.0.1",
|
||||
"@types/koa-router": "^7.4.1",
|
||||
"@types/koa-static-cache": "^5.1.0",
|
||||
"@types/lodash": "^4.14.168",
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import { findFileSync } from '@hydrooj/utils/lib/utils';
|
||||
import { Logger } from './logger';
|
||||
|
||||
const logger = new Logger('options');
|
||||
@@ -13,13 +14,8 @@ export = function load() {
|
||||
process.env[line.split('=')[0]] = line.split('=')[1];
|
||||
}
|
||||
}
|
||||
|
||||
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');
|
||||
if (!fs.existsSync(f)) return null;
|
||||
const f = findFileSync('config.json', false);
|
||||
if (!f) return null;
|
||||
let result: any = {};
|
||||
try {
|
||||
result = JSON.parse(fs.readFileSync(f).toString());
|
||||
|
||||
@@ -6,6 +6,7 @@ const nunjucks = require('nunjucks');
|
||||
const { filter } = require('lodash');
|
||||
const { argv } = require('yargs');
|
||||
const Xss = require('xss');
|
||||
const { findFileSync } = require('@hydrooj/utils/lib/utils');
|
||||
const markdown = require('./markdown');
|
||||
|
||||
const { misc, buildContent } = global.Hydro.lib;
|
||||
@@ -76,6 +77,9 @@ const xss = new Xss.FilterXSS({
|
||||
},
|
||||
});
|
||||
|
||||
if (argv.template && argv.template !== 'string') argv.template = findFileSync('@hydrooj/ui-default/templates');
|
||||
else argv.template = findFileSync(argv.template);
|
||||
|
||||
class Loader extends nunjucks.Loader {
|
||||
// eslint-disable-next-line class-methods-use-this
|
||||
getSource(name) {
|
||||
@@ -88,8 +92,7 @@ class Loader extends nunjucks.Loader {
|
||||
};
|
||||
}
|
||||
let fullpath = null;
|
||||
if (typeof argv.template !== 'string') argv.template = path.resolve(__dirname, '..', 'templates');
|
||||
const p = path.resolve(process.cwd(), argv.template, name);
|
||||
const p = path.resolve(argv.template, name);
|
||||
if (fs.existsSync(p)) fullpath = p;
|
||||
if (!fullpath) {
|
||||
if (global.Hydro.ui.template[name]) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import path, { sep } from 'path';
|
||||
import fs from 'fs';
|
||||
import fs from 'fs-extra';
|
||||
import os from 'os';
|
||||
import { Duplex } from 'stream';
|
||||
import { ObjectID } from 'bson';
|
||||
import { isMoment } from 'moment';
|
||||
@@ -305,6 +306,54 @@ export function changeErrorType(err: Error, Err: any) {
|
||||
return e;
|
||||
}
|
||||
|
||||
export async function findFile(pathname: string, doThrow = true) {
|
||||
if (await fs.pathExists(path.resolve(pathname))) return path.resolve(pathname);
|
||||
if (await fs.pathExists(path.resolve(process.cwd(), pathname))) return path.resolve(process.cwd(), pathname);
|
||||
if (await fs.pathExists(path.resolve(__dirname, pathname))) return path.resolve(__dirname, pathname);
|
||||
try {
|
||||
return require.resolve(pathname);
|
||||
} catch (e) { }
|
||||
if (pathname.includes('/')) {
|
||||
const eles = pathname.split('/');
|
||||
let pkg = eles.shift();
|
||||
if (pkg.startsWith('@')) pkg = `${pkg}/${eles.shift()}`;
|
||||
const rest = eles.join('/');
|
||||
try {
|
||||
const p = require.resolve(pkg);
|
||||
if (await fs.pathExists(path.resolve(p, rest))) return path.resolve(p, rest);
|
||||
} catch (e) { }
|
||||
}
|
||||
if (await fs.pathExists(path.resolve(os.homedir(), pathname))) return path.resolve(__dirname, pathname);
|
||||
if (await fs.pathExists(path.resolve(os.homedir(), '.hydro', pathname))) return path.resolve(__dirname, pathname);
|
||||
if (await fs.pathExists(path.resolve(os.homedir(), '.config', 'hydro', pathname))) return path.resolve(__dirname, pathname);
|
||||
if (doThrow) throw new Error(`File ${pathname} not found`);
|
||||
return null;
|
||||
}
|
||||
|
||||
export function findFileSync(pathname: string, doThrow = true) {
|
||||
if (fs.pathExistsSync(path.resolve(pathname))) return path.resolve(pathname);
|
||||
if (fs.pathExistsSync(path.resolve(process.cwd(), pathname))) return path.resolve(process.cwd(), pathname);
|
||||
if (fs.pathExistsSync(path.resolve(__dirname, pathname))) return path.resolve(__dirname, pathname);
|
||||
try {
|
||||
return require.resolve(pathname);
|
||||
} catch (e) { }
|
||||
if (pathname.includes('/')) {
|
||||
const eles = pathname.split('/');
|
||||
let pkg = eles.shift();
|
||||
if (pkg.startsWith('@')) pkg = `${pkg}/${eles.shift()}`;
|
||||
const rest = eles.join('/');
|
||||
try {
|
||||
const p = require.resolve(pkg);
|
||||
if (fs.pathExistsSync(path.resolve(p, rest))) return path.resolve(p, rest);
|
||||
} catch (e) { }
|
||||
}
|
||||
if (fs.pathExistsSync(path.resolve(os.homedir(), pathname))) return path.resolve(__dirname, pathname);
|
||||
if (fs.pathExistsSync(path.resolve(os.homedir(), '.hydro', pathname))) return path.resolve(__dirname, pathname);
|
||||
if (fs.pathExistsSync(path.resolve(os.homedir(), '.config', 'hydro', pathname))) return path.resolve(__dirname, pathname);
|
||||
if (doThrow) throw new Error(`File ${pathname} not found`);
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function retry(func: Function, ...args: any[]): Promise<any>;
|
||||
export async function retry(times: number, func: Function, ...args: any[]): Promise<any>;
|
||||
// eslint-disable-next-line consistent-return
|
||||
|
||||
Reference in new issue
Block a user