@aptos-labs/aptos-cli 0.0.10 → 0.0.11

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 +36 -146
  2. package/package.json +1 -1
package/bin/aptos CHANGED
@@ -1,10 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const https = require("https");
3
+ const { spawn } = require("child_process");
4
4
  const fs = require("fs");
5
5
  const os = require("os");
6
- const path = require("path");
7
- const { spawn, spawnSync } = require("child_process");
6
+
7
+ const PNAME = "aptos-cli";
8
+ const VERSION = "2.0.2";
8
9
 
9
10
  // Get all arguments passed to the script
10
11
  const getOS = () => {
@@ -21,159 +22,48 @@ const getOS = () => {
21
22
  }
22
23
  };
23
24
 
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`;
28
-
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
- }
25
+ const main = async () => {
26
+ const os = getOS();
27
+ const binaryName = `${PNAME}-${VERSION}-${os}`;
44
28
 
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)
29
+ // If binary does not exist, download it
30
+ if (!fs.existsSync(`${__dirname}/${binaryName}`)) {
31
+ await new Promise((resolve, reject) => {
32
+ const url = `https://github.com/aptos-labs/aptos-core/releases/download/${PNAME}-v${VERSION}/${PNAME}-${VERSION}-${os}-x86_64.zip`;
33
+ console.log("Downloading aptos CLI...");
34
+ let downloadProcess;
35
+ if (os === "Windows") {
36
+ downloadProcess = spawn(
37
+ `curl -L -o C:/tmp/aptos.zip ${url} & powershell Expand-Archive -Path C:/tmp/aptos.zip -DestinationPath C:/tmp -Force & move C:/tmp/aptos ${__dirname}/${binaryName}`,
38
+ {
39
+ stdio: "inherit",
40
+ shell: true,
41
+ }
55
42
  );
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
- });
88
43
  } else {
89
- console.error(
90
- `Failed to download file: ${response.statusCode} ${response.statusMessage}`
44
+ downloadProcess = spawn(
45
+ `curl -L -o /tmp/aptos.zip ${url}; unzip -o -q /tmp/aptos.zip -d /tmp; mv /tmp/aptos ${__dirname}/${binaryName};`,
46
+ {
47
+ stdio: "inherit",
48
+ shell: true,
49
+ }
91
50
  );
92
- reject("Error occurred while downloading aptos CLI");
93
51
  }
94
- });
95
- });
96
- }
97
52
 
98
- const main = async () => {
99
- if (!fs.existsSync(`${__dirname}/${binaryName}`)) {
100
- await downloadBinary(url);
53
+ downloadProcess.on("close", (code) => {
54
+ if (code !== 0) {
55
+ reject("Error occurred while downloading aptos CLI");
56
+ }
57
+ console.log("Download complete");
58
+ resolve();
59
+ });
60
+ });
101
61
  }
62
+
102
63
  // Spawn a child process to execute the binary with the provided arguments
103
- spawnSync(`${__dirname}/${binaryName}`, process.argv.slice(2), {
64
+ spawn(`${__dirname}/${binaryName}`, process.argv.slice(2), {
104
65
  stdio: "inherit",
105
66
  });
106
- process.exit(0);
107
67
  };
108
68
 
109
69
  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.10",
3
+ "version": "0.0.11",
4
4
  "description": "Aptos CLI available from npmjs",
5
5
  "bin": {
6
6
  "aptos": "bin/aptos"