@dainprotocol/cli 1.1.31 → 1.1.32

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.
@@ -50,9 +50,9 @@ var status_1 = __importDefault(require("./status"));
50
50
  var START_DEPLOY_URI = "/api/app/data/deployments/start-deploy";
51
51
  function deploy(options) {
52
52
  return __awaiter(this, void 0, void 0, function () {
53
- var config, spinner, basePath, deployPath, isProduction, environmentName, envArray, result, deploymentId, serviceId, dainJson, dainJsonObject, error_1;
54
- return __generator(this, function (_a) {
55
- switch (_a.label) {
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) {
56
56
  case 0:
57
57
  config = (0, utils_1.getDainConfig)(options.config);
58
58
  spinner = (0, ora_1.default)("Deploying project...").start();
@@ -60,50 +60,79 @@ function deploy(options) {
60
60
  deployPath = "".concat(basePath).concat(START_DEPLOY_URI);
61
61
  isProduction = config["environment"] === "production";
62
62
  environmentName = config["environment"] || "production";
63
- _a.label = 1;
63
+ _a = checkGitConfig(), repoName = _a.repoName, branchName = _a.branchName;
64
+ if (repoName && branchName)
65
+ spinner.info("Found git config, checking if project exists...").start();
66
+ if (!(repoName && branchName)) return [3 /*break*/, 2];
67
+ return [4 /*yield*/, getProjectId({ basePath: basePath, repoName: repoName })];
64
68
  case 1:
65
- _a.trys.push([1, 8, , 9]);
66
- return [4 /*yield*/, loadAndValidateEnvVariables()];
69
+ _b = _c.sent();
70
+ return [3 /*break*/, 3];
67
71
  case 2:
68
- envArray = _a.sent();
69
- return [4 /*yield*/, (0, build_1.default)({ config: options.config, deploy: true })];
72
+ _b = null;
73
+ _c.label = 3;
70
74
  case 3:
71
- _a.sent();
72
- return [4 /*yield*/, deployAndPushFiles(deployPath, envArray, isProduction, environmentName)];
75
+ projectId = _b;
76
+ if (!projectId && repoName && branchName)
77
+ spinner.fail("No project found on platform linked to this repository.");
78
+ _c.label = 4;
73
79
  case 4:
74
- result = _a.sent();
75
- deploymentId = result.deploymentId;
76
- serviceId = result.serviceId;
77
- return [4 /*yield*/, fs_extra_1.default.readFile("./dain.json", "utf-8")];
80
+ _c.trys.push([4, 16, , 17]);
81
+ if (!(projectId && repoName && branchName)) return [3 /*break*/, 7];
82
+ // Deploy project using github repository
83
+ spinner.info("Deploying project using github repository...").start();
84
+ return [4 /*yield*/, fetch("".concat(basePath, "/api/app/codegen/data/projects/").concat(projectId, "/start-deploy"), {
85
+ method: "POST",
86
+ body: JSON.stringify({
87
+ environment: environmentName,
88
+ isProduction: isProduction,
89
+ branch: branchName,
90
+ }),
91
+ })];
78
92
  case 5:
79
- dainJson = _a.sent();
80
- dainJsonObject = JSON.parse(dainJson);
81
- dainJsonObject["deployment-id"] = deploymentId;
82
- dainJsonObject["service-id"] = serviceId;
83
- return [4 /*yield*/, fs_extra_1.default.writeFile("./dain.json", JSON.stringify(dainJsonObject, null, 2))];
93
+ response = _c.sent();
94
+ return [4 /*yield*/, response.json()];
84
95
  case 6:
85
- _a.sent();
86
- console.log("\n-----------------------");
87
- spinner.succeed("Deployment URL: ".concat(result.service.url));
88
- spinner.succeed("Deployment ID: ".concat(result.deploymentId));
89
- spinner.succeed("Deployment completed at: " + new Date().toISOString());
90
- spinner.succeed("Deployment completed successfully.");
91
- spinner.info("You can access logs using `dain logs -w` command.");
92
- console.log("-----------------------");
96
+ result = _c.sent();
97
+ return [3 /*break*/, 12];
98
+ case 7:
99
+ // Deploy project using local files
100
+ spinner.info("Deploying project using local files...").start();
101
+ return [4 /*yield*/, loadAndValidateEnvVariables()];
102
+ case 8:
103
+ envArray = _c.sent();
104
+ return [4 /*yield*/, (0, build_1.default)({ config: options.config, deploy: true })];
105
+ case 9:
106
+ _c.sent();
107
+ return [4 /*yield*/, deployAndPushFiles(deployPath, envArray, isProduction, environmentName)];
108
+ case 10:
109
+ result = _c.sent();
93
110
  return [4 /*yield*/, fetch(result.service.url, {
94
111
  method: "GET",
95
112
  })];
96
- case 7:
97
- _a.sent();
113
+ case 11:
114
+ _c.sent();
115
+ _c.label = 12;
116
+ case 12:
117
+ if (!result) return [3 /*break*/, 14];
118
+ return [4 /*yield*/, updateDainJson(result)];
119
+ case 13:
120
+ _c.sent();
121
+ printDeploymentResult(result, spinner);
98
122
  process.exit(0);
99
- return [3 /*break*/, 9];
100
- case 8:
101
- error_1 = _a.sent();
123
+ return [3 /*break*/, 15];
124
+ case 14:
125
+ spinner.fail("Deployment failed.");
126
+ process.exit(1);
127
+ _c.label = 15;
128
+ case 15: return [3 /*break*/, 17];
129
+ case 16:
130
+ error_1 = _c.sent();
102
131
  spinner.fail("Deployment failed.");
103
132
  (0, utils_1.logError)("Error during deployment: ", error_1);
104
133
  process.exit(1);
105
- return [3 /*break*/, 9];
106
- case 9: return [2 /*return*/];
134
+ return [3 /*break*/, 17];
135
+ case 17: return [2 /*return*/];
107
136
  }
108
137
  });
109
138
  });
@@ -243,3 +272,69 @@ var recursiveStatusUntilRunning = function (config, deploymentId, environmentNam
243
272
  }
244
273
  });
245
274
  }); };
275
+ var checkGitConfig = function () {
276
+ try {
277
+ // Read git config and get repo name
278
+ var gitConfig = fs_extra_1.default.readFileSync(".git/config", "utf-8");
279
+ var remote = gitConfig.match(/\[remote "origin"\]\n\s*url = git@github.com:(.*).git/);
280
+ var repoName = remote ? remote[1] : null;
281
+ // Read git head and get branch name
282
+ var gitHead = fs_extra_1.default.readFileSync(".git/HEAD", "utf-8");
283
+ var branch = gitHead.match(/ref: refs\/heads\/(.*)/);
284
+ var branchName = branch ? branch[1] : null;
285
+ return { repoName: repoName, branchName: branchName };
286
+ }
287
+ catch (error) {
288
+ return { repoName: null, branchName: null };
289
+ }
290
+ };
291
+ var getProjectId = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
292
+ var project, projectId, error_3;
293
+ var basePath = _b.basePath, repoName = _b.repoName;
294
+ return __generator(this, function (_c) {
295
+ switch (_c.label) {
296
+ 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))];
299
+ case 1:
300
+ project = _c.sent();
301
+ return [4 /*yield*/, project.json()];
302
+ case 2:
303
+ projectId = _c.sent();
304
+ return [2 /*return*/, projectId];
305
+ case 3:
306
+ error_3 = _c.sent();
307
+ return [2 /*return*/, null];
308
+ case 4: return [2 /*return*/];
309
+ }
310
+ });
311
+ }); };
312
+ var updateDainJson = function (result) { return __awaiter(void 0, void 0, void 0, function () {
313
+ var deploymentId, serviceId, dainJson, dainJsonObject;
314
+ return __generator(this, function (_a) {
315
+ switch (_a.label) {
316
+ case 0:
317
+ deploymentId = result.deploymentId;
318
+ serviceId = result.serviceId;
319
+ return [4 /*yield*/, fs_extra_1.default.readFile("./dain.json", "utf-8")];
320
+ case 1:
321
+ dainJson = _a.sent();
322
+ dainJsonObject = JSON.parse(dainJson);
323
+ dainJsonObject["deployment-id"] = deploymentId;
324
+ dainJsonObject["service-id"] = serviceId;
325
+ return [4 /*yield*/, fs_extra_1.default.writeFile("./dain.json", JSON.stringify(dainJsonObject, null, 2))];
326
+ case 2:
327
+ _a.sent();
328
+ return [2 /*return*/];
329
+ }
330
+ });
331
+ }); };
332
+ var printDeploymentResult = function (result, spinner) {
333
+ console.log("\n-----------------------");
334
+ spinner.succeed("Deployment URL: ".concat(result.service.url));
335
+ spinner.succeed("Deployment ID: ".concat(result.deploymentId));
336
+ spinner.succeed("Deployment completed at: " + new Date().toISOString());
337
+ spinner.succeed("Deployment completed successfully.");
338
+ spinner.info("You can access logs using `dain logs -w` command.");
339
+ console.log("-----------------------");
340
+ };
@@ -64,6 +64,7 @@ var childProcess = null;
64
64
  var watcher = null;
65
65
  var mf = null;
66
66
  var tunnelUrl = null;
67
+ var isFirstStart = true;
67
68
  function isPortAvailable(port) {
68
69
  return new Promise(function (resolve) {
69
70
  var server = (0, net_1.createServer)()
@@ -76,26 +77,34 @@ function isPortAvailable(port) {
76
77
  });
77
78
  });
78
79
  }
79
- function startProcess(command, options) {
80
+ function startProcess(command, options, isRestart) {
80
81
  var _a, _b;
82
+ if (isRestart === void 0) { isRestart = false; }
81
83
  if (childProcess) {
82
84
  childProcess.kill();
83
85
  }
84
- var spinner = (0, ora_1.default)('Starting development server...').start();
86
+ var spinner = (0, ora_1.default)(isRestart ? 'Restarting development server...' : 'Starting development server...').start();
85
87
  childProcess = (0, child_process_1.exec)(command, options);
88
+ var serverStarted = false;
86
89
  (_a = childProcess.stdout) === null || _a === void 0 ? void 0 : _a.on('data', function (data) {
87
- spinner.succeed('Development server started.');
88
- if (tunnelUrl) {
89
- (0, utils_1.displayTunnelUrl)(tunnelUrl);
90
+ if (!serverStarted) {
91
+ spinner.succeed(isRestart ? 'Development server restarted.' : 'Development server started.');
92
+ if (tunnelUrl && isFirstStart) {
93
+ (0, utils_1.displayTunnelUrl)(tunnelUrl);
94
+ isFirstStart = false;
95
+ }
96
+ serverStarted = true;
90
97
  }
91
98
  console.log(data);
92
99
  });
93
100
  (_b = childProcess.stderr) === null || _b === void 0 ? void 0 : _b.on('data', function (data) {
94
- spinner.fail('Development server error.');
101
+ if (!serverStarted) {
102
+ spinner.fail('Development server error.');
103
+ }
95
104
  (0, utils_1.logError)(data);
96
105
  });
97
106
  childProcess.on('close', function (code) {
98
- if (code !== 0) {
107
+ if (code !== 0 && !serverStarted) {
99
108
  spinner.fail("Development server exited with code ".concat(code));
100
109
  }
101
110
  });
@@ -195,7 +204,7 @@ function dev(options) {
195
204
  });
196
205
  watcher.on('change', function (path) {
197
206
  (0, utils_1.logInfo)("File ".concat(path, " has been changed. Restarting..."));
198
- startProcess(command, { env: __assign(__assign({}, process.env), envVars) });
207
+ startProcess(command, { env: __assign(__assign({}, process.env), envVars) }, true);
199
208
  });
200
209
  (0, utils_1.logInfo)('Watching for file changes...');
201
210
  // Add signal handlers for graceful shutdown
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dainprotocol/cli",
3
- "version": "1.1.31",
3
+ "version": "1.1.32",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -21,7 +21,7 @@
21
21
  ],
22
22
  "dependencies": {
23
23
  "@ai-sdk/anthropic": "^0.0.50",
24
- "@dainprotocol/service-sdk": "^1.1.13",
24
+ "@dainprotocol/service-sdk": "^1.0.85",
25
25
  "@dainprotocol/tunnel": "^1.0.5",
26
26
  "@types/fs-extra": "^11.0.4",
27
27
  "@types/localtunnel": "^2.0.4",