1 | #!/usr/bin/env node
|
2 | "use strict";
|
3 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
4 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
5 | return new (P || (P = Promise))(function (resolve, reject) {
|
6 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
7 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
8 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
9 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
10 | });
|
11 | };
|
12 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
13 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
14 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
15 | function verb(n) { return function (v) { return step([n, v]); }; }
|
16 | function step(op) {
|
17 | if (f) throw new TypeError("Generator is already executing.");
|
18 | while (_) try {
|
19 | 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;
|
20 | if (y = 0, t) op = [op[0] & 2, t.value];
|
21 | switch (op[0]) {
|
22 | case 0: case 1: t = op; break;
|
23 | case 4: _.label++; return { value: op[1], done: false };
|
24 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
25 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
26 | default:
|
27 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
28 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
29 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
30 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
31 | if (t[2]) _.ops.pop();
|
32 | _.trys.pop(); continue;
|
33 | }
|
34 | op = body.call(thisArg, _);
|
35 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
36 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
37 | }
|
38 | };
|
39 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
40 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
41 | };
|
42 | Object.defineProperty(exports, "__esModule", { value: true });
|
43 | var commander_1 = __importDefault(require("commander"));
|
44 | var runtime_1 = require("./runtime");
|
45 | var chalk_1 = __importDefault(require("chalk"));
|
46 | var rc_1 = require("./rc");
|
47 | var runSgtsCLI = function () {
|
48 | commander_1.default
|
49 | .version(require('../package.json').version)
|
50 | .option('generate [generate]', "Generate using config file '.sgtsrc.js'")
|
51 | .option('init', 'Init your config file')
|
52 | .option('-e, --endpoint <endpoint>', 'GraphQl Api endpoint')
|
53 | .option('-j, --json <json>', 'Json file of your GraphQL Api schema')
|
54 | .option('-o, --output <output>', 'Output path of your generated file')
|
55 | .option('--codegen-methods', 'Generate all your graphQL methods fully typed (Inspired by Prisma)')
|
56 | .option('--codegen-react-hooks', 'Generate useMutation and useQuery React hooks typed')
|
57 | .option('--codegen-vue-hooks', 'Generate useMutation and useQuery React hooks typed')
|
58 | .option('--codegen-templates', 'Add gql query strings to the generated output')
|
59 | .option('--header <header>', 'Additional header option to fetch your schema from endpoint')
|
60 | .option('--customScalars <scalars>', 'Provide your custum scalars in format {"myScalar": "MyType"...}')
|
61 | .option('-p, --prefix <prefix>', 'Add prefix to all your types (ex: User becomes IUser with --suffix I)')
|
62 | .option('-s, --suffix <suffix>', 'Add suffix to all your types (ex: User becomes UserModel with --suffix Model)')
|
63 | .option('--compileToJs', 'Generate the methods in Js with declaration files instead of Ts')
|
64 | .option('-D, --download <download>', 'Specify the path to download the GraphQL introspection schema')
|
65 | .parse(process.argv);
|
66 | var generate = commander_1.default.generate, endpoint = commander_1.default.endpoint, json = commander_1.default.json, _a = commander_1.default.output, output = _a === void 0 ? './__generated.ts' : _a, customScalars = commander_1.default.customScalars, header = commander_1.default.header, prefix = commander_1.default.prefix, suffix = commander_1.default.suffix, codegenMethods = commander_1.default.codegenMethods, jsMode = commander_1.default.jsMode, codegenReactHooks = commander_1.default.codegenReactHooks, codegenVueHooks = commander_1.default.codegenVueHooks, codegenTemplates = commander_1.default.codegenTemplates, download = commander_1.default.download, init = commander_1.default.init;
|
67 | if (generate) {
|
68 | var env = typeof generate === 'boolean' ? 'development' : generate;
|
69 | var config = rc_1.getConfigParams(env);
|
70 | if (config)
|
71 | generateUsingConfig(config);
|
72 | else
|
73 | rc_1.createConfig();
|
74 | }
|
75 | else if (init) {
|
76 | rc_1.createConfig();
|
77 | }
|
78 | else {
|
79 | if (customScalars) {
|
80 | try {
|
81 | customScalars = JSON.parse(customScalars);
|
82 | }
|
83 | catch (e) {
|
84 | console.error(chalk_1.default.red('Invalid custom scalars format, expected {"myScalar": "MyType" ...}'));
|
85 | return;
|
86 | }
|
87 | }
|
88 | generateUsingConfig({
|
89 | endpoint: endpoint,
|
90 | json: json,
|
91 | output: output,
|
92 | customScalars: customScalars,
|
93 | header: header,
|
94 | prefix: prefix,
|
95 | suffix: suffix,
|
96 | codegenMethods: codegenMethods,
|
97 | jsMode: jsMode,
|
98 | codegenReactHooks: codegenReactHooks,
|
99 | codegenVueHooks: codegenVueHooks,
|
100 | codegenTemplates: codegenTemplates,
|
101 | download: download,
|
102 | });
|
103 | }
|
104 | };
|
105 | var generateUsingConfig = function (config) { return __awaiter(void 0, void 0, void 0, function () {
|
106 | var e_1;
|
107 | return __generator(this, function (_a) {
|
108 | switch (_a.label) {
|
109 | case 0:
|
110 | _a.trys.push([0, 2, , 3]);
|
111 | return [4, runtime_1.sgtsGenerate(config)];
|
112 | case 1:
|
113 | _a.sent();
|
114 | return [3, 3];
|
115 | case 2:
|
116 | e_1 = _a.sent();
|
117 | console.error(chalk_1.default.red('💔 Generation failed'));
|
118 | return [3, 3];
|
119 | case 3: return [2];
|
120 | }
|
121 | });
|
122 | }); };
|
123 | runSgtsCLI();
|