UNPKG

@ashnov/hephaestus-cli

Version:

Your AI powered CLI-based coding assistant. Powered by OpenAI davinci models.

86 lines (85 loc) 3.47 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import * as fs from 'node:fs/promises'; import { existsSync } from 'node:fs'; import * as os from 'node:os'; export class ConfigHandler { constructor() { } static getPath() { const CONFIG_FOLDER = 'hephaestus'; switch (os.type()) { case 'Darwin': case 'Linux': if (process.env.XDG_CONFIG_HOME) { ConfigHandler._path = process.env.XDG_CONFIG_HOME; } else { ConfigHandler._path = process.env.HOME; ConfigHandler._path += '/.config'; } break; case 'Windows_NT': ConfigHandler._path = process.env.APPDATA; break; default: throw new Error('PLATFORM_NOT_SUPPORTED'); } ConfigHandler._path += '/' + CONFIG_FOLDER; return ConfigHandler._path; } static saveConfig(response) { return __awaiter(this, void 0, void 0, function* () { // If OS is Darwin/Linux and process.env.XDG_CONFIG_HOME not set // Save the config in $HOME/.config // Create .config folder if doesn't exist already if ((os.type() === 'Darwin' || os.type() === 'Linux') && !process.env.XDG_CONFIG_HOME) { if (!existsSync(process.env.HOME + '/.config')) { yield fs.mkdir('.config'); } } let path = ConfigHandler.getPath(); const content = `apt-token=${response['api-token']} model=${response['model']}`; // Create hephaestus folder if doesn't exist already if (!existsSync(path)) { yield fs.mkdir(path); } // Write/Overwrite the config file path += '/.hephaestusrc'; yield fs.writeFile(path, content); }); } static fetchConfig() { return __awaiter(this, void 0, void 0, function* () { let path = ConfigHandler.getPath() + '/.hephaestusrc'; try { const data = yield fs.readFile(path, 'utf8'); if (data.length === 0) return undefined; const propVals = data.split('\n').map((prop) => { return prop.split('=')[1]; }); let config = { 'api-token': propVals[0], model: propVals[1], }; return config; } catch (e) { switch (e.errno) { case -2: // Return undefined when file does not exist return undefined; } } }); } }