49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
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',
|
|
};
|
|
|
|
/**
|
|
* Load a Squeebot environment from a file
|
|
* @param enviroFile Environment JSON file
|
|
* @param chroot Change bot root to this instead of the path in the environment
|
|
* @returns Squeebot environment
|
|
*/
|
|
export async function loadEnvironment(enviroFile = '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;
|
|
}
|