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]));
}