UNPKG

2.05 kBJavaScriptView Raw
1"use strict";
2var path = require("path");
3var fs = require("fs");
4var utils_1 = require("./utils");
5var Config = (function () {
6 function Config() {
7 this.homeFileName = process.platform === "win32" ? ".cloudstitch" : "cloudstitch.json";
8 this.localFileName = ".cloudstitch";
9 this.homePath = utils_1.homePath;
10 var fileDetails = utils_1.readFromParent(process.cwd(), this.localFileName);
11 this.localConfig = fileDetails.fileJson || {};
12 this.localConfigFile = fileDetails.filePath || path.join(process.cwd(), this.localFileName);
13 this.homeConfig = this._readHomeConfig() || {};
14 }
15 Config.prototype._readHomeConfig = function () {
16 var homeConfigPath = path.join(this.homePath, ".config");
17 if (process.platform !== "win32" && !utils_1.isDirectory(homeConfigPath)) {
18 fs.mkdirSync(homeConfigPath);
19 }
20 this.homeConfigFile = path.join(process.platform === "win32" ? this.homePath : homeConfigPath, this.homeFileName);
21 if (utils_1.isFile(this.homeConfigFile)) {
22 return utils_1.loadJson(this.homeConfigFile);
23 }
24 else {
25 return {};
26 }
27 };
28 Config.prototype.get = function (key) {
29 if (this.localConfig && this.localConfig[key]) {
30 return this.localConfig[key];
31 }
32 else if (this.homeConfig && this.homeConfig[key]) {
33 return this.homeConfig[key];
34 }
35 else {
36 return undefined;
37 }
38 };
39 Config.prototype.set = function (key, value, local) {
40 if (local === void 0) { local = false; }
41 if (local && this.localConfigFile) {
42 this.localConfig[key] = value;
43 fs.writeFileSync(this.localConfigFile, JSON.stringify(this.localConfig));
44 }
45 else {
46 this.homeConfig[key] = value;
47 fs.writeFileSync(this.homeConfigFile, JSON.stringify(this.homeConfig));
48 }
49 };
50 return Config;
51}());
52exports.Config = Config;
53exports.instance = new Config();