1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
12 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
13 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
14 | function verb(n) { return function (v) { return step([n, v]); }; }
|
15 | function step(op) {
|
16 | if (f) throw new TypeError("Generator is already executing.");
|
17 | while (_) try {
|
18 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
19 | if (y = 0, t) op = [op[0] & 2, t.value];
|
20 | switch (op[0]) {
|
21 | case 0: case 1: t = op; break;
|
22 | case 4: _.label++; return { value: op[1], done: false };
|
23 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
24 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
25 | default:
|
26 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
27 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
28 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
29 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
30 | if (t[2]) _.ops.pop();
|
31 | _.trys.pop(); continue;
|
32 | }
|
33 | op = body.call(thisArg, _);
|
34 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
35 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
36 | }
|
37 | };
|
38 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
39 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
40 | };
|
41 | Object.defineProperty(exports, "__esModule", { value: true });
|
42 | exports.createConfig = exports.getConfigParams = void 0;
|
43 | var path_1 = __importDefault(require("path"));
|
44 | var chalk_1 = __importDefault(require("chalk"));
|
45 | var fs_1 = __importDefault(require("fs"));
|
46 | var inquirer_1 = __importDefault(require("inquirer"));
|
47 | var figlet_1 = __importDefault(require("figlet"));
|
48 | var templates_1 = require("../templates");
|
49 | require('dotenv').config();
|
50 | function getConfigParams(env) {
|
51 | require('custom-env').env(env);
|
52 | var configPath = path_1.default.resolve(process.cwd(), '.sgtsrc.js');
|
53 | if (fs_1.default.existsSync(configPath))
|
54 | return require(path_1.default.resolve(process.cwd(), '.sgtsrc.js'));
|
55 | return null;
|
56 | }
|
57 | exports.getConfigParams = getConfigParams;
|
58 | exports.createConfig = function () { return __awaiter(void 0, void 0, void 0, function () {
|
59 | var sourceType, sourceValue, outputPath, codeGen, configPath, replace, codeGenDisplay, configValue, e_1;
|
60 | return __generator(this, function (_a) {
|
61 | switch (_a.label) {
|
62 | case 0:
|
63 | _a.trys.push([0, 8, , 9]);
|
64 | console.log(chalk_1.default.magenta(figlet_1.default.textSync('SGTS', {
|
65 | horizontalLayout: 'full',
|
66 | })));
|
67 | return [4, inquirer_1.default.prompt([
|
68 | {
|
69 | type: 'list',
|
70 | name: 'type',
|
71 | message: 'Where does your GraphQL schema comes from?',
|
72 | choices: [
|
73 | {
|
74 | name: 'From an http endpoint',
|
75 | value: 'endpoint',
|
76 | },
|
77 | {
|
78 | name: 'From a JSON file',
|
79 | value: 'json',
|
80 | },
|
81 | ],
|
82 | },
|
83 | ])];
|
84 | case 1:
|
85 | sourceType = _a.sent();
|
86 | return [4, inquirer_1.default.prompt([
|
87 | {
|
88 | type: 'input',
|
89 | name: 'source',
|
90 | message: sourceType.type === 'endpoint'
|
91 | ? "What's the url of your GraphQL api endpoint?"
|
92 | : "What's the absolute path to your GraphQL JSON schema?",
|
93 | },
|
94 | ])];
|
95 | case 2:
|
96 | sourceValue = _a.sent();
|
97 | return [4, inquirer_1.default.prompt([
|
98 | {
|
99 | type: 'input',
|
100 | name: 'ouput',
|
101 | message: 'Where do you want to save your generated file?',
|
102 | default: './__generated.ts',
|
103 | },
|
104 | ])];
|
105 | case 3:
|
106 | outputPath = _a.sent();
|
107 | return [4, inquirer_1.default.prompt([
|
108 | {
|
109 | type: 'checkbox',
|
110 | name: 'codeGens',
|
111 | message: 'What do you want to generate?',
|
112 | choices: [
|
113 | new inquirer_1.default.Separator('------'),
|
114 | {
|
115 | name: 'Typescript definitions',
|
116 | checked: true,
|
117 | disabled: 'Sgts will always generate definitions by default',
|
118 | },
|
119 | new inquirer_1.default.Separator('------'),
|
120 | {
|
121 | name: 'Vanilla es6 helpers functions',
|
122 | value: 'codegenMethods',
|
123 | },
|
124 | {
|
125 | name: 'Apollo React Hooks',
|
126 | value: 'codegenReactHooks',
|
127 | },
|
128 | {
|
129 | name: 'Vue Apollo Hooks',
|
130 | value: 'codegenVueHooks',
|
131 | },
|
132 | {
|
133 | name: 'Only GraphQL string templates',
|
134 | value: 'codegenTemplates',
|
135 | },
|
136 | ],
|
137 | },
|
138 | ])];
|
139 | case 4:
|
140 | codeGen = _a.sent();
|
141 | configPath = path_1.default.resolve(process.cwd(), '.sgtsrc.js');
|
142 | if (!fs_1.default.existsSync(configPath)) return [3, 6];
|
143 | return [4, inquirer_1.default.prompt([
|
144 | {
|
145 | type: 'confirm',
|
146 | name: 'delete',
|
147 | message: 'A config file already exists in this folder, do you want to replace it?',
|
148 | },
|
149 | ])];
|
150 | case 5:
|
151 | replace = _a.sent();
|
152 | if (!replace.delete) {
|
153 | console.log('Ok bye!');
|
154 | return [2];
|
155 | }
|
156 | _a.label = 6;
|
157 | case 6:
|
158 | codeGenDisplay = codeGen.codeGens.map(function (m) { return m + ": true,"; });
|
159 | configValue = "module.exports = {\n " + sourceType.type + ": \"" + sourceValue.source + "\",\n output: \"" + outputPath.ouput + "\",\n " + codeGenDisplay.join('\n ') + "\n}\n ";
|
160 | return [4, fs_1.default.writeFileSync(configPath, templates_1.configTemplate(configValue))];
|
161 | case 7:
|
162 | _a.sent();
|
163 | console.log(chalk_1.default.green("Configuration file created at " + chalk_1.default.bold(configPath)));
|
164 | return [3, 9];
|
165 | case 8:
|
166 | e_1 = _a.sent();
|
167 | return [2, Promise.reject(e_1)];
|
168 | case 9: return [2];
|
169 | }
|
170 | });
|
171 | }); };
|