@agent8/deploy 1.0.8 → 1.1.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.
Files changed (2) hide show
  1. package/bin/agent8.js +59 -46
  2. package/package.json +1 -1
package/bin/agent8.js CHANGED
@@ -6,26 +6,17 @@ 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
+ // Parse command line arguments
11
+ const args = process.argv.slice(2);
12
+ const previewMode = args.includes("--preview");
13
+ const prodMode = args.includes("--prod");
10
14
 
11
15
  const envFilePath = path.resolve(process.cwd(), ".env");
12
16
  const deployedFilePath = path.resolve(process.cwd(), ".deployed");
13
17
 
14
- // Generate random ID for account/verse if needed
15
- const generateRandomId = () => {
16
- return crypto.randomBytes(8).toString("hex");
17
- };
18
-
19
- let envConfig = {};
20
-
21
- // Check if .env exists, create if not
22
18
  if (!fs.existsSync(envFilePath)) {
23
- console.log("Creating new .env file with generated values");
24
- const defaultEnv = `VITE_AGENT8_ACCOUNT=${generateRandomId()}\nVITE_AGENT8_VERSE=${generateRandomId()}\n`;
25
- fs.writeFileSync(envFilePath, defaultEnv);
26
- envConfig = dotenv.parse(fs.readFileSync(envFilePath));
27
- } else {
28
- envConfig = dotenv.parse(fs.readFileSync(envFilePath));
19
+ fs.writeFileSync(envFilePath, "");
29
20
  }
30
21
 
31
22
  // Load previously deployed file hashes
@@ -39,42 +30,53 @@ if (fs.existsSync(deployedFilePath)) {
39
30
  }
40
31
  }
41
32
 
33
+ const envConfig = dotenv.parse(fs.readFileSync(envFilePath));
34
+
42
35
  const endpoint =
43
36
  envConfig.VITE_AGENT8_REMOTE_URL ||
44
37
  "https://verse8-simple-game-backend-609824224664.asia-northeast3.run.app";
45
38
 
46
- // If account/verse don't exist in .env, add them
47
39
  let verse = envConfig.VITE_AGENT8_VERSE;
48
40
  let account = envConfig.VITE_AGENT8_ACCOUNT;
49
- let envChanged = false;
41
+ const accessToken = process.env.V8_ACCESS_TOKEN;
50
42
 
51
- if (!verse) {
52
- verse = generateRandomId();
53
- envConfig.VITE_AGENT8_VERSE = verse;
54
- envChanged = true;
55
- console.log("Generated new verse ID:", verse);
56
- }
43
+ // Check required variables based on mode
44
+ if (previewMode || prodMode) {
45
+ if (!account) {
46
+ console.error("Error: VITE_AGENT8_ACCOUNT is required in .env file");
47
+ process.exit(1);
48
+ }
57
49
 
58
- if (!account) {
59
- account = generateRandomId();
60
- envConfig.VITE_AGENT8_ACCOUNT = account;
61
- envChanged = true;
62
- console.log("Generated new account ID:", account);
63
- }
50
+ if (!accessToken) {
51
+ console.error("Error: V8_ACCESS_TOKEN environment variable is required");
52
+ process.exit(1);
53
+ }
64
54
 
65
- // Save updated .env if needed
66
- if (envChanged) {
67
- const envContent = Object.entries(envConfig)
68
- .map(([key, value]) => `${key}=${value}`)
69
- .join("\n");
70
- fs.writeFileSync(envFilePath, envContent);
71
- console.log("Updated .env file with new values");
55
+ if (prodMode && !verse) {
56
+ console.error(
57
+ "Error: VITE_AGENT8_VERSE is required in .env file for production mode"
58
+ );
59
+ process.exit(1);
60
+ }
72
61
  }
73
62
 
74
- const accessToken = process.env.V8_ACCESS_TOKEN || "TESTER";
75
-
76
- console.log("Starting deployment to verse:", verse);
77
- console.log("Account:", account);
63
+ // Determine target verse based on mode
64
+ let targetVerse = verse;
65
+ if (previewMode) {
66
+ targetVerse = `${account}-preview`;
67
+ console.log("Preview mode: Deploying to preview verse:", targetVerse);
68
+ } else if (prodMode) {
69
+ console.log("Production mode: Deploying to verse:", targetVerse);
70
+ } else {
71
+ // Default mode (original behavior)
72
+ if (!verse) {
73
+ verse = crypto.randomBytes(16).toString("hex"); // Generate a random 32-byte string
74
+ fs.appendFileSync(envFilePath, `\nVITE_AGENT8_VERSE=${verse}\n`);
75
+ console.log("Generated random verse and updated .env:", verse);
76
+ }
77
+ targetVerse = verse;
78
+ console.log("Default mode: Deploying to verse:", targetVerse);
79
+ }
78
80
 
79
81
  // Files to upload
80
82
  const filesToUpload = [];
@@ -110,7 +112,7 @@ if (fs.existsSync(serverFilePath)) {
110
112
  }
111
113
  }
112
114
 
113
- if (command !== "server") {
115
+ if (!previewMode) {
114
116
  // Check for dist directory
115
117
  const distDirPath = path.resolve(process.cwd(), "dist");
116
118
  if (fs.existsSync(distDirPath) && fs.statSync(distDirPath).isDirectory()) {
@@ -153,6 +155,20 @@ if (filesToUpload.length === 0) {
153
155
 
154
156
  console.log(`Found ${filesToUpload.length} files to upload...`);
155
157
 
158
+ // Prepare authentication headers based on mode
159
+ const getAuthHeaders = (formHeaders) => {
160
+ if (previewMode || prodMode) {
161
+ return {
162
+ ...formHeaders,
163
+ Authorization: `Bearer ${accessToken}`,
164
+ };
165
+ } else {
166
+ return {
167
+ ...formHeaders,
168
+ };
169
+ }
170
+ };
171
+
156
172
  // Upload files in parallel
157
173
  const uploadPromises = filesToUpload.map((fileInfo) => {
158
174
  const { filePath, uploadPath, fileName } = fileInfo;
@@ -162,11 +178,8 @@ const uploadPromises = filesToUpload.map((fileInfo) => {
162
178
  form.append("path", uploadPath || "/");
163
179
 
164
180
  return axios
165
- .post(`${endpoint}/verses/${account}-${verse}/files`, form, {
166
- headers: {
167
- ...form.getHeaders(),
168
- "X-Access-Token": accessToken,
169
- },
181
+ .post(`${endpoint}/verses/${targetVerse}/files`, form, {
182
+ headers: getAuthHeaders(form.getHeaders()),
170
183
  })
171
184
  .then(() => {
172
185
  console.log(`Uploaded: ${uploadPath}/${fileName}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agent8/deploy",
3
- "version": "1.0.8",
3
+ "version": "1.1.0",
4
4
  "description": "A CLI tool for running agent8 commands",
5
5
  "main": "index.js",
6
6
  "bin": {