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.ts

71 lines
1.8 KiB
TypeScript

5 years ago
/* eslint-disable import/no-dynamic-require */
/* eslint-disable no-await-in-loop */
/* eslint-disable no-eval */
import fs from 'fs';
import os from 'os';
import path from 'path';
import download from './download';
import { folderSize } from '../utils';
5 years ago
function root(name: string) {
5 years ago
return path.resolve(process.cwd(), name);
}
const moduleRoots = [
root('.build/module'),
root('module'),
root(path.resolve(os.homedir(), '.hydro', 'module')),
root('.'),
];
let moduleRoot: string;
for (const i of moduleRoots) {
if (fs.existsSync(i) && fs.statSync(i).isDirectory()) {
moduleRoot = i;
break;
}
}
export 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)) {
4 years ago
modules.push(file);
}
}
return modules;
}
export async function getDetail() {
4 years ago
const modules = [];
const files = fs.readdirSync(`${os.tmpdir()}/hydro/tmp`);
for (const file of files) {
const info = `${os.tmpdir()}/hydro/tmp/${file}/hydro.json`;
if (fs.existsSync(info)) {
const i = JSON.parse(fs.readFileSync(info).toString());
const size = folderSize(`${os.tmpdir()}/hydro/tmp/${file}`);
4 years ago
modules.push({
id: i.id,
version: i.version,
description: i.description,
size,
4 years ago
});
5 years ago
}
}
return modules;
5 years ago
}
export async function del(id: string) {
fs.unlinkSync(root(`${moduleRoot}/${id}.hydro`));
5 years ago
}
export function install(url: string) {
return download(url, root(`${moduleRoot}/${String.random(16)}.hydro`));
5 years ago
}
global.Hydro.lib.hpm = {
4 years ago
getInstalled, getDetail, del, install,
};