@dainprotocol/cli 1.1.31 → 1.2.0

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.
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
25
35
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
36
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
37
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -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,8 @@ var childProcess = null;
64
64
  var watcher = null;
65
65
  var mf = null;
66
66
  var tunnelUrl = null;
67
+ var isFirstStart = true;
68
+ var serverStarted = false;
67
69
  function isPortAvailable(port) {
68
70
  return new Promise(function (resolve) {
69
71
  var server = (0, net_1.createServer)()
@@ -76,26 +78,38 @@ function isPortAvailable(port) {
76
78
  });
77
79
  });
78
80
  }
79
- function startProcess(command, options) {
81
+ function startProcess(command, options, isRestart) {
80
82
  var _a, _b;
83
+ if (isRestart === void 0) { isRestart = false; }
81
84
  if (childProcess) {
82
85
  childProcess.kill();
83
86
  }
84
- var spinner = (0, ora_1.default)('Starting development server...').start();
87
+ serverStarted = false; // Reset for each new process
88
+ var spinner = (0, ora_1.default)(isRestart ? 'Restarting development server...' : 'Starting development server...').start();
85
89
  childProcess = (0, child_process_1.exec)(command, options);
86
90
  (_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);
91
+ var dataStr = data.toString().trim();
92
+ console.log("[DEBUG] stdout event - serverStarted: ".concat(serverStarted, ", data: \"").concat(dataStr.substring(0, 50), "...\""));
93
+ if (!serverStarted) {
94
+ spinner.succeed(isRestart ? 'Development server restarted.' : 'Development server started.');
95
+ if (tunnelUrl && isFirstStart) {
96
+ (0, utils_1.displayTunnelUrl)(tunnelUrl);
97
+ isFirstStart = false;
98
+ }
99
+ serverStarted = true;
100
+ console.log("[DEBUG] Set serverStarted to true");
90
101
  }
91
102
  console.log(data);
92
103
  });
93
104
  (_b = childProcess.stderr) === null || _b === void 0 ? void 0 : _b.on('data', function (data) {
94
- spinner.fail('Development server error.');
105
+ if (!serverStarted) {
106
+ spinner.fail('Development server error.');
107
+ serverStarted = true; // Prevent multiple error messages too
108
+ }
95
109
  (0, utils_1.logError)(data);
96
110
  });
97
111
  childProcess.on('close', function (code) {
98
- if (code !== 0) {
112
+ if (code !== 0 && !serverStarted) {
99
113
  spinner.fail("Development server exited with code ".concat(code));
100
114
  }
101
115
  });
@@ -195,7 +209,7 @@ function dev(options) {
195
209
  });
196
210
  watcher.on('change', function (path) {
197
211
  (0, utils_1.logInfo)("File ".concat(path, " has been changed. Restarting..."));
198
- startProcess(command, { env: __assign(__assign({}, process.env), envVars) });
212
+ startProcess(command, { env: __assign(__assign({}, process.env), envVars) }, true);
199
213
  });
200
214
  (0, utils_1.logInfo)('Watching for file changes...');
201
215
  // Add signal handlers for graceful shutdown
@@ -26,13 +26,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
26
26
  }) : function(o, v) {
27
27
  o["default"] = v;
28
28
  });
29
- var __importStar = (this && this.__importStar) || function (mod) {
30
- if (mod && mod.__esModule) return mod;
31
- var result = {};
32
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
33
- __setModuleDefault(result, mod);
34
- return result;
35
- };
29
+ var __importStar = (this && this.__importStar) || (function () {
30
+ var ownKeys = function(o) {
31
+ ownKeys = Object.getOwnPropertyNames || function (o) {
32
+ var ar = [];
33
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
34
+ return ar;
35
+ };
36
+ return ownKeys(o);
37
+ };
38
+ return function (mod) {
39
+ if (mod && mod.__esModule) return mod;
40
+ var result = {};
41
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
42
+ __setModuleDefault(result, mod);
43
+ return result;
44
+ };
45
+ })();
36
46
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
37
47
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
38
48
  return new (P || (P = Promise))(function (resolve, reject) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dainprotocol/cli",
3
- "version": "1.1.31",
3
+ "version": "1.2.0",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -21,8 +21,8 @@
21
21
  ],
22
22
  "dependencies": {
23
23
  "@ai-sdk/anthropic": "^0.0.50",
24
- "@dainprotocol/service-sdk": "^1.1.13",
25
- "@dainprotocol/tunnel": "^1.0.5",
24
+ "@dainprotocol/service-sdk": "^1.3.0",
25
+ "@dainprotocol/tunnel": "^1.1.0",
26
26
  "@types/fs-extra": "^11.0.4",
27
27
  "@types/localtunnel": "^2.0.4",
28
28
  "ai": "^3.3.41",