UNPKG

steadybit

Version:

Command-line interface to interact with the Steadybit API

114 lines 5.44 kB
"use strict"; // SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: 2022 Steadybit GmbH 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addProfile = addProfile; exports.removeProfile = removeProfile; exports.getProfiles = getProfiles; exports.getActiveProfile = getActiveProfile; exports.setActiveProfile = setActiveProfile; const os_1 = require("os"); const promises_1 = __importDefault(require("fs/promises")); const path_1 = __importDefault(require("path")); const errors_1 = require("../../errors"); const configDir = path_1.default.join((0, os_1.homedir)(), '.steadybit'); const profilesFile = path_1.default.join(configDir, 'profiles.json'); const activeProfileFile = path_1.default.join(configDir, 'activeProfile'); function addProfile(profile) { return __awaiter(this, void 0, void 0, function* () { const profiles = yield getProfiles(); const updatedProfiles = profiles.filter(p => p.name !== profile.name).concat(profile); yield writeProfiles(updatedProfiles); }); } function removeProfile(profileName) { return __awaiter(this, void 0, void 0, function* () { const profiles = yield getProfiles(); const updatedProfiles = profiles.filter(p => p.name !== profileName); yield writeProfiles(updatedProfiles); }); } function getProfiles() { return __awaiter(this, void 0, void 0, function* () { var _a, _b; yield ensureConfigDirectoryExists(); let fileContent; try { fileContent = yield promises_1.default.readFile(profilesFile, { encoding: 'utf8' }); } catch (e) { if ((e === null || e === void 0 ? void 0 : e.code) === 'ENOENT') { return []; } throw (0, errors_1.abortExecution)("Failed to read file '%s': %s", profilesFile, (_a = e === null || e === void 0 ? void 0 : e.message) !== null && _a !== void 0 ? _a : 'Unknown error'); } try { return JSON.parse(fileContent); } catch (e) { throw (0, errors_1.abortExecution)("Failed to parse file '%s' as JSON: %s", profilesFile, (_b = e === null || e === void 0 ? void 0 : e.message) !== null && _b !== void 0 ? _b : 'Unknown error'); } }); } function writeProfiles(profiles) { return __awaiter(this, void 0, void 0, function* () { var _a; yield ensureConfigDirectoryExists(); try { yield promises_1.default.writeFile(profilesFile, JSON.stringify(profiles, undefined, 2)); } catch (e) { throw (0, errors_1.abortExecution)("Failed to write to file '%s': %s", profilesFile, (_a = e === null || e === void 0 ? void 0 : e.message) !== null && _a !== void 0 ? _a : 'Unknown error'); } }); } function ensureConfigDirectoryExists() { return __awaiter(this, void 0, void 0, function* () { yield promises_1.default.mkdir(configDir, { recursive: true }); }); } function getActiveProfile() { return __awaiter(this, void 0, void 0, function* () { var _a, _b; yield ensureConfigDirectoryExists(); let activeProfileName; try { activeProfileName = yield promises_1.default.readFile(activeProfileFile, { encoding: 'utf8' }); // Users opening and saving the file might end up adding a trailing new line character. activeProfileName = activeProfileName.trim(); } catch (e) { if ((e === null || e === void 0 ? void 0 : e.code) !== 'ENOENT') { throw (0, errors_1.abortExecution)("Failed to read file '%s': %s", profilesFile, (_a = e === null || e === void 0 ? void 0 : e.message) !== null && _a !== void 0 ? _a : 'Unknown error'); } } const profiles = yield getProfiles(); const activeProfile = (_b = profiles.find(p => p.name === (activeProfileName === null || activeProfileName === void 0 ? void 0 : activeProfileName.trim()))) !== null && _b !== void 0 ? _b : profiles[0]; return activeProfile; }); } function setActiveProfile(profileName) { return __awaiter(this, void 0, void 0, function* () { var _a; yield ensureConfigDirectoryExists(); try { yield promises_1.default.writeFile(activeProfileFile, profileName); } catch (e) { throw (0, errors_1.abortExecution)("Failed to write to file '%s': %s", activeProfileFile, (_a = e === null || e === void 0 ? void 0 : e.message) !== null && _a !== void 0 ? _a : 'Unknown error'); } }); } //# sourceMappingURL=service.js.map