UNPKG

9.84 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};
37Object.defineProperty(exports, "__esModule", { value: true });
38require("source-map-support/register");
39var Output_1 = require("./Output");
40var Config_1 = require("./Config");
41var Parser_1 = require("./Parser");
42var Help_1 = require("./Help");
43var Client_1 = require("./Client/Client");
44// import { Auth } from './Auth'
45var graphcool_yml_1 = require("graphcool-yml");
46var packagejson = require("../package.json");
47var mock = require("./mock");
48var fs = require("fs-extra");
49var path = require("path");
50var debug = require('debug')('command');
51var pjson = packagejson;
52var Command = (function () {
53 function Command(options) {
54 if (options === void 0) { options = { config: { mock: true } }; }
55 this.config =
56 (options.config && options.config.mockConfig) ||
57 options.config instanceof Config_1.Config
58 ? options.config
59 : new Config_1.Config(options.config);
60 this.out = new Output_1.Output(this.config);
61 this.config.setOutput(this.out);
62 this.argv = options.config && options.config.argv ? options.config.argv : [];
63 this.env = new graphcool_yml_1.Environment(this.config.home, this.out);
64 this.definition = new graphcool_yml_1.GraphcoolDefinitionClass(this.env, this.config.definitionPath, process.env, this.out);
65 this.client = new Client_1.Client(this.config, this.env, this.out);
66 // this.auth = new Auth(this.out, this.config, this.env, this.client)
67 // this.client.setAuth(this.auth)
68 }
69 Object.defineProperty(Command, "id", {
70 get: function () {
71 return this.command ? this.topic + ":" + this.command : this.topic;
72 },
73 enumerable: true,
74 configurable: true
75 });
76 Command.mock = function () {
77 var argv = [];
78 for (var _i = 0; _i < arguments.length; _i++) {
79 argv[_i] = arguments[_i];
80 }
81 return __awaiter(this, void 0, void 0, function () {
82 var customArgs, mockDefinition, mockRC, mockConfig;
83 return __generator(this, function (_a) {
84 customArgs = null;
85 if (typeof argv[0] === 'object') {
86 customArgs = argv.shift();
87 }
88 argv.unshift('argv0', 'cmd');
89 mockDefinition = customArgs && customArgs.mockDefinition
90 ? customArgs.mockDefinition
91 : mock.mockDefinition;
92 mockRC = customArgs && customArgs.mockRC ? customArgs.mockRC : null;
93 mockConfig = customArgs && customArgs.mockConfig ? customArgs.mockConfig : null;
94 debug("Using mockDefinition", mockDefinition);
95 debug("Using mockRC", mockRC);
96 return [2 /*return*/, this.run({ argv: argv, mock: true, mockDefinition: mockDefinition, mockRC: mockRC, mockConfig: mockConfig })];
97 });
98 });
99 };
100 Command.run = function (config) {
101 return __awaiter(this, void 0, void 0, function () {
102 var cmd, err_1, requests, requestsPath;
103 return __generator(this, function (_a) {
104 switch (_a.label) {
105 case 0:
106 if (process.env.NOCK_WRITE_RESPONSE_CMD === 'true') {
107 debug('RECORDING');
108 require('nock').recorder.rec({
109 dont_print: true,
110 });
111 }
112 cmd = new this({ config: config });
113 _a.label = 1;
114 case 1:
115 _a.trys.push([1, 5, , 6]);
116 return [4 /*yield*/, cmd.init(config)];
117 case 2:
118 _a.sent();
119 return [4 /*yield*/, cmd.run()];
120 case 3:
121 _a.sent();
122 return [4 /*yield*/, cmd.out.done()];
123 case 4:
124 _a.sent();
125 return [3 /*break*/, 6];
126 case 5:
127 err_1 = _a.sent();
128 cmd.out.error(err_1);
129 return [3 /*break*/, 6];
130 case 6:
131 if (process.env.NOCK_WRITE_RESPONSE_CMD === 'true') {
132 requests = require('nock').recorder.play();
133 requestsPath = path.join(process.cwd(), 'requests.js');
134 debug('WRITING', requestsPath);
135 fs.writeFileSync(requestsPath, requests.join('\n'));
136 }
137 return [2 /*return*/, cmd];
138 }
139 });
140 });
141 };
142 Command.buildHelp = function (config) {
143 var help = new Help_1.default(config);
144 return help.command(this);
145 };
146 Command.buildHelpLine = function (config) {
147 var help = new Help_1.default(config);
148 return help.commandLine(this);
149 };
150 Command.prototype.run = function () {
151 var rest = [];
152 for (var _i = 0; _i < arguments.length; _i++) {
153 rest[_i] = arguments[_i];
154 }
155 return __awaiter(this, void 0, void 0, function () {
156 return __generator(this, function (_a) {
157 return [2 /*return*/];
158 });
159 });
160 };
161 Command.prototype.init = function (options) {
162 return __awaiter(this, void 0, void 0, function () {
163 var mockDefinition, mockRC, parser, _a, argv, flags, args;
164 return __generator(this, function (_b) {
165 switch (_b.label) {
166 case 0:
167 mockDefinition = options && options.mockDefinition;
168 mockRC = options && options.mockRC;
169 parser = new Parser_1.Parser({
170 flags: this.constructor.flags || {},
171 args: this.constructor.args || [],
172 variableArgs: this.constructor.variableArgs,
173 cmd: this,
174 });
175 return [4 /*yield*/, parser.parse({
176 flags: this.flags,
177 argv: this.argv.slice(2),
178 })];
179 case 1:
180 _a = _b.sent(), argv = _a.argv, flags = _a.flags, args = _a.args;
181 this.flags = flags;
182 this.argv = argv;
183 this.args = args;
184 return [4 /*yield*/, this.env.load(flags)];
185 case 2:
186 _b.sent();
187 return [2 /*return*/];
188 }
189 });
190 });
191 };
192 Object.defineProperty(Command.prototype, "stdout", {
193 get: function () {
194 return this.out.stdout.output;
195 },
196 enumerable: true,
197 configurable: true
198 });
199 Object.defineProperty(Command.prototype, "stderr", {
200 get: function () {
201 return this.out.stderr.output;
202 },
203 enumerable: true,
204 configurable: true
205 });
206 Command.args = [];
207 Command.aliases = [];
208 Command.hidden = false;
209 Command.allowAnyFlags = false;
210 Command.version = pjson.version;
211 return Command;
212}());
213exports.Command = Command;
214//# sourceMappingURL=Command.js.map
\No newline at end of file