1 | "use strict";
|
2 | var __assign = (this && this.__assign) || Object.assign || function(t) {
|
3 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
4 | s = arguments[i];
|
5 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
6 | t[p] = s[p];
|
7 | }
|
8 | return t;
|
9 | };
|
10 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
11 | return new (P || (P = Promise))(function (resolve, reject) {
|
12 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
13 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
14 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
15 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
16 | });
|
17 | };
|
18 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
19 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
20 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
21 | function verb(n) { return function (v) { return step([n, v]); }; }
|
22 | function step(op) {
|
23 | if (f) throw new TypeError("Generator is already executing.");
|
24 | while (_) try {
|
25 | if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
|
26 | if (y = 0, t) op = [0, t.value];
|
27 | switch (op[0]) {
|
28 | case 0: case 1: t = op; break;
|
29 | case 4: _.label++; return { value: op[1], done: false };
|
30 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
31 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
32 | default:
|
33 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
34 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
35 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
36 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
37 | if (t[2]) _.ops.pop();
|
38 | _.trys.pop(); continue;
|
39 | }
|
40 | op = body.call(thisArg, _);
|
41 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
42 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
43 | }
|
44 | };
|
45 | Object.defineProperty(exports, "__esModule", { value: true });
|
46 | var yaml = require("js-yaml");
|
47 | var path = require("path");
|
48 | var fs_1 = require("./fs");
|
49 | var lodash_1 = require("lodash");
|
50 | var Variables_1 = require("./ProjectDefinition/Variables");
|
51 | var debug = require('debug')('environment');
|
52 | var stringSimilarity = require("string-similarity");
|
53 | var chalk_1 = require("chalk");
|
54 | var defaultRC = {
|
55 | clusters: {
|
56 | default: 'shared-eu-west-1',
|
57 | },
|
58 | };
|
59 | var Environment = (function () {
|
60 | function Environment(out, config) {
|
61 | var _this = this;
|
62 | this.localRC = {};
|
63 | this.globalRC = {};
|
64 | this.activeCluster = 'shared-eu-west-1';
|
65 | this.warningCache = {};
|
66 | this.resolveTargetAliases = function (targets, allTargets) {
|
67 | return lodash_1.mapValues(targets, function (target) {
|
68 | return _this.isTargetAlias(target)
|
69 | ? _this.resolveTarget(target, allTargets)
|
70 | : target;
|
71 | });
|
72 | };
|
73 | this.isTargetAlias = function (target) {
|
74 | return typeof target === 'string' && target.split('/').length === 1;
|
75 | };
|
76 | this.resolveTarget = function (target, targets) {
|
77 | return targets[target] ? _this.resolveTarget(targets[target], targets) : target;
|
78 | };
|
79 | this.subscriptionURL = function (_a) {
|
80 | var region = _a.region, projectId = _a.projectId, localAddr = _a.localAddr, localPort = _a.localPort;
|
81 | return localPort
|
82 | ? "ws://" + localAddr + ":" + localPort + "/subscriptions/v1/" + projectId
|
83 | : subscriptionEndpoints[region] + "/v1/" + projectId;
|
84 | };
|
85 | this.out = out;
|
86 | this.config = config;
|
87 | this.migrateOldFormat();
|
88 | }
|
89 | Object.defineProperty(Environment.prototype, "rc", {
|
90 | get: function () {
|
91 |
|
92 | return this.deserializeRCs(this.localRC, this.globalRC, this.config.localRCPath, this.config.globalRCPath);
|
93 | },
|
94 | enumerable: true,
|
95 | configurable: true
|
96 | });
|
97 | Object.defineProperty(Environment.prototype, "token", {
|
98 | get: function () {
|
99 | if (process.env.GRAPHCOOL_PLATFORM_TOKEN) {
|
100 | debug('taking process.env.GRAPHCOOL_PLATFORM_TOKEN as the token');
|
101 | return process.env.GRAPHCOOL_PLATFORM_TOKEN;
|
102 | }
|
103 | if (this.isSharedCluster(this.activeCluster)) {
|
104 | return this.rc.platformToken;
|
105 | }
|
106 | return this.rc.clusters[this.activeCluster].clusterSecret;
|
107 | },
|
108 | enumerable: true,
|
109 | configurable: true
|
110 | });
|
111 | Object.defineProperty(Environment.prototype, "default", {
|
112 | get: function () {
|
113 | if (this.rc.targets && this.rc.targets.default) {
|
114 | return this.rc.targets.default;
|
115 | }
|
116 | return null;
|
117 | },
|
118 | enumerable: true,
|
119 | configurable: true
|
120 | });
|
121 | Object.defineProperty(Environment.prototype, "allClusters", {
|
122 | get: function () {
|
123 | var localClusters = this.rc.clusters ? Object.keys(this.rc.clusters) : [];
|
124 | return this.config.sharedClusters.concat(localClusters);
|
125 | },
|
126 | enumerable: true,
|
127 | configurable: true
|
128 | });
|
129 | Environment.prototype.checkCluster = function (cluster) {
|
130 | var allClusters = this.config.sharedClusters.concat(Object.keys(this.rc.clusters || {}));
|
131 | if (!allClusters.includes(cluster)) {
|
132 | if (cluster === 'local') {
|
133 | this.out.log("You chose the cluster " + chalk_1.default.bold('local') + ", but don't have docker initialized, yet.\nPlease run " + chalk_1.default.green('$ graphcool-framework local up') + " to get a local Graphcool cluster.\n");
|
134 | this.out.exit(1);
|
135 | }
|
136 | var bestMatch = stringSimilarity.findBestMatch(cluster, allClusters)
|
137 | .bestMatch.target;
|
138 | this.out.error(cluster + " is not a valid cluster. Did you mean " + bestMatch + "?");
|
139 | }
|
140 | };
|
141 | Environment.prototype.setLocalTarget = function (name, value) {
|
142 | if (value.split('/').length !== 2) {
|
143 | this.out.error("Invalid target " + name + " " + value);
|
144 | }
|
145 | if (!this.localRC.targets) {
|
146 | this.localRC.targets = {};
|
147 | }
|
148 | this.localRC.targets[name] = value;
|
149 | };
|
150 | Environment.prototype.setLocalDefaultTarget = function (value) {
|
151 | if (!this.localRC.targets) {
|
152 | this.localRC.targets = {};
|
153 | }
|
154 | this.localRC.targets.default = value;
|
155 | };
|
156 | Environment.prototype.getTarget = function (targetName, silent) {
|
157 | var target = null;
|
158 | if (targetName && targetName.split('/').length > 1) {
|
159 | target = this.deserializeTarget(targetName);
|
160 | }
|
161 | target = targetName || (this.rc.targets && this.rc.targets.default);
|
162 | if (typeof target === 'string' && this.rc.targets) {
|
163 | target = this.rc.targets[target];
|
164 | }
|
165 | if (target) {
|
166 | this.setActiveCluster(target.cluster);
|
167 | }
|
168 | else if (!silent) {
|
169 | this.out.error('Please provide a valid target that points to a valid cluster and service id');
|
170 | }
|
171 | return target;
|
172 | };
|
173 | Environment.prototype.getTargetWithName = function (targetName) {
|
174 | var target = null;
|
175 | var name = null;
|
176 | if (targetName && targetName.split('/').length > 1) {
|
177 | target = this.deserializeTarget(targetName);
|
178 | }
|
179 | else {
|
180 | if (targetName) {
|
181 | target = targetName;
|
182 | name = targetName;
|
183 | }
|
184 | else if (this.rc.targets && this.rc.targets.default) {
|
185 | target = this.rc.targets.default;
|
186 | name =
|
187 | (this.localRC.targets && this.localRC.targets.default) ||
|
188 | (this.globalRC.targets && this.globalRC.targets.default) ||
|
189 | null;
|
190 | name = name && name.split('/').length === 1 ? name : null;
|
191 | }
|
192 | if (typeof target === 'string' && this.rc.targets) {
|
193 | target = this.rc.targets[target];
|
194 | }
|
195 | }
|
196 | if (target) {
|
197 | this.setActiveCluster(target.cluster);
|
198 | }
|
199 | return {
|
200 | target: target,
|
201 | targetName: name,
|
202 | };
|
203 | };
|
204 | Environment.prototype.getDefaultTargetName = function (cluster) {
|
205 | var targetName = this.isSharedCluster(cluster) ? 'prod' : 'dev';
|
206 | if (!this.rc.targets || !this.rc.targets[targetName]) {
|
207 | return targetName;
|
208 | }
|
209 | else {
|
210 | var count = 1;
|
211 | while (this.rc.targets[targetName + count]) {
|
212 | count++;
|
213 | }
|
214 | return targetName + count;
|
215 | }
|
216 | };
|
217 | Environment.prototype.setActiveCluster = function (cluster) {
|
218 | this.checkCluster(cluster);
|
219 | this.activeCluster = cluster;
|
220 | };
|
221 | Environment.prototype.isSharedCluster = function (cluster) {
|
222 | return this.config.sharedClusters.includes(cluster);
|
223 | };
|
224 | Environment.prototype.deleteIfExist = function (serviceIds) {
|
225 | var _this = this;
|
226 | serviceIds.forEach(function (id) {
|
227 | var localTarget = Object.keys(_this.localRC.targets || {}).find(function (name) { return _this.localRC.targets[name].split('/')[1] === id; });
|
228 | if (localTarget) {
|
229 | delete _this.localRC[localTarget];
|
230 | }
|
231 | var globalTarget = Object.keys(_this.globalRC.targets || {}).find(function (name) { return _this.globalRC.targets[name].split('/')[1] === id; });
|
232 | if (globalTarget) {
|
233 | delete _this.globalRC[globalTarget];
|
234 | }
|
235 | });
|
236 | };
|
237 | |
238 |
|
239 |
|
240 | Environment.prototype.migrateOldFormat = function () {
|
241 | this.migrateGlobalFiles();
|
242 | this.migrateLocalFile();
|
243 | this.migrateClusters(this.config.localRCPath);
|
244 | this.migrateClusters(this.config.globalRCPath);
|
245 | };
|
246 | Environment.prototype.migrateLocalFile = function () {
|
247 | if (fs_1.default.pathExistsSync(this.config.localRCPath)) {
|
248 | var file = fs_1.default.readFileSync(this.config.localRCPath, 'utf-8');
|
249 | var content = void 0;
|
250 | try {
|
251 | content = yaml.safeLoad(file);
|
252 |
|
253 | if (content.environments) {
|
254 | var newLocalRcJson = {
|
255 | targets: lodash_1.mapValues(content.environments, function (env) {
|
256 | return "shared-eu-west-1/" + env;
|
257 | }),
|
258 | clusters: {},
|
259 | };
|
260 | if (content.default) {
|
261 | newLocalRcJson.targets.default = content.default;
|
262 | }
|
263 | var newLocalRcYaml = yaml.safeDump(newLocalRcJson);
|
264 | var oldPath = path.join(this.config.cwd, '.graphcoolrc.old');
|
265 | fs_1.default.moveSync(this.config.localRCPath, oldPath);
|
266 | fs_1.default.writeFileSync(this.config.localRCPath, newLocalRcYaml);
|
267 | this.out.warn("We detected the old definition format of the " + this.config.localRCPath + " file.\nIt has been renamed to " + oldPath + ". The up-to-date format has been written to " + this.config.localRCPath + ".\nRead more about the changes here:\nhttps://github.com/graphcool/framework/issues/714\n");
|
268 | }
|
269 | }
|
270 | catch (e) { }
|
271 | }
|
272 | };
|
273 | Environment.prototype.migrateClusters = function (rcPath) {
|
274 | if (fs_1.default.pathExistsSync(rcPath)) {
|
275 | var file = fs_1.default.readFileSync(rcPath, 'utf-8');
|
276 | var content_1;
|
277 | try {
|
278 | content_1 = yaml.safeLoad(file);
|
279 | if (content_1.clusters &&
|
280 | Object.keys(content_1.clusters).find(function (c) { return content_1.clusters[c].token; })) {
|
281 | var newRcJson = __assign({}, content_1, { clusters: lodash_1.mapValues(content_1.clusters, function (c) {
|
282 | return typeof c === 'string'
|
283 | ? c
|
284 | : {
|
285 | clusterSecret: c.token,
|
286 | host: c.host,
|
287 | };
|
288 | }) });
|
289 | var newLocalRcYaml = yaml.safeDump(newRcJson);
|
290 | var oldPath = path.join(path.dirname(rcPath), '.graphcoolrc.old');
|
291 | fs_1.default.moveSync(rcPath, oldPath);
|
292 | fs_1.default.writeFileSync(rcPath, newLocalRcYaml);
|
293 | this.out.warn("We detected the old definition format of " + chalk_1.default.bold('clusters') + " in the " + rcPath + " file.\n" + chalk_1.default.bold('token') + " has been renamed to " + chalk_1.default.bold('clusterSecret') + ".\nIt has been renamed to " + oldPath + ". The up-to-date format has been written to " + rcPath + ".\n");
|
294 | }
|
295 | if (content_1.clusters &&
|
296 | Object.keys(content_1.clusters).find(function (c) {
|
297 | return typeof content_1.clusters[c] !== 'string' &&
|
298 | !content_1.clusters[c].faasHost;
|
299 | })) {
|
300 | var newRcJson = __assign({}, content_1, { clusters: lodash_1.mapValues(content_1.clusters, function (c) {
|
301 | return typeof c === 'string'
|
302 | ? c
|
303 | : __assign({}, c, { faasHost: 'http://localhost:60050' });
|
304 | }) });
|
305 | var newLocalRcYaml = yaml.safeDump(newRcJson);
|
306 | fs_1.default.writeFileSync(rcPath, newLocalRcYaml);
|
307 | this.out.warn("We detected the old definition format of " + chalk_1.default.bold('clusters') + " in the " + rcPath + " file. A new field called " + chalk_1.default.bold('faasHost') + " has been added, which contains the address to the new local function runtime.");
|
308 | }
|
309 | }
|
310 | catch (e) { }
|
311 | }
|
312 | };
|
313 | Environment.prototype.migrateGlobalFiles = function () {
|
314 | var dotFilePath = path.join(this.config.home, '.graphcool');
|
315 | var dotExists = fs_1.default.pathExistsSync(dotFilePath);
|
316 | if (fs_1.default.lstatSync(dotFilePath).isDirectory()) {
|
317 | return;
|
318 | }
|
319 | var rcHomePath = path.join(this.config.home, '.graphcoolrc');
|
320 | var rcHomeExists = fs_1.default.pathExistsSync(rcHomePath);
|
321 | var dotFile = dotExists ? fs_1.default.readFileSync(dotFilePath, 'utf-8') : null;
|
322 | var rcFile = rcHomeExists ? fs_1.default.readFileSync(rcHomePath, 'utf-8') : null;
|
323 |
|
324 | if (rcHomeExists && rcFile) {
|
325 |
|
326 |
|
327 | try {
|
328 | var rcJson = JSON.parse(rcFile);
|
329 | if (Object.keys(rcJson).length === 1 && rcJson.token) {
|
330 | this.out.warn("Moved deprecated file " + rcHomePath + " to .graphcoolrc.old");
|
331 | fs_1.default.moveSync(rcHomePath, path.join(this.config.home, '.graphcoolrc.old'));
|
332 | }
|
333 | }
|
334 | catch (e) {
|
335 |
|
336 | }
|
337 | }
|
338 | if (dotExists) {
|
339 | if (dotFile) {
|
340 | try {
|
341 | var dotJson = JSON.parse(dotFile);
|
342 | if (dotJson.token) {
|
343 | var rc = __assign({}, defaultRC, { platformToken: dotJson.token });
|
344 | var rcSerialized = this.serializeRC(rc);
|
345 | var oldPath = path.join(this.config.home, '.graphcool.old');
|
346 | fs_1.default.moveSync(dotFilePath, oldPath);
|
347 | debug("Writing", rcHomePath, rcSerialized);
|
348 | fs_1.default.writeFileSync(rcHomePath, rcSerialized);
|
349 | var READ = fs_1.default.readFileSync(rcHomePath, 'utf-8');
|
350 | debug('YES', READ);
|
351 | this.out
|
352 | .warn("We detected the old definition format of the " + dotFilePath + " file.\nIt has been renamed to " + oldPath + ". The new file is called " + rcHomePath + ".\nRead more about the changes here:\nhttps://github.com/graphcool/framework/issues/714\n");
|
353 | }
|
354 | }
|
355 | catch (e) {
|
356 |
|
357 | }
|
358 | }
|
359 | }
|
360 | else if (rcHomeExists && rcFile) {
|
361 | try {
|
362 | var rcJson = JSON.parse(rcFile);
|
363 | var rc = __assign({}, defaultRC, { platformToken: rcJson.token });
|
364 | var rcSerialized = this.serializeRC(rc);
|
365 | fs_1.default.writeFileSync(rcHomePath, rcSerialized);
|
366 | }
|
367 | catch (e) {
|
368 |
|
369 | }
|
370 | }
|
371 | };
|
372 | Environment.prototype.loadYaml = function (file, filePath) {
|
373 | if (filePath === void 0) { filePath = null; }
|
374 | return __awaiter(this, void 0, void 0, function () {
|
375 | var content, variables;
|
376 | return __generator(this, function (_a) {
|
377 | switch (_a.label) {
|
378 | case 0:
|
379 | if (!file) return [3 , 2];
|
380 | content = void 0;
|
381 | try {
|
382 | content = yaml.safeLoad(file);
|
383 | }
|
384 | catch (e) {
|
385 | this.out.error("Yaml parsing error in " + filePath + ": " + e.message);
|
386 | }
|
387 | variables = new Variables_1.default(this.out, filePath || 'no filepath provided', this.args);
|
388 | return [4 , variables.populateJson(content)];
|
389 | case 1:
|
390 | content = _a.sent();
|
391 | return [2 , content];
|
392 | case 2: return [2 , {}];
|
393 | }
|
394 | });
|
395 | });
|
396 | };
|
397 | Environment.prototype.load = function (args) {
|
398 | return __awaiter(this, void 0, void 0, function () {
|
399 | var localFile, globalFile;
|
400 | return __generator(this, function (_a) {
|
401 | switch (_a.label) {
|
402 | case 0:
|
403 | localFile = this.config.localRCPath && fs_1.default.pathExistsSync(this.config.localRCPath)
|
404 | ? fs_1.default.readFileSync(this.config.localRCPath, 'utf-8')
|
405 | : null;
|
406 | globalFile = this.config.globalRCPath && fs_1.default.pathExistsSync(this.config.globalRCPath)
|
407 | ? fs_1.default.readFileSync(this.config.globalRCPath, 'utf-8')
|
408 | : null;
|
409 | return [4 , this.loadRCs(localFile, globalFile, args)];
|
410 | case 1:
|
411 | _a.sent();
|
412 | if (process.env.NODE_ENV === 'test') {
|
413 | this.setTestToken();
|
414 | }
|
415 | return [2 ];
|
416 | }
|
417 | });
|
418 | });
|
419 | };
|
420 | Environment.prototype.loadRCs = function (localFile, globalFile, args) {
|
421 | if (args === void 0) { args = {}; }
|
422 | return __awaiter(this, void 0, void 0, function () {
|
423 | var _a, _b;
|
424 | return __generator(this, function (_c) {
|
425 | switch (_c.label) {
|
426 | case 0:
|
427 | this.args = args;
|
428 | _a = this;
|
429 | return [4 , this.loadYaml(localFile, this.config.localRCPath)];
|
430 | case 1:
|
431 | _a.localRC = _c.sent();
|
432 | _b = this;
|
433 | return [4 , this.loadYaml(globalFile, this.config.globalRCPath)];
|
434 | case 2:
|
435 | _b.globalRC = _c.sent();
|
436 | if (this.rc.clusters && this.rc.clusters.default) {
|
437 | if (!this.allClusters.includes(this.rc.clusters.default)) {
|
438 | this.out.error("Could not find default cluster " + this.rc.clusters.default);
|
439 | }
|
440 | this.activeCluster = this.rc.clusters.default;
|
441 | }
|
442 | return [2 ];
|
443 | }
|
444 | });
|
445 | });
|
446 | };
|
447 | Environment.prototype.deserializeRCs = function (localFile, globalFile, localFilePath, globalFilePath) {
|
448 | var allTargets = __assign({}, localFile.targets, globalFile.targets);
|
449 | var newLocalFile = __assign({}, localFile);
|
450 | var newGlobalFile = __assign({}, globalFile);
|
451 |
|
452 |
|
453 | newGlobalFile.targets = this.resolveTargetAliases(newGlobalFile.targets, newGlobalFile.targets);
|
454 |
|
455 | for (var i = 0; i < 2; i++) {
|
456 |
|
457 | newLocalFile.targets = this.resolveTargetAliases(newLocalFile.targets, allTargets);
|
458 | allTargets = __assign({}, newLocalFile.targets, newGlobalFile.targets);
|
459 | }
|
460 |
|
461 |
|
462 | newLocalFile.targets = this.deserializeTargets(newLocalFile.targets, localFilePath);
|
463 | newGlobalFile.targets = this.deserializeTargets(newGlobalFile.targets, globalFilePath);
|
464 |
|
465 | var allClusters = this.config.sharedClusters.concat(Object.keys(newGlobalFile.clusters || {}), Object.keys(newLocalFile.clusters || {}));
|
466 | this.checkClusters(newLocalFile.targets, allClusters, localFilePath);
|
467 | this.checkClusters(newGlobalFile.targets, allClusters, globalFilePath);
|
468 | return lodash_1.merge({}, newGlobalFile, newLocalFile);
|
469 | };
|
470 | Environment.prototype.checkClusters = function (targets, clusters, filePath) {
|
471 | var _this = this;
|
472 | Object.keys(targets).forEach(function (key) {
|
473 | var target = targets[key];
|
474 | if (!clusters.includes(target.cluster) &&
|
475 | !_this.warningCache[target.cluster]) {
|
476 | _this.warningCache[target.cluster] = true;
|
477 | if (target.cluster === 'local') {
|
478 | _this.out.warn("Could not find cluster " + target.cluster + " defined for target " + key + " in " + filePath + ".\nPlease run " + chalk_1.default.bold('graphcool local up') + " to start the local cluster.");
|
479 | }
|
480 | else {
|
481 | _this.out.error("Could not find cluster " + target.cluster + " defined for target " + key + " in " + filePath);
|
482 | }
|
483 | }
|
484 | });
|
485 | };
|
486 | Environment.prototype.deserializeTargets = function (targets, filePath) {
|
487 | var _this = this;
|
488 | return lodash_1.mapValues(targets, function (target) {
|
489 | return _this.deserializeTarget(target, filePath);
|
490 | });
|
491 | };
|
492 | Environment.prototype.deserializeTarget = function (target, filePath) {
|
493 | if (filePath === void 0) { filePath = null; }
|
494 | var splittedTarget = target.split('/');
|
495 | if (splittedTarget.length === 1) {
|
496 | this.out.error("Could not parse target " + target + " in " + filePath);
|
497 | }
|
498 | return {
|
499 | cluster: splittedTarget[0],
|
500 | id: splittedTarget[1],
|
501 | };
|
502 | };
|
503 | Environment.prototype.serializeRC = function (rc) {
|
504 |
|
505 |
|
506 |
|
507 |
|
508 | return yaml.safeDump(rc);
|
509 | };
|
510 |
|
511 |
|
512 |
|
513 |
|
514 | Environment.prototype.setToken = function (token) {
|
515 | this.globalRC.platformToken = token;
|
516 | };
|
517 | Environment.prototype.saveLocalRC = function () {
|
518 | var file = this.serializeRC(this.localRC);
|
519 | fs_1.default.writeFileSync(this.config.localRCPath, file);
|
520 | };
|
521 | Environment.prototype.saveGlobalRC = function () {
|
522 | var file = this.serializeRC(this.globalRC);
|
523 | fs_1.default.writeFileSync(this.config.globalRCPath, file);
|
524 | };
|
525 | Environment.prototype.save = function () {
|
526 | this.saveLocalRC();
|
527 | this.saveGlobalRC();
|
528 | };
|
529 | Environment.prototype.setGlobalCluster = function (name, cluster) {
|
530 | if (!this.globalRC.clusters) {
|
531 | this.globalRC.clusters = {};
|
532 | }
|
533 | this.globalRC.clusters[name] = cluster;
|
534 | };
|
535 | Environment.prototype.setLocalDefaultCluster = function (cluster) {
|
536 | if (!this.globalRC.clusters) {
|
537 | this.globalRC.clusters = {};
|
538 | }
|
539 | this.globalRC.clusters.default = cluster;
|
540 | };
|
541 | Environment.prototype.getRegionFromCluster = function (cluster) {
|
542 | if (this.isSharedCluster(cluster)) {
|
543 | return cluster
|
544 | .slice(7)
|
545 | .replace(/-/g, '_')
|
546 | .toUpperCase();
|
547 | }
|
548 | else {
|
549 | return 'EU_WEST_1';
|
550 | }
|
551 | };
|
552 | Object.defineProperty(Environment.prototype, "clusterEndpoint", {
|
553 | get: function () {
|
554 | if (this.isSharedCluster(this.activeCluster)) {
|
555 | return this.config.systemAPIEndpoint;
|
556 | }
|
557 | return this.rc.clusters[this.activeCluster].host + '/system';
|
558 | },
|
559 | enumerable: true,
|
560 | configurable: true
|
561 | });
|
562 | Environment.prototype.simpleEndpoint = function (projectId) {
|
563 | if (this.isSharedCluster(this.activeCluster)) {
|
564 | return this.config.simpleAPIEndpoint + projectId;
|
565 | }
|
566 | return (this.rc.clusters[this.activeCluster].host +
|
567 | '/simple/v1/' +
|
568 | projectId);
|
569 | };
|
570 | Environment.prototype.importEndpoint = function (projectId) {
|
571 | if (this.isSharedCluster(this.activeCluster)) {
|
572 | return this.config.simpleAPIEndpoint + projectId + '/import';
|
573 | }
|
574 | return (this.rc.clusters[this.activeCluster].host +
|
575 | '/simple/v1/' +
|
576 | projectId +
|
577 | '/import');
|
578 | };
|
579 | Environment.prototype.exportEndpoint = function (projectId) {
|
580 | if (this.isSharedCluster(this.activeCluster)) {
|
581 | return this.config.simpleAPIEndpoint + projectId + '/export';
|
582 | }
|
583 | return (this.rc.clusters[this.activeCluster].host +
|
584 | '/simple/v1/' +
|
585 | projectId +
|
586 | '/export');
|
587 | };
|
588 | Environment.prototype.relayEndpoint = function (projectId) {
|
589 | if (this.isSharedCluster(this.activeCluster)) {
|
590 | return this.config.relayAPIEndpoint + projectId;
|
591 | }
|
592 | return (this.rc.clusters[this.activeCluster].host +
|
593 | '/relay/v1/' +
|
594 | projectId);
|
595 | };
|
596 | Environment.prototype.fileEndpoint = function (projectId) {
|
597 | if (this.isSharedCluster(this.activeCluster)) {
|
598 | return this.config.fileAPIEndpoint + projectId;
|
599 | }
|
600 | return (this.rc.clusters[this.activeCluster].host +
|
601 | '/file/v1/' +
|
602 | projectId);
|
603 | };
|
604 | Environment.prototype.subscriptionEndpoint = function (projectId) {
|
605 | if (this.isSharedCluster(this.activeCluster)) {
|
606 | var region = this.getRegionFromCluster(this.activeCluster);
|
607 | return this.subscriptionURL({ region: region, projectId: projectId });
|
608 | }
|
609 | var match = this.clusterEndpoint.match(/https?:\/\/(.*):(\d+)\/?.*/);
|
610 | var localAddr = match ? match[1] : 'localhost';
|
611 | var localPort = match ? match[2] : '60000';
|
612 | return this.subscriptionURL({ localAddr: localAddr, localPort: localPort, projectId: projectId });
|
613 | };
|
614 | Environment.prototype.setTestToken = function () {
|
615 | debug('taking graphcool test token');
|
616 | this.globalRC.platformToken = process.env.GRAPHCOOL_TEST_TOKEN;
|
617 | };
|
618 | return Environment;
|
619 | }());
|
620 | exports.Environment = Environment;
|
621 | var subscriptionEndpoints = {
|
622 | EU_WEST_1: 'wss://subscriptions.graph.cool',
|
623 | US_WEST_2: 'wss://subscriptions.us-west-2.graph.cool',
|
624 | AP_NORTHEAST_1: 'wss://subscriptions.ap-northeast-1.graph.cool',
|
625 | };
|
626 |
|
\ | No newline at end of file |