UNPKG

@chix/config

Version:

Configuration File Handler

80 lines 2.72 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var fs_extra_1 = require("fs-extra"); var lodash_1 = require("lodash"); var path_1 = require("path"); var util_1 = require("./util"); var version = '1.0.0'; var Config = (function () { function Config(options) { this.config = { version: version, }; this.path = options && options.configFile ? options.configFile : Config.globalConfigFile; this.config = this.readConfig(); if (!this.config.version) { throw Error('Unable to determine config version.'); } } Object.defineProperty(Config, "globalConfigDir", { get: function () { return util_1.configPath('chix'); }, enumerable: true, configurable: true }); Object.defineProperty(Config, "globalConfigFile", { get: function () { return path_1.join(Config.globalConfigDir, 'config.json'); }, enumerable: true, configurable: true }); Config.hasGlobalConfigFile = function () { return fs_extra_1.pathExistsSync(Config.globalConfigFile); }; Config.createGlobalConfigFile = function () { fs_extra_1.ensureDirSync(this.globalConfigDir); if (!Config.hasGlobalConfigFile()) { Config.write(this.globalConfigFile, { version: version }); } }; Config.writeGlobalConfig = function (config) { Config.write(Config.globalConfigFile, __assign(__assign({}, config), { version: version })); }; Config.write = function (path, config) { fs_extra_1.writeFileSync(path, JSON.stringify(config, null, 2)); }; Config.prototype.set = function (path, value) { lodash_1.set(this.config, path, value); }; Config.prototype.get = function (path) { return lodash_1.get(this.config, path); }; Config.prototype.has = function (path) { return lodash_1.has(this.config, path); }; Config.prototype.write = function () { Config.write(this.path, this.config); }; Config.prototype.readConfig = function () { return JSON.parse(fs_extra_1.readFileSync(this.path, 'utf-8')); }; return Config; }()); exports.Config = Config; //# sourceMappingURL=config.js.map