@agent8/deploy 1.0.0 → 1.0.2
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.
- package/bin/agent8.js +96 -64
- package/package.json +1 -1
package/bin/agent8.js
CHANGED
|
@@ -9,11 +9,23 @@ const crypto = require("crypto");
|
|
|
9
9
|
const [, , command, fnName, argsString] = process.argv;
|
|
10
10
|
|
|
11
11
|
const envFilePath = path.resolve(process.cwd(), ".env");
|
|
12
|
+
const deployedFilePath = path.resolve(process.cwd(), ".deployed");
|
|
12
13
|
|
|
13
14
|
if (!fs.existsSync(envFilePath)) {
|
|
14
15
|
fs.writeFileSync(envFilePath, "");
|
|
15
16
|
}
|
|
16
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
|
+
|
|
17
29
|
const envConfig = dotenv.parse(fs.readFileSync(envFilePath));
|
|
18
30
|
|
|
19
31
|
const endpoint =
|
|
@@ -24,28 +36,40 @@ let verse = envConfig.VITE_AGENT8_VERSE;
|
|
|
24
36
|
let account = envConfig.VITE_AGENT8_ACCOUNT || "0xtest";
|
|
25
37
|
let signature = envConfig.VITE_AGENT8_SIGNATURE;
|
|
26
38
|
|
|
27
|
-
if (
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
39
|
+
if (!verse) {
|
|
40
|
+
verse = crypto.randomBytes(16).toString("hex"); // Generate a random 32-byte string
|
|
41
|
+
fs.appendFileSync(envFilePath, `VITE_AGENT8_VERSE=${verse}\n`);
|
|
42
|
+
console.log("Generated random verse and updated .env:", verse);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
console.log("Starting deployment to verse:", verse);
|
|
33
46
|
|
|
34
|
-
|
|
47
|
+
// Files to upload
|
|
48
|
+
const filesToUpload = [];
|
|
49
|
+
const newHashes = {};
|
|
35
50
|
|
|
36
|
-
|
|
37
|
-
|
|
51
|
+
// Calculate file hash
|
|
52
|
+
const calculateFileHash = (filePath) => {
|
|
53
|
+
const fileContent = fs.readFileSync(filePath);
|
|
54
|
+
return crypto.createHash("md5").update(fileContent).digest("hex");
|
|
55
|
+
};
|
|
38
56
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
57
|
+
// Check for server.js
|
|
58
|
+
const serverFilePath = path.resolve(process.cwd(), "server.js");
|
|
59
|
+
if (fs.existsSync(serverFilePath)) {
|
|
60
|
+
const hash = calculateFileHash(serverFilePath);
|
|
61
|
+
newHashes["server.js"] = hash;
|
|
62
|
+
|
|
63
|
+
if (hash !== deployedHashes["server.js"]) {
|
|
42
64
|
filesToUpload.push({
|
|
43
65
|
filePath: serverFilePath,
|
|
44
66
|
uploadPath: "",
|
|
45
67
|
fileName: "server.js",
|
|
46
68
|
});
|
|
47
69
|
}
|
|
70
|
+
}
|
|
48
71
|
|
|
72
|
+
if (command !== "server") {
|
|
49
73
|
// Check for dist directory
|
|
50
74
|
const distDirPath = path.resolve(process.cwd(), "dist");
|
|
51
75
|
if (fs.existsSync(distDirPath) && fs.statSync(distDirPath).isDirectory()) {
|
|
@@ -56,70 +80,78 @@ if (command === "deploy") {
|
|
|
56
80
|
const fullPath = path.join(dirPath, file);
|
|
57
81
|
const relativePath = path.join(basePath, file);
|
|
58
82
|
const uploadPath = path.dirname(relativePath);
|
|
83
|
+
const deployKey = path
|
|
84
|
+
.join(uploadPath, path.basename(file))
|
|
85
|
+
.replace(/\\/g, "/");
|
|
59
86
|
|
|
60
87
|
if (fs.statSync(fullPath).isDirectory()) {
|
|
61
88
|
processDistFiles(fullPath, relativePath);
|
|
62
89
|
} else {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
90
|
+
const hash = calculateFileHash(fullPath);
|
|
91
|
+
newHashes[deployKey] = hash;
|
|
92
|
+
|
|
93
|
+
if (hash !== deployedHashes[deployKey]) {
|
|
94
|
+
filesToUpload.push({
|
|
95
|
+
filePath: fullPath,
|
|
96
|
+
uploadPath: uploadPath === "." ? "" : uploadPath,
|
|
97
|
+
fileName: path.basename(file),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
68
100
|
}
|
|
69
101
|
});
|
|
70
102
|
};
|
|
71
103
|
|
|
72
104
|
processDistFiles(distDirPath);
|
|
73
105
|
}
|
|
106
|
+
}
|
|
74
107
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
process.exit(1);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
console.log(`Found ${filesToUpload.length} files to upload...`);
|
|
83
|
-
|
|
84
|
-
// Upload files in parallel
|
|
85
|
-
const uploadPromises = filesToUpload.map((fileInfo) => {
|
|
86
|
-
const { filePath, uploadPath, fileName } = fileInfo;
|
|
87
|
-
const form = new FormData();
|
|
88
|
-
const fileContent = fs.readFileSync(filePath);
|
|
89
|
-
form.append("file", fileContent, fileName);
|
|
90
|
-
form.append("path", uploadPath || "/");
|
|
91
|
-
|
|
92
|
-
return axios
|
|
93
|
-
.post(`${endpoint}/verses/${verse}/files`, form, {
|
|
94
|
-
headers: {
|
|
95
|
-
...form.getHeaders(),
|
|
96
|
-
"X-Signature": signature,
|
|
97
|
-
},
|
|
98
|
-
})
|
|
99
|
-
.then(() => {
|
|
100
|
-
// 경로 출력 처리
|
|
101
|
-
console.log(`Uploaded: ${uploadPath}/${fileName}`);
|
|
102
|
-
return true;
|
|
103
|
-
})
|
|
104
|
-
.catch((error) => {
|
|
105
|
-
console.error(
|
|
106
|
-
`Error uploading ${uploadPath}/${fileName}:`,
|
|
107
|
-
error.message
|
|
108
|
-
);
|
|
109
|
-
return false;
|
|
110
|
-
});
|
|
111
|
-
});
|
|
108
|
+
if (filesToUpload.length === 0) {
|
|
109
|
+
console.log("No changes detected. Nothing to deploy.");
|
|
110
|
+
process.exit(0);
|
|
111
|
+
}
|
|
112
112
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
113
|
+
console.log(`Found ${filesToUpload.length} files to upload...`);
|
|
114
|
+
|
|
115
|
+
// Upload files in parallel
|
|
116
|
+
const uploadPromises = filesToUpload.map((fileInfo) => {
|
|
117
|
+
const { filePath, uploadPath, fileName } = fileInfo;
|
|
118
|
+
const form = new FormData();
|
|
119
|
+
const fileContent = fs.readFileSync(filePath);
|
|
120
|
+
form.append("file", fileContent, fileName);
|
|
121
|
+
form.append("path", uploadPath || "/");
|
|
122
|
+
|
|
123
|
+
return axios
|
|
124
|
+
.post(`${endpoint}/verses/${verse}/files`, form, {
|
|
125
|
+
headers: {
|
|
126
|
+
...form.getHeaders(),
|
|
127
|
+
"X-Signature": signature,
|
|
128
|
+
},
|
|
129
|
+
})
|
|
130
|
+
.then(() => {
|
|
131
|
+
// 경로 출력 처리
|
|
132
|
+
console.log(`Uploaded: ${uploadPath}/${fileName}`);
|
|
133
|
+
return true;
|
|
119
134
|
})
|
|
120
135
|
.catch((error) => {
|
|
121
|
-
console.error(
|
|
136
|
+
console.error(
|
|
137
|
+
`Error uploading ${uploadPath}/${fileName}:`,
|
|
138
|
+
error.message
|
|
139
|
+
);
|
|
140
|
+
return false;
|
|
122
141
|
});
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
Promise.all(uploadPromises)
|
|
145
|
+
.then((results) => {
|
|
146
|
+
const successCount = results.filter(Boolean).length;
|
|
147
|
+
console.log(
|
|
148
|
+
`Deployment complete: ${successCount}/${filesToUpload.length} files uploaded successfully.`
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
// Save new hashes to .deployed file
|
|
152
|
+
fs.writeFileSync(deployedFilePath, JSON.stringify(newHashes, null, 2));
|
|
153
|
+
console.log("Updated .deployed file with new file hashes");
|
|
154
|
+
})
|
|
155
|
+
.catch((error) => {
|
|
156
|
+
console.error("Deployment failed:", error.message);
|
|
157
|
+
});
|