core: allow load from nix store

This commit is contained in:
undefined committed 2024-03-23 04:25:45 +08:00
1 parent c123995235
commit c0df5cecb3
3 files changed
+24 -4

No files matched your search

+22 -2
View File
@@ -7,6 +7,7 @@ import './init';
import './interface';
import Schema from 'schemastery';
import path from 'path';
import child from 'child_process';
// eslint-disable-next-line import/no-duplicates
import './utils';
import cac from 'cac';
@@ -26,6 +27,21 @@ logger.debug('%o', argv);
process.on('unhandledRejection', logger.error);
process.on('uncaughtException', logger.error);
const HYDROPATH = [];
if (process.env.NIX_PROFILES) {
try {
const result = JSON.parse(child.execSync('nix-env -q --json --out-path --meta').toString()) as Record<string, any>;
for (const derivation of Object.values(result)) {
if (derivation.name.startsWith('hydro-plugin-') && derivation.outputs?.out) {
HYDROPATH.push(derivation.outputs.out);
}
}
} catch (e) {
logger.error('Nix detected, but failed to list installed derivations.');
}
}
export function resolveConfig(plugin: any, config: any) {
if (config === false) return;
if (config === true) config = undefined;
@@ -108,8 +124,12 @@ export class Loader {
try {
this.cache[name] ||= require.resolve(name);
} catch (err) {
logger.error(err.message);
return;
try {
this.cache[name] ||= require.resolve(name, { paths: HYDROPATH });
} catch (e) {
logger.error(err.message);
return;
}
}
return unwrapExports(require(this.cache[name]));
}
+1 -1
View File
@@ -94,7 +94,7 @@ export interface EventMap extends LifecycleEvents, HandlerEvents {
'contest/before-add': (payload: Partial<Tdoc>) => VoidReturn
'contest/add': (payload: Partial<Tdoc>, id: ObjectId) => VoidReturn
'contest/list': (query: Fileter<Tdoc>, handler: any) => VoidReturn
'contest/list': (query: Filter<Tdoc>, handler: any) => VoidReturn
'contest/scoreboard': (tdoc: Tdoc, rows: ScoreboardRow[], udict: BaseUserDict, pdict: ProblemDict) => VoidReturn
'contest/balloon': (domainId: string, tid: ObjectId, bdoc: ContestBalloonDoc) => VoidReturn
+1 -1
View File
@@ -44,7 +44,7 @@ export function convertIniConfig(ini: string) {
const count = parseInt(f[0], 10);
const res = { subtasks: [] };
for (let i = 1; i <= count; i++) {
if (!f[i] || !f[i].trim()) throw new Error('Testcada count incorrect.');
if (!f[i]?.trim()) throw new Error('Testdata count incorrect.');
const [input, output, time, score, memory] = f[i].split('|');
const cur = {
cases: [{ input, output }],