@dainprotocol/cli 1.2.25 → 1.2.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __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
+ };
2
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -46,13 +57,37 @@ var fs_extra_1 = __importDefault(require("fs-extra"));
46
57
  var path_1 = __importDefault(require("path"));
47
58
  var archiver_1 = __importDefault(require("archiver"));
48
59
  var build_1 = __importDefault(require("./build"));
49
- var status_1 = __importDefault(require("./status"));
50
60
  var START_DEPLOY_URI = "/api/app/data/deployments/start-deploy";
61
+ var FETCH_TIMEOUT_MS = 120000; // 2 minutes for deployment
62
+ function fetchWithTimeout(url_1, options_1) {
63
+ return __awaiter(this, arguments, void 0, function (url, options, timeoutMs) {
64
+ var controller, timeoutId, response;
65
+ if (timeoutMs === void 0) { timeoutMs = FETCH_TIMEOUT_MS; }
66
+ return __generator(this, function (_a) {
67
+ switch (_a.label) {
68
+ case 0:
69
+ controller = new AbortController();
70
+ timeoutId = setTimeout(function () { return controller.abort(); }, timeoutMs);
71
+ _a.label = 1;
72
+ case 1:
73
+ _a.trys.push([1, , 3, 4]);
74
+ return [4 /*yield*/, fetch(url, __assign(__assign({}, options), { signal: controller.signal }))];
75
+ case 2:
76
+ response = _a.sent();
77
+ return [2 /*return*/, response];
78
+ case 3:
79
+ clearTimeout(timeoutId);
80
+ return [7 /*endfinally*/];
81
+ case 4: return [2 /*return*/];
82
+ }
83
+ });
84
+ });
85
+ }
51
86
  function deploy(options) {
52
87
  return __awaiter(this, void 0, void 0, function () {
53
- var config, spinner, basePath, deployPath, isProduction, environmentName, _a, repoName, branchName, projectId, _b, result, response, envArray, error_1;
54
- return __generator(this, function (_c) {
55
- switch (_c.label) {
88
+ var config, spinner, basePath, deployPath, isProduction, environmentName, _a, repoName, branchName, projectId, _b, result, response, envArray, _c, error_1;
89
+ return __generator(this, function (_d) {
90
+ switch (_d.label) {
56
91
  case 0:
57
92
  config = (0, utils_1.getDainConfig)(options.config);
58
93
  spinner = (0, ora_1.default)("Deploying project...").start();
@@ -66,23 +101,24 @@ function deploy(options) {
66
101
  if (!(repoName && branchName)) return [3 /*break*/, 2];
67
102
  return [4 /*yield*/, getProjectId({ basePath: basePath, repoName: repoName })];
68
103
  case 1:
69
- _b = _c.sent();
104
+ _b = _d.sent();
70
105
  return [3 /*break*/, 3];
71
106
  case 2:
72
107
  _b = null;
73
- _c.label = 3;
108
+ _d.label = 3;
74
109
  case 3:
75
110
  projectId = _b;
76
111
  if (!projectId && repoName && branchName)
77
112
  spinner.fail("No project found on platform linked to this repository.");
78
- _c.label = 4;
113
+ _d.label = 4;
79
114
  case 4:
80
- _c.trys.push([4, 16, , 17]);
115
+ _d.trys.push([4, 18, , 19]);
81
116
  if (!(projectId && repoName && branchName)) return [3 /*break*/, 7];
82
117
  // Deploy project using github repository
83
118
  spinner.info("Deploying project using github repository...").start();
84
- return [4 /*yield*/, fetch("".concat(basePath, "/api/app/codegen/data/projects/").concat(projectId, "/start-deploy"), {
119
+ return [4 /*yield*/, fetchWithTimeout("".concat(basePath, "/api/app/codegen/data/projects/").concat(projectId, "/start-deploy"), {
85
120
  method: "POST",
121
+ headers: { "Content-Type": "application/json" },
86
122
  body: JSON.stringify({
87
123
  environment: environmentName,
88
124
  isProduction: isProduction,
@@ -90,49 +126,56 @@ function deploy(options) {
90
126
  }),
91
127
  })];
92
128
  case 5:
93
- response = _c.sent();
129
+ response = _d.sent();
130
+ if (!response.ok) {
131
+ throw new Error("Deploy failed: ".concat(response.status, " ").concat(response.statusText));
132
+ }
94
133
  return [4 /*yield*/, response.json()];
95
134
  case 6:
96
- result = _c.sent();
97
- return [3 /*break*/, 12];
135
+ result = _d.sent();
136
+ return [3 /*break*/, 14];
98
137
  case 7:
99
138
  // Deploy project using local files
100
139
  spinner.info("Deploying project using local files...").start();
101
140
  return [4 /*yield*/, loadAndValidateEnvVariables()];
102
141
  case 8:
103
- envArray = _c.sent();
142
+ envArray = _d.sent();
104
143
  return [4 /*yield*/, (0, build_1.default)({ config: options.config, deploy: true })];
105
144
  case 9:
106
- _c.sent();
145
+ _d.sent();
107
146
  return [4 /*yield*/, deployAndPushFiles(deployPath, envArray, isProduction, environmentName)];
108
147
  case 10:
109
- result = _c.sent();
110
- return [4 /*yield*/, fetch(result.service.url, {
111
- method: "GET",
112
- })];
148
+ result = _d.sent();
149
+ _d.label = 11;
113
150
  case 11:
114
- _c.sent();
115
- _c.label = 12;
151
+ _d.trys.push([11, 13, , 14]);
152
+ return [4 /*yield*/, fetchWithTimeout(result.service.url, { method: "GET" }, 10000)];
116
153
  case 12:
117
- if (!result) return [3 /*break*/, 14];
118
- return [4 /*yield*/, updateDainJson(result)];
154
+ _d.sent();
155
+ return [3 /*break*/, 14];
119
156
  case 13:
120
- _c.sent();
157
+ _c = _d.sent();
158
+ return [3 /*break*/, 14];
159
+ case 14:
160
+ if (!result) return [3 /*break*/, 16];
161
+ return [4 /*yield*/, updateDainJson(result)];
162
+ case 15:
163
+ _d.sent();
121
164
  printDeploymentResult(result, spinner);
122
165
  process.exit(0);
123
- return [3 /*break*/, 15];
124
- case 14:
166
+ return [3 /*break*/, 17];
167
+ case 16:
125
168
  spinner.fail("Deployment failed.");
126
169
  process.exit(1);
127
- _c.label = 15;
128
- case 15: return [3 /*break*/, 17];
129
- case 16:
130
- error_1 = _c.sent();
170
+ _d.label = 17;
171
+ case 17: return [3 /*break*/, 19];
172
+ case 18:
173
+ error_1 = _d.sent();
131
174
  spinner.fail("Deployment failed.");
132
175
  (0, utils_1.logError)("Error during deployment: ", error_1);
133
176
  process.exit(1);
134
- return [3 /*break*/, 17];
135
- case 17: return [2 /*return*/];
177
+ return [3 /*break*/, 19];
178
+ case 19: return [2 /*return*/];
136
179
  }
137
180
  });
138
181
  });
@@ -162,7 +205,7 @@ function loadAndValidateEnvVariables() {
162
205
  // Deploys and pushes files to the platform
163
206
  function deployAndPushFiles(startDeployUrl, envArray, isProduction, environmentName) {
164
207
  return __awaiter(this, void 0, void 0, function () {
165
- var projectZip, projectZipBuffer, formData, response, errorText, result, error_2;
208
+ var projectZip, projectZipBuffer, formData, response, errorText;
166
209
  return __generator(this, function (_a) {
167
210
  switch (_a.label) {
168
211
  case 0: return [4 /*yield*/, zipDirectory("./", "project.zip")];
@@ -173,34 +216,21 @@ function deployAndPushFiles(startDeployUrl, envArray, isProduction, environmentN
173
216
  projectZipBuffer = _a.sent();
174
217
  formData = new FormData();
175
218
  formData.append("array", JSON.stringify(envArray));
176
- formData.append("file", new Blob([projectZipBuffer]), "project.zip");
219
+ formData.append("file", new Blob([new Uint8Array(projectZipBuffer)]), "project.zip");
177
220
  formData.append("isProduction", isProduction.toString());
178
221
  formData.append("environment", environmentName);
179
- _a.label = 3;
180
- case 3:
181
- _a.trys.push([3, 8, , 9]);
182
- return [4 /*yield*/, fetch(startDeployUrl, {
222
+ return [4 /*yield*/, fetchWithTimeout(startDeployUrl, {
183
223
  method: "POST",
184
224
  body: formData,
185
225
  })];
186
- case 4:
226
+ case 3:
187
227
  response = _a.sent();
188
- if (!!response.ok) return [3 /*break*/, 6];
228
+ if (!!response.ok) return [3 /*break*/, 5];
189
229
  return [4 /*yield*/, response.text()];
190
- case 5:
230
+ case 4:
191
231
  errorText = _a.sent();
192
- throw new Error("Deployment failed: ".concat(response.statusText, ". Response: ").concat(errorText));
193
- case 6: return [4 /*yield*/, response.json()];
194
- case 7:
195
- result = _a.sent();
196
- return [2 /*return*/, result];
197
- case 8:
198
- error_2 = _a.sent();
199
- (0, utils_1.logError)("Error during deployment and push files: ", error_2.stack);
200
- console.error("Detailed error:", error_2);
201
- process.exit(1);
202
- return [3 /*break*/, 9];
203
- case 9: return [2 /*return*/];
232
+ throw new Error("Deployment failed: ".concat(response.status, " ").concat(response.statusText, ". Response: ").concat(errorText));
233
+ case 5: return [2 /*return*/, response.json()];
204
234
  }
205
235
  });
206
236
  });
@@ -224,11 +254,20 @@ function loadEnvVariables() {
224
254
  envContent = _a.sent();
225
255
  return [2 /*return*/, envContent
226
256
  .split("\n")
257
+ .filter(function (line) { return line.trim() && !line.trim().startsWith("#"); })
227
258
  .map(function (line) {
228
- var _a = line.split("="), name = _a[0], value = _a[1];
229
- return { name: name, value: value };
259
+ var equalsIndex = line.indexOf("=");
260
+ if (equalsIndex === -1)
261
+ return null;
262
+ var name = line.substring(0, equalsIndex).trim();
263
+ var value = line.substring(equalsIndex + 1).trim();
264
+ // Remove surrounding quotes if present
265
+ var unquotedValue = value.replace(/^["']|["']$/g, "");
266
+ return { name: name, value: unquotedValue };
230
267
  })
231
- .filter(function (env) { return env.name && env.value; })];
268
+ .filter(function (env) {
269
+ return env !== null && env.name !== "" && env.value !== "";
270
+ })];
232
271
  }
233
272
  });
234
273
  });
@@ -252,26 +291,6 @@ function zipDirectory(sourceDir, outputZip) {
252
291
  });
253
292
  });
254
293
  }
255
- var recursiveStatusUntilRunning = function (config, deploymentId, environmentName, serviceId) { return __awaiter(void 0, void 0, void 0, function () {
256
- var statusResult;
257
- return __generator(this, function (_a) {
258
- switch (_a.label) {
259
- case 0: return [4 /*yield*/, (0, status_1.default)({}, deploymentId, environmentName, serviceId)];
260
- case 1:
261
- statusResult = _a.sent();
262
- if (statusResult.status === "RUNNING") {
263
- return [2 /*return*/];
264
- }
265
- return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 15000); })];
266
- case 2:
267
- _a.sent();
268
- return [4 /*yield*/, recursiveStatusUntilRunning(config, deploymentId, environmentName, serviceId)];
269
- case 3:
270
- _a.sent();
271
- return [2 /*return*/];
272
- }
273
- });
274
- }); };
275
294
  var checkGitConfig = function () {
276
295
  try {
277
296
  // Read git config and get repo name
@@ -289,43 +308,50 @@ var checkGitConfig = function () {
289
308
  }
290
309
  };
291
310
  var getProjectId = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
292
- var project, projectId, error_3;
311
+ var response, _c;
293
312
  var basePath = _b.basePath, repoName = _b.repoName;
294
- return __generator(this, function (_c) {
295
- switch (_c.label) {
313
+ return __generator(this, function (_d) {
314
+ switch (_d.label) {
296
315
  case 0:
297
- _c.trys.push([0, 3, , 4]);
298
- return [4 /*yield*/, fetch("".concat(basePath, "/api/app/codegen/data/projects/get-by-repo?repo=").concat(repoName))];
316
+ _d.trys.push([0, 2, , 3]);
317
+ return [4 /*yield*/, fetchWithTimeout("".concat(basePath, "/api/app/codegen/data/projects/get-by-repo?repo=").concat(encodeURIComponent(repoName)), { method: "GET" }, 30000)];
299
318
  case 1:
300
- project = _c.sent();
301
- return [4 /*yield*/, project.json()];
319
+ response = _d.sent();
320
+ if (!response.ok)
321
+ return [2 /*return*/, null];
322
+ return [2 /*return*/, response.json()];
302
323
  case 2:
303
- projectId = _c.sent();
304
- return [2 /*return*/, projectId];
305
- case 3:
306
- error_3 = _c.sent();
324
+ _c = _d.sent();
307
325
  return [2 /*return*/, null];
308
- case 4: return [2 /*return*/];
326
+ case 3: return [2 /*return*/];
309
327
  }
310
328
  });
311
329
  }); };
312
330
  var updateDainJson = function (result) { return __awaiter(void 0, void 0, void 0, function () {
313
- var deploymentId, serviceId, dainJson, dainJsonObject;
331
+ var deploymentId, serviceId, dainJson, dainJsonObject, error_2;
314
332
  return __generator(this, function (_a) {
315
333
  switch (_a.label) {
316
334
  case 0:
317
335
  deploymentId = result.deploymentId;
318
336
  serviceId = result.serviceId;
319
- return [4 /*yield*/, fs_extra_1.default.readFile("./dain.json", "utf-8")];
337
+ _a.label = 1;
320
338
  case 1:
339
+ _a.trys.push([1, 4, , 5]);
340
+ return [4 /*yield*/, fs_extra_1.default.readFile("./dain.json", "utf-8")];
341
+ case 2:
321
342
  dainJson = _a.sent();
322
343
  dainJsonObject = JSON.parse(dainJson);
323
344
  dainJsonObject["deployment-id"] = deploymentId;
324
345
  dainJsonObject["service-id"] = serviceId;
325
346
  return [4 /*yield*/, fs_extra_1.default.writeFile("./dain.json", JSON.stringify(dainJsonObject, null, 2))];
326
- case 2:
347
+ case 3:
327
348
  _a.sent();
328
- return [2 /*return*/];
349
+ return [3 /*break*/, 5];
350
+ case 4:
351
+ error_2 = _a.sent();
352
+ (0, utils_1.logError)("Failed to update dain.json", error_2);
353
+ return [3 /*break*/, 5];
354
+ case 5: return [2 /*return*/];
329
355
  }
330
356
  });
331
357
  }); };