UNPKG

14.3 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14 return new (P || (P = Promise))(function (resolve, reject) {
15 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
18 step((generator = generator.apply(thisArg, _arguments || [])).next());
19 });
20};
21var __generator = (this && this.__generator) || function (thisArg, body) {
22 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24 function verb(n) { return function (v) { return step([n, v]); }; }
25 function step(op) {
26 if (f) throw new TypeError("Generator is already executing.");
27 while (_) try {
28 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;
29 if (y = 0, t) op = [op[0] & 2, t.value];
30 switch (op[0]) {
31 case 0: case 1: t = op; break;
32 case 4: _.label++; return { value: op[1], done: false };
33 case 5: _.label++; y = op[1]; op = [0]; continue;
34 case 7: op = _.ops.pop(); _.trys.pop(); continue;
35 default:
36 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40 if (t[2]) _.ops.pop();
41 _.trys.pop(); continue;
42 }
43 op = body.call(thisArg, _);
44 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46 }
47};
48Object.defineProperty(exports, "__esModule", { value: true });
49var graphql_config_1 = require("graphql-config");
50var prisma_yml_1 = require("prisma-yml");
51var lodash_1 = require("lodash");
52var os = require("os");
53var path = require("path");
54var fs = require("fs");
55function patchConfig(config, cwd, envVars) {
56 return __awaiter(this, void 0, void 0, function () {
57 return __generator(this, function (_a) {
58 switch (_a.label) {
59 case 0: return [4 /*yield*/, patchEndpointsToConfig(config, cwd, envVars)];
60 case 1:
61 config = _a.sent();
62 config = patchDirectivesToConfig(config, cwd, envVars);
63 return [2 /*return*/, config];
64 }
65 });
66 });
67}
68exports.patchConfig = patchConfig;
69function patchDirectivesToConfig(config, cwd, envVars) {
70 config.config = patchDirectivesToConfigData(config.config, cwd, envVars);
71 return config;
72}
73function patchDirectivesToConfigData(config, cwd, envVars) {
74 // return early if no prisma extension found
75 var allExtensions = [config.extensions].concat(lodash_1.values(config.projects).map(function (p) { return p.extensions; }));
76 if (!allExtensions.some(function (e) { return e && e.prisma; })) {
77 return config;
78 }
79 var newConfig = __assign({}, config);
80 if (newConfig.extensions) {
81 lodash_1.set(newConfig, ['extensions', 'customDirectives'], getCustomDirectives());
82 }
83 if (newConfig.projects) {
84 Object.keys(newConfig.projects).map(function (projectName) {
85 var project = newConfig.projects[projectName];
86 if (project.extensions) {
87 lodash_1.set(newConfig, ['projects', projectName, 'extensions', 'customDirectives'], getCustomDirectives());
88 }
89 });
90 }
91 return newConfig;
92}
93function getCustomDirectives(version) {
94 return [
95 'enum DeleteValues { CASCADE SET_NULL }',
96 'directive @default(value: String!) on FIELD_DEFINITION | SCALAR',
97 'directive @relation(name: String!, onDelete: DeleteValues) on FIELD_DEFINITION | SCALAR',
98 'directive @unique on FIELD_DEFINITION | SCALAR',
99 'directive @pgRelation(column: String!) on FIELD_DEFINITION | SCALAR',
100 'directive @pgRelationTable(table: String!, relationColumn: String!, targetColumn: String!) on FIELD_DEFINITION | SCALAR',
101 'directive @pgTable(name: String!) on FIELD_DEFINITION | SCALAR',
102 'directive @pgColumn(name: String!) on FIELD_DEFINITION | SCALAR',
103 'directive @pgDefault(value: String!) on FIELD_DEFINITION | SCALAR',
104 ];
105}
106exports.getCustomDirectives = getCustomDirectives;
107// TODO: Deprecate and remove this public API in favor
108// of patchConfig function in playground and other usages
109// of this project.
110function patchEndpointsToConfig(config, cwd, envVars, graceful) {
111 return __awaiter(this, void 0, void 0, function () {
112 var _a;
113 return __generator(this, function (_b) {
114 switch (_b.label) {
115 case 0:
116 _a = config;
117 return [4 /*yield*/, patchEndpointsToConfigData(config.config, cwd, envVars, graceful)];
118 case 1:
119 _a.config = _b.sent();
120 return [2 /*return*/, config];
121 }
122 });
123 });
124}
125exports.patchEndpointsToConfig = patchEndpointsToConfig;
126function patchEndpointsToConfigData(config, cwd, envVars, graceful) {
127 return __awaiter(this, void 0, void 0, function () {
128 var allExtensions, newConfig, home, env, _a, _b;
129 var _this = this;
130 return __generator(this, function (_c) {
131 switch (_c.label) {
132 case 0:
133 allExtensions = [config.extensions].concat(lodash_1.values(config.projects).map(function (p) { return p.extensions; }));
134 if (!allExtensions.some(function (e) { return e && e.prisma; })) {
135 return [2 /*return*/, config];
136 }
137 newConfig = __assign({}, config);
138 home = os.homedir();
139 env = new prisma_yml_1.Environment(home);
140 return [4 /*yield*/, env.load()];
141 case 1:
142 _c.sent();
143 if (!(newConfig.extensions && newConfig.extensions.prisma)) return [3 /*break*/, 3];
144 _a = lodash_1.set;
145 _b = [newConfig,
146 ['extensions', 'endpoints']];
147 return [4 /*yield*/, getEndpointsFromPath(env, newConfig.extensions.prisma, cwd, envVars, graceful)];
148 case 2:
149 _a.apply(void 0, _b.concat([_c.sent()]));
150 _c.label = 3;
151 case 3:
152 if (!newConfig.projects) return [3 /*break*/, 5];
153 return [4 /*yield*/, Promise.all(Object.keys(newConfig.projects).map(function (projectName) { return __awaiter(_this, void 0, void 0, function () {
154 var project, _a, _b;
155 return __generator(this, function (_c) {
156 switch (_c.label) {
157 case 0:
158 project = newConfig.projects[projectName];
159 if (!(project.extensions && project.extensions.prisma)) return [3 /*break*/, 2];
160 _a = lodash_1.set;
161 _b = [newConfig,
162 ['projects', projectName, 'extensions', 'endpoints']];
163 return [4 /*yield*/, getEndpointsFromPath(env, project.extensions.prisma, cwd, envVars, graceful)];
164 case 1:
165 _a.apply(void 0, _b.concat([_c.sent()]));
166 _c.label = 2;
167 case 2: return [2 /*return*/];
168 }
169 });
170 }); }))];
171 case 4:
172 _c.sent();
173 _c.label = 5;
174 case 5: return [2 /*return*/, newConfig];
175 }
176 });
177 });
178}
179exports.patchEndpointsToConfigData = patchEndpointsToConfigData;
180function makeConfigFromPath(cwd, envVars) {
181 if (cwd === void 0) { cwd = process.cwd(); }
182 return __awaiter(this, void 0, void 0, function () {
183 var _a, _b, ymlPath, home, env, definition, serviceName, stage, clusterName, cluster, url, token, headers, data;
184 return __generator(this, function (_c) {
185 switch (_c.label) {
186 case 0:
187 ymlPath = path.join(cwd, 'prisma.yml');
188 if (!fs.existsSync(ymlPath)) {
189 return [2 /*return*/, null];
190 }
191 home = os.homedir();
192 env = new prisma_yml_1.Environment(home);
193 return [4 /*yield*/, env.load()];
194 case 1:
195 _c.sent();
196 definition = new prisma_yml_1.PrismaDefinitionClass(env, ymlPath, envVars);
197 return [4 /*yield*/, definition.load({})];
198 case 2:
199 _c.sent();
200 serviceName = definition.service;
201 stage = definition.stage;
202 clusterName = definition.cluster;
203 if (!clusterName) {
204 throw new Error("No cluster set. Please set the \"cluster\" property in your prisma.yml");
205 }
206 return [4 /*yield*/, definition.getCluster()];
207 case 3:
208 cluster = _c.sent();
209 if (!cluster) {
210 throw new Error("Cluster " + clusterName + " provided in prisma.yml could not be found in global ~/.prisma/config.yml.\nPlease check in ~/.prisma/config.yml, if the cluster exists.\nYou can use `docker-compose up -d` to start a new cluster.");
211 }
212 url = cluster.getApiEndpoint(serviceName, stage, definition.getWorkspace() || undefined);
213 token = definition.getToken(serviceName, stage);
214 headers = token
215 ? {
216 Authorization: "Bearer " + token,
217 }
218 : undefined;
219 data = {
220 schemaPath: '',
221 projects: (_a = {},
222 _a[serviceName] = {
223 schemaPath: '',
224 extensions: {
225 endpoints: (_b = {},
226 _b[stage] = {
227 url: url,
228 headers: headers,
229 },
230 _b),
231 },
232 },
233 _a),
234 };
235 return [2 /*return*/, new graphql_config_1.GraphQLConfig(data, '')];
236 }
237 });
238 });
239}
240exports.makeConfigFromPath = makeConfigFromPath;
241function getEndpointsFromPath(env, ymlPath, cwd, envVars, graceful) {
242 return __awaiter(this, void 0, void 0, function () {
243 var _a, joinedYmlPath, definition, serviceName, stage, clusterName, cluster, url, token, headers;
244 return __generator(this, function (_b) {
245 switch (_b.label) {
246 case 0:
247 joinedYmlPath = cwd ? path.join(cwd, ymlPath) : ymlPath;
248 definition = new prisma_yml_1.PrismaDefinitionClass(env, joinedYmlPath, envVars);
249 return [4 /*yield*/, definition.load({}, undefined, graceful)];
250 case 1:
251 _b.sent();
252 serviceName = definition.service;
253 stage = definition.stage;
254 clusterName = definition.cluster;
255 if (!clusterName) {
256 throw new Error("No cluster set. Please set the \"cluster\" property in your prisma.yml");
257 }
258 return [4 /*yield*/, definition.getCluster()];
259 case 2:
260 cluster = _b.sent();
261 if (!cluster) {
262 throw new Error("Cluster " + clusterName + " provided in prisma.yml could not be found in global ~/.prisma/config.yml.\nPlease check in ~/.prisma/config.yml, if the cluster exists.\nYou can use `docker-compose up -d` to start a new cluster.");
263 }
264 url = cluster.getApiEndpoint(serviceName, stage, definition.getWorkspace() || undefined);
265 token = definition.getToken(serviceName, stage);
266 headers = token
267 ? {
268 Authorization: "Bearer " + token,
269 }
270 : undefined;
271 return [2 /*return*/, (_a = {},
272 _a[stage] = {
273 url: url,
274 headers: headers,
275 },
276 _a)];
277 }
278 });
279 });
280}
281//# sourceMappingURL=index.js.map
\No newline at end of file