1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var path = require("path");
|
4 | var os = require("os");
|
5 | var fs = require("fs-extra");
|
6 | var cuid = require("scuid");
|
7 | var findUp = require("find-up");
|
8 | var debug = require('debug')('config');
|
9 | var Config = (function () {
|
10 | function Config(options) {
|
11 | this.debug = Boolean(process.env.DEBUG && process.env.DEBUG.includes('*'));
|
12 | this.windows = false;
|
13 | this.bin = 'graphcool';
|
14 | this.mock = true;
|
15 | this.argv = process.argv.slice(1);
|
16 | this.commandsDir = path.join(__dirname, '../dist/commands');
|
17 | this.defaultCommand = 'help';
|
18 | this.userPlugins = false;
|
19 | this.version = '1.3.11';
|
20 | this.name = 'graphcool';
|
21 | this.pjson = {
|
22 | name: 'cli-engine',
|
23 | version: '0.0.0',
|
24 | dependencies: {},
|
25 | 'cli-engine': {
|
26 | defaultCommand: 'help',
|
27 | },
|
28 | };
|
29 | this.root = path.join(__dirname, '..');
|
30 | this.warnings = [];
|
31 | |
32 |
|
33 |
|
34 | this.statusEndpoint = 'https://crm.graph.cool/prod/status';
|
35 | this.cloudApiEndpoint = 'http://localhost:4000';
|
36 | this.consoleEndpoint = 'https://app.graph.cool';
|
37 |
|
38 | this.__cache = {};
|
39 | this.cwd = (options && options.cwd) || this.getCwd();
|
40 | this.home = (options && options.home) || this.getHome();
|
41 | debug("CWD", this.cwd);
|
42 | debug("HOME", this.home);
|
43 | this.setDefinitionPaths();
|
44 | this.setPaths();
|
45 | if (options) {
|
46 | this.readPackageJson(options);
|
47 | }
|
48 | }
|
49 | Config.prototype.setOutput = function (out) {
|
50 | this.out = out;
|
51 | this.warnings.forEach(function (warning) { return out.warn(warning); });
|
52 | this.warnings = [];
|
53 | };
|
54 | Object.defineProperty(Config.prototype, "arch", {
|
55 | get: function () {
|
56 | return os.arch() === 'ia32' ? 'x86' : os.arch();
|
57 | },
|
58 | enumerable: true,
|
59 | configurable: true
|
60 | });
|
61 | Object.defineProperty(Config.prototype, "platform", {
|
62 | get: function () {
|
63 | return os.platform() === 'win32' ? 'windows' : os.platform();
|
64 | },
|
65 | enumerable: true,
|
66 | configurable: true
|
67 | });
|
68 | Object.defineProperty(Config.prototype, "userAgent", {
|
69 | get: function () {
|
70 | return this.name + "/" + this.version + " (" + this.platform + "-" + this.arch + ") node-" + process.version;
|
71 | },
|
72 | enumerable: true,
|
73 | configurable: true
|
74 | });
|
75 | Object.defineProperty(Config.prototype, "dirname", {
|
76 | get: function () {
|
77 | return this.pjson['cli-engine'].dirname || this.bin;
|
78 | },
|
79 | enumerable: true,
|
80 | configurable: true
|
81 | });
|
82 | Object.defineProperty(Config.prototype, "cacheDir", {
|
83 | get: function () {
|
84 | var x = dir(this, 'cache', this.platform === 'darwin'
|
85 | ? path.join(this.home, 'Library', 'Caches')
|
86 | : null);
|
87 | return x;
|
88 | },
|
89 | enumerable: true,
|
90 | configurable: true
|
91 | });
|
92 | Object.defineProperty(Config.prototype, "requireCachePath", {
|
93 | get: function () {
|
94 | return path.join(this.cacheDir, '/.require-cache.json');
|
95 | },
|
96 | enumerable: true,
|
97 | configurable: true
|
98 | });
|
99 | Object.defineProperty(Config.prototype, "requestsCachePath", {
|
100 | get: function () {
|
101 | return path.join(this.cacheDir, '/.requests.json');
|
102 | },
|
103 | enumerable: true,
|
104 | configurable: true
|
105 | });
|
106 | Config.prototype.readPackageJson = function (options) {
|
107 | this.mock = options.mock;
|
108 | this.argv = options.argv || this.argv;
|
109 | if (options.root) {
|
110 | this.root = options.root;
|
111 | var pjsonPath = path.join(options.root, 'package.json');
|
112 | var pjson = fs.readJSONSync(pjsonPath);
|
113 | if (pjson && pjson['cli-engine']) {
|
114 | this.pjson = pjson;
|
115 | this.version = pjson.version;
|
116 | }
|
117 | }
|
118 | };
|
119 | Config.prototype.setPaths = function () {
|
120 | this.globalGraphcoolPath = path.join(this.home, '.graphcool/');
|
121 | this.globalConfigPath = path.join(this.home, '.graphcool/config.yml');
|
122 | this.globalClusterCachePath = path.join(this.home, '.graphcool/cache.yml');
|
123 | };
|
124 | Config.prototype.warn = function (msg) {
|
125 | this.warnings.push(msg);
|
126 | };
|
127 | Config.prototype.setDefinitionPaths = function () {
|
128 | var definitionPath = path.join(this.cwd, 'graphcool.yml');
|
129 | if (fs.pathExistsSync(definitionPath)) {
|
130 | this.definitionDir = this.cwd;
|
131 | this.definitionPath = definitionPath;
|
132 | }
|
133 | else {
|
134 | var found = findUp.sync('graphcool.yml', { cwd: this.cwd });
|
135 | this.definitionDir = found ? path.dirname(found) : this.cwd;
|
136 | this.definitionPath = found || null;
|
137 | }
|
138 | debug("definitionDir", this.definitionDir);
|
139 | debug("definitionPath", this.definitionPath);
|
140 | };
|
141 | Config.prototype.getCwd = function () {
|
142 |
|
143 | var cwd = process.cwd();
|
144 | if (process.env.NODE_ENV === 'test') {
|
145 | cwd = path.join(os.tmpdir(), cuid() + "/");
|
146 | fs.mkdirpSync(cwd);
|
147 | debug('cwd', cwd);
|
148 | }
|
149 | return cwd;
|
150 | };
|
151 | Config.prototype.getHome = function () {
|
152 |
|
153 | var home = os.homedir() || os.tmpdir();
|
154 | if (process.env.NODE_ENV === 'test') {
|
155 | home = path.join(os.tmpdir(), cuid() + "/");
|
156 | fs.mkdirpSync(home);
|
157 | debug('home', home);
|
158 | }
|
159 | return home;
|
160 | };
|
161 | return Config;
|
162 | }());
|
163 | exports.Config = Config;
|
164 | function dir(config, category, d) {
|
165 | var cacheKey = "dir:" + category;
|
166 | var cache = config.__cache[cacheKey];
|
167 | if (cache) {
|
168 | return cache;
|
169 | }
|
170 | d =
|
171 | d ||
|
172 | path.join(config.home, category === 'data' ? '.local/share' : '.' + category);
|
173 | if (config.windows) {
|
174 | d = process.env.LOCALAPPDATA || d;
|
175 | }
|
176 | d = process.env.XDG_DATA_HOME || d;
|
177 | d = path.join(d, config.dirname);
|
178 | fs.mkdirpSync(d);
|
179 | config.__cache[cacheKey] = d;
|
180 | return d;
|
181 | }
|
182 |
|
\ | No newline at end of file |