@aptos-labs/aptos-cli 0.0.8 → 0.0.10

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/aptos +145 -39
  2. package/package.json +1 -1
package/bin/aptos CHANGED
@@ -1,12 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const { spawn } = require("child_process");
3
+ const https = require("https");
4
4
  const fs = require("fs");
5
5
  const os = require("os");
6
6
  const path = require("path");
7
-
8
- const PNAME = "aptos-cli";
9
- const VERSION = "2.0.2";
7
+ const { spawn, spawnSync } = require("child_process");
10
8
 
11
9
  // Get all arguments passed to the script
12
10
  const getOS = () => {
@@ -23,51 +21,159 @@ const getOS = () => {
23
21
  }
24
22
  };
25
23
 
26
- const main = async () => {
27
- const os = getOS();
28
- const binaryName = `${PNAME}-${VERSION}-${os}`;
24
+ const PNAME = "aptos-cli";
25
+ const VERSION = "2.0.2";
26
+ const currentOS = getOS();
27
+ const url = `https://github.com/aptos-labs/aptos-core/releases/download/${PNAME}-v${VERSION}/${PNAME}-${VERSION}-${currentOS}-x86_64.zip`;
29
28
 
30
- // If binary does not exist, download it
31
- if (!fs.existsSync(`${__dirname}/${binaryName}`)) {
32
- await new Promise((resolve, reject) => {
33
- const url = `https://github.com/aptos-labs/aptos-core/releases/download/${PNAME}-v${VERSION}/${PNAME}-${VERSION}-${os}-x86_64.zip`;
34
- console.log("Downloading aptos CLI...");
35
- let downloadProcess;
36
- if (os === "Windows") {
37
- downloadProcess = spawn(
38
- `cmd.exe /c "curl -o C:\\tmp\\aptos.zip ${url} && powershell -Command \"Expand-Archive -Path C:\\tmp\\aptos.zip -DestinationPath C:\\tmp -Force\" && move C:\\tmp\\aptos ${path.join(
39
- __dirname,
40
- binaryName
41
- )}"`,
42
- {
43
- stdio: "inherit",
44
- cwd: __dirname,
45
- }
29
+ // Determine the appropriate unzip command based on the operating system
30
+ const unzipCommand =
31
+ process.platform === "win32"
32
+ ? 'powershell -Command "Expand-Archive"'
33
+ : "unzip";
34
+
35
+ // Define the path to the zip file and the destination directory
36
+ const zipFileName = path.basename(url);
37
+ const binaryName = `${PNAME}-${VERSION}-${currentOS}`;
38
+ const tmpDir = path.join(__dirname, "tmp");
39
+
40
+ // Ensure the destination directory exists
41
+ if (!fs.existsSync(tmpDir)) {
42
+ fs.mkdirSync(tmpDir, { recursive: true });
43
+ }
44
+
45
+ async function downloadBinary(url) {
46
+ await new Promise((resolve, reject) => {
47
+ https.get(url, (response) => {
48
+ if (response.statusCode === 302 && response.headers.location) {
49
+ // If it's a redirect, follow the new location and download from there
50
+ resolve(downloadBinary(response.headers.location));
51
+ } else if (response.statusCode === 200) {
52
+ // Pipe the response to a writable stream to save the zip file
53
+ const writeStream = fs.createWriteStream(
54
+ path.join(tmpDir, zipFileName)
46
55
  );
56
+ response.pipe(writeStream);
57
+
58
+ writeStream.on("finish", () => {
59
+ console.log(`File "${zipFileName}" downloaded successfully.`);
60
+
61
+ // Unzip the downloaded zip file
62
+ const unzipProcess = spawn(unzipCommand, [
63
+ path.join(tmpDir, zipFileName),
64
+ "-d",
65
+ tmpDir,
66
+ ]);
67
+
68
+ unzipProcess.on("close", (code) => {
69
+ if (code === 0) {
70
+ console.log(`File "${zipFileName}" unzipped successfully.`);
71
+
72
+ // Copy the unzipped files to the current working directory
73
+ const sourceDir = path.join(tmpDir, "aptos");
74
+
75
+ fs.renameSync(sourceDir, `${__dirname}/${binaryName}`);
76
+
77
+ console.log(`Files copied to current working directory.`);
78
+
79
+ // Remove the temporary directory
80
+ fs.rmSync(tmpDir, { recursive: true });
81
+
82
+ resolve();
83
+ } else {
84
+ console.error(`Failed to unzip file: ${code}`);
85
+ }
86
+ });
87
+ });
47
88
  } else {
48
- downloadProcess = spawn(
49
- `curl -L -o /tmp/aptos.zip ${url}; unzip -o -q /tmp/aptos.zip -d /tmp; mv /tmp/aptos ${__dirname}/${binaryName};`,
50
- {
51
- stdio: "inherit",
52
- shell: true,
53
- }
89
+ console.error(
90
+ `Failed to download file: ${response.statusCode} ${response.statusMessage}`
54
91
  );
92
+ reject("Error occurred while downloading aptos CLI");
55
93
  }
56
-
57
- downloadProcess.on("close", (code) => {
58
- if (code !== 0) {
59
- reject("Error occurred while downloading aptos CLI");
60
- }
61
- console.log("Download complete");
62
- resolve();
63
- });
64
94
  });
65
- }
95
+ });
96
+ }
66
97
 
98
+ const main = async () => {
99
+ if (!fs.existsSync(`${__dirname}/${binaryName}`)) {
100
+ await downloadBinary(url);
101
+ }
67
102
  // Spawn a child process to execute the binary with the provided arguments
68
- spawn(`${__dirname}/${binaryName}`, process.argv.slice(2), {
103
+ spawnSync(`${__dirname}/${binaryName}`, process.argv.slice(2), {
69
104
  stdio: "inherit",
70
105
  });
106
+ process.exit(0);
71
107
  };
72
108
 
73
109
  main().catch(console.error);
110
+
111
+ //////////////////////
112
+
113
+ // const { spawn } = require("child_process");
114
+ // const fs = require("fs");
115
+ // const os = require("os");
116
+
117
+ // const PNAME = "aptos-cli";
118
+ // const VERSION = "2.0.2";
119
+
120
+ // // Get all arguments passed to the script
121
+ // const getOS = () => {
122
+ // const platform = os.platform();
123
+ // switch (platform) {
124
+ // case "darwin":
125
+ // return "MacOSX";
126
+ // case "linux":
127
+ // return "Ubuntu";
128
+ // case "win32":
129
+ // return "Windows";
130
+ // default:
131
+ // throw new Error(`Unsupported OS ${platform}`);
132
+ // }
133
+ // };
134
+
135
+ // const main = async () => {
136
+ // const os = getOS();
137
+ // const binaryName = `${PNAME}-${VERSION}-${os}`;
138
+
139
+ // // If binary does not exist, download it
140
+ // if (!fs.existsSync(`${__dirname}/${binaryName}`)) {
141
+ // await new Promise((resolve, reject) => {
142
+ // const url = `https://github.com/aptos-labs/aptos-core/releases/download/${PNAME}-v${VERSION}/${PNAME}-${VERSION}-${os}-x86_64.zip`;
143
+ // console.log("Downloading aptos CLI...");
144
+ // let downloadProcess;
145
+ // if (os === "Windows") {
146
+ // downloadProcess = spawn(
147
+ // `curl -o C:\\tmp\\aptos.zip ${url} && powershell -Command "Expand-Archive -Path C:\\tmp\\aptos.zip -DestinationPath C:\\tmp -Force" && move C:\\tmp\\aptos ${__dirname}\\${binaryName}`,
148
+ // {
149
+ // stdio: "inherit",
150
+ // shell: true,
151
+ // }
152
+ // );
153
+ // } else {
154
+ // downloadProcess = spawn(
155
+ // `curl -L -o /tmp/aptos.zip ${url}; unzip -o -q /tmp/aptos.zip -d /tmp; mv /tmp/aptos ${__dirname}/${binaryName};`,
156
+ // {
157
+ // stdio: "inherit",
158
+ // shell: true,
159
+ // }
160
+ // );
161
+ // }
162
+
163
+ // downloadProcess.on("close", (code) => {
164
+ // if (code !== 0) {
165
+ // reject("Error occurred while downloading aptos CLI");
166
+ // }
167
+ // console.log("Download complete");
168
+ // resolve();
169
+ // });
170
+ // });
171
+ // }
172
+
173
+ // // Spawn a child process to execute the binary with the provided arguments
174
+ // spawn(`${__dirname}/${binaryName}`, process.argv.slice(2), {
175
+ // stdio: "inherit",
176
+ // });
177
+ // };
178
+
179
+ // main().catch(console.error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aptos-labs/aptos-cli",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "Aptos CLI available from npmjs",
5
5
  "bin": {
6
6
  "aptos": "bin/aptos"