@agent8/deploy 1.0.1 → 1.0.3

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.
Files changed (2) hide show
  1. package/bin/agent8.js +79 -33
  2. package/package.json +1 -1
package/bin/agent8.js CHANGED
@@ -6,13 +6,26 @@ const dotenv = require("dotenv");
6
6
  const axios = require("axios");
7
7
  const FormData = require("form-data");
8
8
  const crypto = require("crypto");
9
+ const [, , command, fnName, argsString] = process.argv;
9
10
 
10
11
  const envFilePath = path.resolve(process.cwd(), ".env");
12
+ const deployedFilePath = path.resolve(process.cwd(), ".deployed");
11
13
 
12
14
  if (!fs.existsSync(envFilePath)) {
13
15
  fs.writeFileSync(envFilePath, "");
14
16
  }
15
17
 
18
+ // Load previously deployed file hashes
19
+ let deployedHashes = {};
20
+ if (fs.existsSync(deployedFilePath)) {
21
+ try {
22
+ deployedHashes = JSON.parse(fs.readFileSync(deployedFilePath, "utf8"));
23
+ } catch (error) {
24
+ console.warn("Failed to parse .deployed file, will redeploy all files");
25
+ deployedHashes = {};
26
+ }
27
+ }
28
+
16
29
  const envConfig = dotenv.parse(fs.readFileSync(envFilePath));
17
30
 
18
31
  const endpoint =
@@ -33,48 +46,77 @@ console.log("Starting deployment to verse:", verse);
33
46
 
34
47
  // Files to upload
35
48
  const filesToUpload = [];
49
+ const newHashes = {};
50
+
51
+ // Calculate file hash
52
+ const calculateFileHash = (filePath) => {
53
+ const fileContent = fs.readFileSync(filePath);
54
+ return crypto.createHash("md5").update(fileContent).digest("hex");
55
+ };
36
56
 
37
57
  // Check for server.js
38
58
  const serverFilePath = path.resolve(process.cwd(), "server.js");
39
59
  if (fs.existsSync(serverFilePath)) {
40
- filesToUpload.push({
41
- filePath: serverFilePath,
42
- uploadPath: "",
43
- fileName: "server.js",
44
- });
60
+ // Validate server.js syntax before deployment
61
+ try {
62
+ const serverContent = fs.readFileSync(serverFilePath, "utf8");
63
+ eval(`(function() { ${serverContent}; new Server(); })`)();
64
+
65
+ const hash = calculateFileHash(serverFilePath);
66
+ newHashes["server.js"] = hash;
67
+
68
+ if (hash !== deployedHashes["server.js"]) {
69
+ filesToUpload.push({
70
+ filePath: serverFilePath,
71
+ uploadPath: "",
72
+ fileName: "server.js",
73
+ });
74
+ }
75
+ } catch (error) {
76
+ console.error("Error in server.js syntax:", error.message);
77
+ process.exit(1);
78
+ }
45
79
  }
46
80
 
47
- // Check for dist directory
48
- const distDirPath = path.resolve(process.cwd(), "dist");
49
- if (fs.existsSync(distDirPath) && fs.statSync(distDirPath).isDirectory()) {
50
- const processDistFiles = (dirPath, basePath = "") => {
51
- const files = fs.readdirSync(dirPath);
52
-
53
- files.forEach((file) => {
54
- const fullPath = path.join(dirPath, file);
55
- const relativePath = path.join(basePath, file);
56
- const uploadPath = path.dirname(relativePath);
57
-
58
- if (fs.statSync(fullPath).isDirectory()) {
59
- processDistFiles(fullPath, relativePath);
60
- } else {
61
- filesToUpload.push({
62
- filePath: fullPath,
63
- uploadPath: uploadPath === "." ? "" : uploadPath,
64
- fileName: path.basename(file),
65
- });
66
- }
67
- });
68
- };
69
-
70
- processDistFiles(distDirPath);
81
+ if (command !== "server") {
82
+ // Check for dist directory
83
+ const distDirPath = path.resolve(process.cwd(), "dist");
84
+ if (fs.existsSync(distDirPath) && fs.statSync(distDirPath).isDirectory()) {
85
+ const processDistFiles = (dirPath, basePath = "") => {
86
+ const files = fs.readdirSync(dirPath);
87
+
88
+ files.forEach((file) => {
89
+ const fullPath = path.join(dirPath, file);
90
+ const relativePath = path.join(basePath, file);
91
+ const uploadPath = path.dirname(relativePath);
92
+ const deployKey = path
93
+ .join(uploadPath, path.basename(file))
94
+ .replace(/\\/g, "/");
95
+
96
+ if (fs.statSync(fullPath).isDirectory()) {
97
+ processDistFiles(fullPath, relativePath);
98
+ } else {
99
+ const hash = calculateFileHash(fullPath);
100
+ newHashes[deployKey] = hash;
101
+
102
+ if (hash !== deployedHashes[deployKey]) {
103
+ filesToUpload.push({
104
+ filePath: fullPath,
105
+ uploadPath: uploadPath === "." ? "" : uploadPath,
106
+ fileName: path.basename(file),
107
+ });
108
+ }
109
+ }
110
+ });
111
+ };
112
+
113
+ processDistFiles(distDirPath);
114
+ }
71
115
  }
72
116
 
73
117
  if (filesToUpload.length === 0) {
74
- console.error(
75
- "No files found to upload. Make sure server.js or dist/ directory exists."
76
- );
77
- process.exit(1);
118
+ console.log("No changes detected. Nothing to deploy.");
119
+ process.exit(0);
78
120
  }
79
121
 
80
122
  console.log(`Found ${filesToUpload.length} files to upload...`);
@@ -114,6 +156,10 @@ Promise.all(uploadPromises)
114
156
  console.log(
115
157
  `Deployment complete: ${successCount}/${filesToUpload.length} files uploaded successfully.`
116
158
  );
159
+
160
+ // Save new hashes to .deployed file
161
+ fs.writeFileSync(deployedFilePath, JSON.stringify(newHashes, null, 2));
162
+ console.log("Updated .deployed file with new file hashes");
117
163
  })
118
164
  .catch((error) => {
119
165
  console.error("Deployment failed:", error.message);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agent8/deploy",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "A CLI tool for running agent8 commands",
5
5
  "main": "index.js",
6
6
  "bin": {