UNPKG

@graphql-hive/cli

Version:

A CLI util to manage and control your GraphQL Hive

77 lines 2.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Config = exports.graphqlEndpoint = void 0; const tslib_1 = require("tslib"); const fs_1 = tslib_1.__importDefault(require("fs")); const path_1 = tslib_1.__importDefault(require("path")); const mkdirp_1 = tslib_1.__importDefault(require("mkdirp")); exports.graphqlEndpoint = 'https://app.graphql-hive.com/graphql'; class Config { constructor({ filepath, rootDir }) { if (filepath) { this.filepath = filepath; } else { this.filepath = path_1.default.join(rootDir, 'hive.json'); } } has(key) { const map = this.read(); return typeof map[key] !== 'undefined' && map[key] !== null; } get(key) { const map = this.read(); return map[key]; } set(key, value) { const map = this.read(); map[key] = value; this.write(map); } delete(key) { if (this.has(key)) { const map = this.read(); delete map[key]; this.write(map); } } clear() { try { mkdirp_1.default.sync(path_1.default.dirname(this.filepath)); } catch (_a) { } fs_1.default.writeFileSync(this.filepath, JSON.stringify({}), 'utf8'); } readSpace(content) { // eslint-disable-next-line no-process-env const space = process.env.HIVE_SPACE; if (space) { return content[space]; } if ('default' in content) { return content['default']; } return content; } read() { try { if (!this.cache) { this.cache = this.readSpace(JSON.parse(fs_1.default.readFileSync(this.filepath, 'utf-8'))); } } catch (error) { this.cache = {}; } return this.cache; } write(map) { this.cache = map; try { mkdirp_1.default.sync(path_1.default.dirname(this.filepath)); } catch (e) { } fs_1.default.writeFileSync(this.filepath, JSON.stringify(this.cache)); } } exports.Config = Config; //# sourceMappingURL=config.js.map