You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/hydro/lib/hpm.js

56 lines
1.5 KiB
JavaScript

5 years ago
/* eslint-disable import/no-dynamic-require */
/* eslint-disable no-await-in-loop */
/* eslint-disable no-eval */
const fs = require('fs');
const os = require('os');
5 years ago
const path = require('path');
const download = require('./download');
function root(name) {
return path.resolve(process.cwd(), name);
}
const moduleRoots = [
root('.build/module'),
root('module'),
root(path.resolve(os.homedir(), '.hydro', 'module')),
root('.'),
];
let moduleRoot;
for (const i of moduleRoots) {
if (fs.existsSync(i) && fs.statSync(i).isDirectory()) {
moduleRoot = i;
break;
}
}
5 years ago
async function getInstalled() {
const modules = [];
4 years ago
const files = fs.readdirSync(`${os.tmpdir()}/hydro/tmp`);
5 years ago
for (const file of files) {
4 years ago
const info = `${os.tmpdir()}/hydro/tmp/${file}/hydro.json`;
if (fs.existsSync(info)) {
try {
4 years ago
const m = file;
const t = JSON.parse(fs.readFileSync(info).toString());
modules.push(`${m}|${t.id}`);
} catch (e) {
if (e.code === 'Z_DATA_ERROR') {
console.error(`Module Load Fail: ${file} (File Corrupted)`);
} else console.error(`Module Load Fail: ${file} ${e}`);
}
5 years ago
}
}
return modules;
5 years ago
}
async function del(id) {
fs.unlinkSync(root(`${moduleRoot}/${id}.hydro`));
5 years ago
}
async function install(url) {
await download(url, root(`${moduleRoot}/${String.random(16)}.hydro`));
5 years ago
}
global.Hydro.lib.hpm = module.exports = { getInstalled, del, install };