UNPKG

12.2 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10var __generator = (this && this.__generator) || function (thisArg, body) {
11 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13 function verb(n) { return function (v) { return step([n, v]); }; }
14 function step(op) {
15 if (f) throw new TypeError("Generator is already executing.");
16 while (_) try {
17 if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
18 if (y = 0, t) op = [0, t.value];
19 switch (op[0]) {
20 case 0: case 1: t = op; break;
21 case 4: _.label++; return { value: op[1], done: false };
22 case 5: _.label++; y = op[1]; op = [0]; continue;
23 case 7: op = _.ops.pop(); _.trys.pop(); continue;
24 default:
25 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29 if (t[2]) _.ops.pop();
30 _.trys.pop(); continue;
31 }
32 op = body.call(thisArg, _);
33 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35 }
36};
37var _this = this;
38Object.defineProperty(exports, "__esModule", { value: true });
39var path = require("path");
40var Config_1 = require("./Config");
41var Output_1 = require("./Output");
42var Lock_1 = require("./Plugin/Lock");
43var Dispatcher_1 = require("./Dispatcher/Dispatcher");
44var NotFound_1 = require("./NotFound");
45var fs_1 = require("./fs");
46var util_1 = require("./util");
47var StatusChecker_1 = require("./StatusChecker");
48var updateNotifier = require("update-notifier");
49var chalk_1 = require("chalk");
50var Raven = require("raven");
51Raven.config('https://93ed780dfd604e99974beeeff20559cf:bcbbced22be04f889c0f40e847cf544d@sentry.io/263225').install();
52var debug = require('debug')('cli');
53var handleEPIPE = function (err) {
54 Raven.captureException(err);
55 if (err.code !== 'EPIPE') {
56 throw err;
57 }
58};
59var out;
60if (!global.testing) {
61 process.once('SIGINT', function () {
62 if (out) {
63 if (out.action.task) {
64 out.action.stop(out.color.red('ctrl-c'));
65 }
66 out.exit(1);
67 }
68 else {
69 process.exit(1);
70 }
71 });
72 var handleErr = function (err) { return __awaiter(_this, void 0, void 0, function () {
73 return __generator(this, function (_a) {
74 if (!out) {
75 throw err;
76 }
77 out.error(err);
78 return [2 /*return*/];
79 });
80 }); };
81 process.once('uncaughtException', handleErr);
82 process.once('unhandledRejection', handleErr);
83 process.stdout.on('error', handleEPIPE);
84 process.stderr.on('error', handleEPIPE);
85}
86process.env.CLI_ENGINE_VERSION = require('../package.json').version;
87var CLI = (function () {
88 function CLI(_a) {
89 var config = (_a === void 0 ? {} : _a).config;
90 if (!config) {
91 config = {
92 mock: false,
93 };
94 }
95 var parentFilename = module.parent.parent
96 ? module.parent.parent.filename
97 : module.parent.filename;
98 if (!config.initPath) {
99 config.initPath = parentFilename;
100 }
101 if (!config.root) {
102 var findUp = require('find-up');
103 config.root = path.dirname(findUp.sync('package.json', {
104 cwd: parentFilename,
105 }));
106 }
107 this.config = new Config_1.Config(config);
108 this.notifier = updateNotifier({
109 pkg: this.config.pjson,
110 updateCheckInterval: 1,
111 });
112 }
113 CLI.prototype.run = function () {
114 return __awaiter(this, void 0, void 0, function () {
115 var _a, id, dispatcher, result, plugin, foundCommand, lock, _b, checker, requests, requestsPath, topic, timeout;
116 return __generator(this, function (_c) {
117 switch (_c.label) {
118 case 0:
119 out = new Output_1.Output(this.config);
120 this.config.setOutput(out);
121 if (!this.cmdAskingForHelp) return [3 /*break*/, 2];
122 debug('command asking for help');
123 _a = this;
124 return [4 /*yield*/, this.Help.run(this.config)];
125 case 1:
126 _a.cmd = _c.sent();
127 return [3 /*break*/, 12];
128 case 2:
129 id = util_1.getCommandId(this.config.argv.slice(1));
130 debug('command id', id);
131 // if there is a subcommand, cut the first away so the Parser still works correctly
132 if (this.config.argv[1] &&
133 this.config.argv[1].startsWith('-') &&
134 id !== 'help' &&
135 id !== 'init') {
136 this.config.argv = this.config.argv.slice(1);
137 }
138 dispatcher = new Dispatcher_1.Dispatcher(this.config);
139 return [4 /*yield*/, dispatcher.findCommand(id || this.config.defaultCommand || 'help')
140 // if nothing is found, try again with taking what is before :
141 ];
142 case 3:
143 result = _c.sent();
144 if (!(!result.Command && id && id.includes(':'))) return [3 /*break*/, 5];
145 return [4 /*yield*/, dispatcher.findCommand(id.split(':')[0])];
146 case 4:
147 result = _c.sent();
148 _c.label = 5;
149 case 5:
150 plugin = result.plugin;
151 foundCommand = result.Command;
152 if (!foundCommand) return [3 /*break*/, 8];
153 lock = new Lock_1.default(out);
154 return [4 /*yield*/, lock.unread()
155 // TODO remove this
156 ];
157 case 6:
158 _c.sent();
159 // TODO remove this
160 if (process.env.NOCK_WRITE_RESPONSE_CLI === 'true') {
161 debug('RECORDING');
162 require('nock').recorder.rec({
163 dont_print: true,
164 });
165 }
166 _b = this;
167 return [4 /*yield*/, foundCommand.run(this.config)];
168 case 7:
169 _b.cmd = _c.sent();
170 checker = new StatusChecker_1.StatusChecker(this.config, this.cmd.env);
171 checker.checkStatus(id, this.cmd.args, this.cmd.flags, this.cmd.argv);
172 if (process.env.NOCK_WRITE_RESPONSE_CLI === 'true') {
173 requests = require('nock').recorder.play();
174 requestsPath = path.join(process.cwd(), 'requests.js');
175 debug('WRITING', requestsPath);
176 fs_1.default.writeFileSync(requestsPath, requests.join('\n'));
177 }
178 return [3 /*break*/, 12];
179 case 8: return [4 /*yield*/, dispatcher.findTopic(id)];
180 case 9:
181 topic = _c.sent();
182 if (!topic) return [3 /*break*/, 11];
183 return [4 /*yield*/, this.Help.run(this.config)];
184 case 10:
185 _c.sent();
186 return [3 /*break*/, 12];
187 case 11: return [2 /*return*/, new NotFound_1.NotFound(out, this.config.argv).run()];
188 case 12:
189 if (this.notifier.update) {
190 this.notifier.notify({
191 message: 'Update available ' +
192 chalk_1.default.dim(this.notifier.update.current) +
193 chalk_1.default.reset(' → ') +
194 chalk_1.default.green(this.notifier.update.latest) +
195 ("\nRun " + chalk_1.default.bold.green('npm i -g graphcool') + " to update"),
196 boxenOpts: {
197 padding: 1,
198 margin: 1,
199 align: 'center',
200 borderColor: 'green',
201 borderStyle: 'round',
202 },
203 });
204 }
205 if (!!(this.config.argv.includes('logs') ||
206 this.config.argv.includes('logs:function') ||
207 (this.config.argv.includes('deploy') &&
208 (this.config.argv.includes('-w') ||
209 this.config.argv.includes('--watch'))))) return [3 /*break*/, 14];
210 timeout = require('./util').timeout;
211 return [4 /*yield*/, timeout(this.flush(), 1000)];
212 case 13:
213 _c.sent();
214 out.exit(0);
215 return [3 /*break*/, 15];
216 case 14:
217 debug('not flushing');
218 _c.label = 15;
219 case 15: return [2 /*return*/];
220 }
221 });
222 });
223 };
224 CLI.prototype.flush = function () {
225 if (global.testing) {
226 return Promise.resolve();
227 }
228 var p = new Promise(function (resolve) { return process.stdout.once('drain', resolve); });
229 process.stdout.write('');
230 return p;
231 };
232 Object.defineProperty(CLI.prototype, "cmdAskingForHelp", {
233 get: function () {
234 for (var _i = 0, _a = this.config.argv; _i < _a.length; _i++) {
235 var arg = _a[_i];
236 if (['--help', '-h'].includes(arg)) {
237 return true;
238 }
239 if (arg === '--') {
240 return false;
241 }
242 }
243 return false;
244 },
245 enumerable: true,
246 configurable: true
247 });
248 Object.defineProperty(CLI.prototype, "Help", {
249 get: function () {
250 var Help = require('./commands/help').default;
251 return Help;
252 },
253 enumerable: true,
254 configurable: true
255 });
256 return CLI;
257}());
258exports.CLI = CLI;
259function run(_a) {
260 var config = (_a === void 0 ? {} : _a).config;
261 if (!config) {
262 config = {
263 mock: false,
264 };
265 }
266 var cli = new CLI({ config: config });
267 return cli.run();
268}
269exports.run = run;
270//# sourceMappingURL=CLI.js.map
\No newline at end of file