core/src/core/environment.ts

43 lines
1003 B
TypeScript

import * as fs from 'fs-extra';
import * as path from 'path';
import { IEnvironment } from '../types/environment';
const dirs: {[key: string]: string} = {
configurationPath: 'configs',
pluginsPath: 'plugins',
repositoryPath: 'repos',
};
export async function loadEnvironment(enviroFile: string = 'squeebot.env.json', chroot?: string): Promise<IEnvironment> {
if (!await fs.pathExists(enviroFile)) {
throw new Error('Environment file does not exist.');
}
const env = await fs.readJson(enviroFile);
if (!env.path && !chroot) {
throw new Error('Root path is unspecified.');
}
let root = env.path;
if (chroot) {
root = chroot;
env.path = chroot;
}
if (!await fs.pathExists(root)) {
throw new Error('Root path does not exist.');
}
// Ensure necessary directories exist
for (const opt in dirs) {
if (!env[opt]) {
env[opt] = dirs[opt];
}
env[opt] = path.resolve(root, dirs[opt]);
await fs.ensureDir(env[opt]);
}
return env;
}