@dainprotocol/cli 1.2.36 → 1.3.1

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,293 +1,149 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- 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;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
1
+ import { DEFAULT_API_BASE_URL, fetchWithTimeout, getDainConfig, joinUrl, logError, parseEnvContent, } from "../utils.js";
2
+ import ora from "ora";
3
+ import fs from "fs-extra";
4
+ import path from "path";
5
+ import archiver from "archiver";
6
+ import build from "./build.js";
7
+ const START_DEPLOY_URI = "/api/app/data/deployments/start-deploy";
8
+ const DEPLOY_TIMEOUT_MS = 120000;
9
+ export default async function deploy(options) {
10
+ const config = getDainConfig(options.config);
11
+ const spinner = ora("Deploying project...").start();
12
+ const basePath = config["api-base-url"] || DEFAULT_API_BASE_URL;
13
+ const deployPath = joinUrl(basePath, START_DEPLOY_URI);
14
+ const isProduction = config["environment"] === "production";
15
+ const environmentName = config["environment"] || "production";
16
+ const { repoName, branchName } = checkGitConfig();
17
+ if (repoName && branchName)
18
+ spinner.info("Found git config, checking if project exists...").start();
19
+ const projectId = repoName && branchName ? await getProjectId({ basePath, repoName }) : null;
20
+ if (!projectId && repoName && branchName)
21
+ spinner.fail("No project found on platform linked to this repository.");
22
+ try {
23
+ let result;
24
+ if (projectId && repoName && branchName) {
25
+ spinner.info("Deploying project using github repository...").start();
26
+ const response = await fetchWithTimeout(joinUrl(basePath, `/api/app/codegen/data/projects/${projectId}/start-deploy`), {
27
+ method: "POST",
28
+ headers: { "Content-Type": "application/json" },
29
+ body: JSON.stringify({ environment: environmentName, isProduction, branch: branchName }),
30
+ }, DEPLOY_TIMEOUT_MS);
31
+ if (!response.ok)
32
+ throw new Error(`Deploy failed: ${response.status} ${response.statusText}`);
33
+ result = await response.json();
34
+ }
35
+ else {
36
+ spinner.info("Deploying project using local files...").start();
37
+ const envArray = await loadAndValidateEnvVariables();
38
+ await build({ config: options.config, deploy: true });
39
+ result = await deployAndPushFiles(deployPath, envArray, isProduction, environmentName);
40
+ try {
41
+ await fetchWithTimeout(result.service.url, { method: "GET" }, 10000);
32
42
  }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
43
+ catch { }
44
+ }
45
+ await updateDainJson(result);
46
+ printDeploymentResult(result, spinner);
47
+ process.exit(0);
48
+ }
49
+ catch (error) {
50
+ spinner.fail("Deployment failed.");
51
+ logError("Error during deployment: ", error);
52
+ process.exit(1);
36
53
  }
37
- };
38
- var __importDefault = (this && this.__importDefault) || function (mod) {
39
- return (mod && mod.__esModule) ? mod : { "default": mod };
40
- };
41
- Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.default = deploy;
43
- var utils_1 = require("../utils");
44
- var ora_1 = __importDefault(require("ora"));
45
- var fs_extra_1 = __importDefault(require("fs-extra"));
46
- var path_1 = __importDefault(require("path"));
47
- var archiver_1 = __importDefault(require("archiver"));
48
- var build_1 = __importDefault(require("./build"));
49
- var START_DEPLOY_URI = "/api/app/data/deployments/start-deploy";
50
- var DEPLOY_TIMEOUT_MS = 120000;
51
- function deploy(options) {
52
- return __awaiter(this, void 0, void 0, function () {
53
- var config, spinner, basePath, deployPath, isProduction, environmentName, _a, repoName, branchName, projectId, _b, result, response, envArray, _c, error_1;
54
- return __generator(this, function (_d) {
55
- switch (_d.label) {
56
- case 0:
57
- config = (0, utils_1.getDainConfig)(options.config);
58
- spinner = (0, ora_1.default)("Deploying project...").start();
59
- basePath = config["api-base-url"] || utils_1.DEFAULT_API_BASE_URL;
60
- deployPath = (0, utils_1.joinUrl)(basePath, START_DEPLOY_URI);
61
- isProduction = config["environment"] === "production";
62
- environmentName = config["environment"] || "production";
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 })];
68
- case 1:
69
- _b = _d.sent();
70
- return [3 /*break*/, 3];
71
- case 2:
72
- _b = null;
73
- _d.label = 3;
74
- case 3:
75
- projectId = _b;
76
- if (!projectId && repoName && branchName)
77
- spinner.fail("No project found on platform linked to this repository.");
78
- _d.label = 4;
79
- case 4:
80
- _d.trys.push([4, 16, , 17]);
81
- result = void 0;
82
- if (!(projectId && repoName && branchName)) return [3 /*break*/, 7];
83
- spinner.info("Deploying project using github repository...").start();
84
- return [4 /*yield*/, (0, utils_1.fetchWithTimeout)((0, utils_1.joinUrl)(basePath, "/api/app/codegen/data/projects/".concat(projectId, "/start-deploy")), {
85
- method: "POST",
86
- headers: { "Content-Type": "application/json" },
87
- body: JSON.stringify({ environment: environmentName, isProduction: isProduction, branch: branchName }),
88
- }, DEPLOY_TIMEOUT_MS)];
89
- case 5:
90
- response = _d.sent();
91
- if (!response.ok)
92
- throw new Error("Deploy failed: ".concat(response.status, " ").concat(response.statusText));
93
- return [4 /*yield*/, response.json()];
94
- case 6:
95
- result = _d.sent();
96
- return [3 /*break*/, 14];
97
- case 7:
98
- spinner.info("Deploying project using local files...").start();
99
- return [4 /*yield*/, loadAndValidateEnvVariables()];
100
- case 8:
101
- envArray = _d.sent();
102
- return [4 /*yield*/, (0, build_1.default)({ config: options.config, deploy: true })];
103
- case 9:
104
- _d.sent();
105
- return [4 /*yield*/, deployAndPushFiles(deployPath, envArray, isProduction, environmentName)];
106
- case 10:
107
- result = _d.sent();
108
- _d.label = 11;
109
- case 11:
110
- _d.trys.push([11, 13, , 14]);
111
- return [4 /*yield*/, (0, utils_1.fetchWithTimeout)(result.service.url, { method: "GET" }, 10000)];
112
- case 12:
113
- _d.sent();
114
- return [3 /*break*/, 14];
115
- case 13:
116
- _c = _d.sent();
117
- return [3 /*break*/, 14];
118
- case 14: return [4 /*yield*/, updateDainJson(result)];
119
- case 15:
120
- _d.sent();
121
- printDeploymentResult(result, spinner);
122
- process.exit(0);
123
- return [3 /*break*/, 17];
124
- case 16:
125
- error_1 = _d.sent();
126
- spinner.fail("Deployment failed.");
127
- (0, utils_1.logError)("Error during deployment: ", error_1);
128
- process.exit(1);
129
- return [3 /*break*/, 17];
130
- case 17: return [2 /*return*/];
131
- }
132
- });
133
- });
134
54
  }
135
- function loadAndValidateEnvVariables() {
136
- return __awaiter(this, void 0, void 0, function () {
137
- var envArray;
138
- return __generator(this, function (_a) {
139
- switch (_a.label) {
140
- case 0: return [4 /*yield*/, loadEnvVariables()];
141
- case 1:
142
- envArray = _a.sent();
143
- if (envArray.length === 0) {
144
- (0, utils_1.logError)("No environment variables found. Please ensure your .env file is correctly set up.");
145
- process.exit(1);
146
- }
147
- if (!envArray.some(function (env) { return env.name === "DAIN_API_KEY"; })) {
148
- (0, utils_1.logError)("DAIN_API_KEY not found in environment variables. Please check your .env file.");
149
- process.exit(1);
150
- }
151
- return [2 /*return*/, envArray];
152
- }
153
- });
154
- });
55
+ async function loadAndValidateEnvVariables() {
56
+ const envArray = await loadEnvVariables();
57
+ if (envArray.length === 0) {
58
+ logError("No environment variables found. Please ensure your .env file is correctly set up.");
59
+ process.exit(1);
60
+ }
61
+ if (!envArray.some((env) => env.name === "DAIN_API_KEY")) {
62
+ logError("DAIN_API_KEY not found in environment variables. Please check your .env file.");
63
+ process.exit(1);
64
+ }
65
+ return envArray;
155
66
  }
156
- function deployAndPushFiles(startDeployUrl, envArray, isProduction, environmentName) {
157
- return __awaiter(this, void 0, void 0, function () {
158
- var projectZip, projectZipBuffer, formData, response, errorText;
159
- return __generator(this, function (_a) {
160
- switch (_a.label) {
161
- case 0: return [4 /*yield*/, zipDirectory("./", "project.zip")];
162
- case 1:
163
- projectZip = _a.sent();
164
- _a.label = 2;
165
- case 2:
166
- _a.trys.push([2, , 7, 9]);
167
- return [4 /*yield*/, fs_extra_1.default.readFile(projectZip)];
168
- case 3:
169
- projectZipBuffer = _a.sent();
170
- formData = new FormData();
171
- formData.append("array", JSON.stringify(envArray));
172
- formData.append("file", new Blob([new Uint8Array(projectZipBuffer)]), "project.zip");
173
- formData.append("isProduction", isProduction.toString());
174
- formData.append("environment", environmentName);
175
- return [4 /*yield*/, (0, utils_1.fetchWithTimeout)(startDeployUrl, { method: "POST", body: formData }, DEPLOY_TIMEOUT_MS)];
176
- case 4:
177
- response = _a.sent();
178
- if (!!response.ok) return [3 /*break*/, 6];
179
- return [4 /*yield*/, response.text()];
180
- case 5:
181
- errorText = _a.sent();
182
- throw new Error("Deployment failed: ".concat(response.status, " ").concat(response.statusText, ". Response: ").concat(errorText));
183
- case 6: return [2 /*return*/, response.json()];
184
- case 7: return [4 /*yield*/, fs_extra_1.default.remove(projectZip).catch(function () { return undefined; })];
185
- case 8:
186
- _a.sent();
187
- return [7 /*endfinally*/];
188
- case 9: return [2 /*return*/];
189
- }
190
- });
191
- });
67
+ async function deployAndPushFiles(startDeployUrl, envArray, isProduction, environmentName) {
68
+ const projectZip = await zipDirectory("./", "project.zip");
69
+ try {
70
+ const projectZipBuffer = await fs.readFile(projectZip);
71
+ const formData = new FormData();
72
+ formData.append("array", JSON.stringify(envArray));
73
+ formData.append("file", new Blob([new Uint8Array(projectZipBuffer)]), "project.zip");
74
+ formData.append("isProduction", isProduction.toString());
75
+ formData.append("environment", environmentName);
76
+ const response = await fetchWithTimeout(startDeployUrl, { method: "POST", body: formData }, DEPLOY_TIMEOUT_MS);
77
+ if (!response.ok) {
78
+ const errorText = await response.text();
79
+ throw new Error(`Deployment failed: ${response.status} ${response.statusText}. Response: ${errorText}`);
80
+ }
81
+ return response.json();
82
+ }
83
+ finally {
84
+ await fs.remove(projectZip).catch(() => undefined);
85
+ }
192
86
  }
193
- function loadEnvVariables() {
194
- return __awaiter(this, void 0, void 0, function () {
195
- var files, envFile, _a;
196
- return __generator(this, function (_b) {
197
- switch (_b.label) {
198
- case 0: return [4 /*yield*/, fs_extra_1.default.readdir("./")];
199
- case 1:
200
- files = _b.sent();
201
- envFile = files.find(function (file) { return file === ".env"; });
202
- if (!envFile) {
203
- (0, utils_1.logError)("Environment file not found. Please create a .env file in the build directory.");
204
- process.exit(1);
205
- }
206
- _a = utils_1.parseEnvContent;
207
- return [4 /*yield*/, fs_extra_1.default.readFile(path_1.default.join("./", envFile), "utf-8")];
208
- case 2: return [2 /*return*/, _a.apply(void 0, [_b.sent()])];
209
- }
210
- });
211
- });
87
+ async function loadEnvVariables() {
88
+ const files = await fs.readdir("./");
89
+ const envFile = files.find((file) => file === ".env");
90
+ if (!envFile) {
91
+ logError("Environment file not found. Please create a .env file in the build directory.");
92
+ process.exit(1);
93
+ }
94
+ return parseEnvContent(await fs.readFile(path.join("./", envFile), "utf-8"));
212
95
  }
213
96
  function zipDirectory(sourceDir, outputZip) {
214
- return new Promise(function (resolve, reject) {
215
- var output = fs_extra_1.default.createWriteStream(outputZip);
216
- var archive = (0, archiver_1.default)("zip", { zlib: { level: 9 } });
217
- output.on("close", function () { return resolve(outputZip); });
218
- archive.on("error", function (err) { (0, utils_1.logError)("Error during zipping: ", err); reject(err); });
97
+ return new Promise((resolve, reject) => {
98
+ const output = fs.createWriteStream(outputZip);
99
+ const archive = archiver("zip", { zlib: { level: 9 } });
100
+ output.on("close", () => resolve(outputZip));
101
+ archive.on("error", (err) => { logError("Error during zipping: ", err); reject(err); });
219
102
  archive.pipe(output);
220
103
  archive.glob("**/*", { ignore: ["node_modules/**", "project.zip", ".git/**", ".env*", ".dain/**"] });
221
104
  archive.finalize();
222
105
  });
223
106
  }
224
107
  function checkGitConfig() {
225
- var _a, _b, _c;
226
108
  try {
227
- var gitConfig = fs_extra_1.default.readFileSync(".git/config", "utf-8");
228
- var remote = gitConfig.match(/\[remote "origin"\][\s\S]*?url = ([^\n]+)/);
229
- var remoteUrl = remote ? remote[1].trim() : null;
230
- var repoName = ((_a = remoteUrl === null || remoteUrl === void 0 ? void 0 : remoteUrl.match(/git@github.com:(.+)\.git/)) === null || _a === void 0 ? void 0 : _a[1])
231
- || ((_b = remoteUrl === null || remoteUrl === void 0 ? void 0 : remoteUrl.match(/https?:\/\/github.com\/(.+)\.git/)) === null || _b === void 0 ? void 0 : _b[1])
109
+ const gitConfig = fs.readFileSync(".git/config", "utf-8");
110
+ const remote = gitConfig.match(/\[remote "origin"\][\s\S]*?url = ([^\n]+)/);
111
+ const remoteUrl = remote ? remote[1].trim() : null;
112
+ const repoName = remoteUrl?.match(/git@github.com:(.+)\.git/)?.[1]
113
+ || remoteUrl?.match(/https?:\/\/github.com\/(.+)\.git/)?.[1]
232
114
  || null;
233
- var gitHead = fs_extra_1.default.readFileSync(".git/HEAD", "utf-8");
234
- var branchName = ((_c = gitHead.match(/ref: refs\/heads\/(.*)/)) === null || _c === void 0 ? void 0 : _c[1]) || null;
235
- return { repoName: repoName, branchName: branchName };
115
+ const gitHead = fs.readFileSync(".git/HEAD", "utf-8");
116
+ const branchName = gitHead.match(/ref: refs\/heads\/(.*)/)?.[1] || null;
117
+ return { repoName, branchName };
236
118
  }
237
- catch (_d) {
119
+ catch {
238
120
  return { repoName: null, branchName: null };
239
121
  }
240
122
  }
241
- function getProjectId(_a) {
242
- return __awaiter(this, arguments, void 0, function (_b) {
243
- var response, _c;
244
- var basePath = _b.basePath, repoName = _b.repoName;
245
- return __generator(this, function (_d) {
246
- switch (_d.label) {
247
- case 0:
248
- _d.trys.push([0, 2, , 3]);
249
- return [4 /*yield*/, (0, utils_1.fetchWithTimeout)((0, utils_1.joinUrl)(basePath, "/api/app/codegen/data/projects/get-by-repo?repo=".concat(encodeURIComponent(repoName))), { method: "GET" })];
250
- case 1:
251
- response = _d.sent();
252
- return [2 /*return*/, response.ok ? response.json() : null];
253
- case 2:
254
- _c = _d.sent();
255
- return [2 /*return*/, null];
256
- case 3: return [2 /*return*/];
257
- }
258
- });
259
- });
123
+ async function getProjectId({ basePath, repoName }) {
124
+ try {
125
+ const response = await fetchWithTimeout(joinUrl(basePath, `/api/app/codegen/data/projects/get-by-repo?repo=${encodeURIComponent(repoName)}`), { method: "GET" });
126
+ return response.ok ? response.json() : null;
127
+ }
128
+ catch {
129
+ return null;
130
+ }
260
131
  }
261
- function updateDainJson(result) {
262
- return __awaiter(this, void 0, void 0, function () {
263
- var dainJson, _a, _b, error_2;
264
- return __generator(this, function (_c) {
265
- switch (_c.label) {
266
- case 0:
267
- _c.trys.push([0, 3, , 4]);
268
- _b = (_a = JSON).parse;
269
- return [4 /*yield*/, fs_extra_1.default.readFile("./dain.json", "utf-8")];
270
- case 1:
271
- dainJson = _b.apply(_a, [_c.sent()]);
272
- dainJson["deployment-id"] = result.deploymentId;
273
- dainJson["service-id"] = result.serviceId;
274
- return [4 /*yield*/, fs_extra_1.default.writeFile("./dain.json", JSON.stringify(dainJson, null, 2))];
275
- case 2:
276
- _c.sent();
277
- return [3 /*break*/, 4];
278
- case 3:
279
- error_2 = _c.sent();
280
- (0, utils_1.logError)("Failed to update dain.json", error_2);
281
- return [3 /*break*/, 4];
282
- case 4: return [2 /*return*/];
283
- }
284
- });
285
- });
132
+ async function updateDainJson(result) {
133
+ try {
134
+ const dainJson = JSON.parse(await fs.readFile("./dain.json", "utf-8"));
135
+ dainJson["deployment-id"] = result.deploymentId;
136
+ dainJson["service-id"] = result.serviceId;
137
+ await fs.writeFile("./dain.json", JSON.stringify(dainJson, null, 2));
138
+ }
139
+ catch (error) {
140
+ logError("Failed to update dain.json", error);
141
+ }
286
142
  }
287
143
  function printDeploymentResult(result, spinner) {
288
144
  console.log("\n-----------------------");
289
- spinner.succeed("Deployment URL: ".concat(result.service.url));
290
- spinner.succeed("Deployment ID: ".concat(result.deploymentId));
145
+ spinner.succeed(`Deployment URL: ${result.service.url}`);
146
+ spinner.succeed(`Deployment ID: ${result.deploymentId}`);
291
147
  spinner.succeed("Deployment completed at: " + new Date().toISOString());
292
148
  spinner.succeed("Deployment completed successfully.");
293
149
  spinner.info("You can access logs using `dain logs -w` command.");
@@ -0,0 +1,6 @@
1
+ export default function dev(options: {
2
+ port: string;
3
+ noproxy: boolean;
4
+ config?: string;
5
+ runtime?: 'node' | 'bun' | 'workers';
6
+ }): Promise<void>;