Skip to content

Commit

Permalink
refactor, add specific command flags
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmaayan committed Sep 26, 2024
1 parent 9ecdb55 commit 88e5c78
Show file tree
Hide file tree
Showing 20 changed files with 372 additions and 210 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/run-bin-script-on-mac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ jobs:
uses: actions/checkout@v3

- name: Run the bin script on Mac
run: node bin/aptos
run: |
npm install
npm run build
node ./dist/aptos.js
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bin/aptos-cli*
package-lock.json
node_modules
node_modules
dist
19 changes: 15 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
# Contributing

## Testing
To test changes to the script, do this:
Clone this repo:

```
git clone [email protected]:aptos-labs/aptos-cli.git
```

Install dependencies:

```
npm install
```

Run the bin file

```
pnpm install
./bin/aptos
npm run dev
```
201 changes: 0 additions & 201 deletions bin/aptos

This file was deleted.

32 changes: 32 additions & 0 deletions bin/aptos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env node

// On MacOS we install the CLI with brew. There are two main reasons for this:
// 1. Brew builds the CLI for the native CPU architecture for the user, which
// eliminates any issues arising from using x86 binaries on ARM machines.
// 2. Brew handles dependency management for us. This isn't relevant right now but
// might become necessary later if we reintroduce OpenSSL as a dep for the CLI.
//
// On Linux and Windows we just query the GH API for the latest CLI release and
// download and extract that.

import { program } from "commander";

import { parseCommandOptions } from "./utils/parseCommandOptions.js";

program
.name("")
.description("")
.option("-i, --install", "install the latest version of the CLI")
.option("-u, --update", "update the CLI to the latest version");

program.parse();

const main = async () => {
const options = {
install: program.opts().install,
update: program.opts().update,
};
await parseCommandOptions(options);
};

main().catch(console.error);
56 changes: 56 additions & 0 deletions bin/tasks/install.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { execSync } from "child_process";
import { existsSync } from "fs";

import { getCliPathBrew } from "../utils/brewOperations.js";
import { GH_CLI_DOWNLOAD_URL, PNAME } from "../utils/consts.js";
import { execSyncShell } from "../utils/execSyncShell.js";
import { getCurrentOpenSSLVersion } from "../utils/versions.js";
import { getOS } from "../utils/getUserOs.js";
import { getLatestVersion } from "../utils/getAptosCliLatestVersion.js";
import { getLocalBinPath } from "../utils/getLocalBinPath.js";

// Install the CLI.
export const installCli = async () => {
const path = getLocalBinPath();
if (existsSync(path)) {
console.log("Aptos CLI is already installed");
return;
}
// Look up the latest version.
const latestCLIVersion = await getLatestVersion();
console.log(`Downloading aptos CLI version ${latestCLIVersion}`);
const os = getOS();

if (os === "Windows") {
const url = `${GH_CLI_DOWNLOAD_URL}/${PNAME}-v${latestCLIVersion}/${PNAME}-${latestCLIVersion}-${os}-x86_64.zip`;
// Download the zip file, extract it, and move the binary to the correct location.
execSync(
`powershell -Command "if (!(Test-Path -Path 'C:\\tmp')) { New-Item -ItemType Directory -Path 'C:\\tmp' } ; Invoke-RestMethod -Uri ${url} -OutFile C:\\tmp\\aptos.zip; Expand-Archive -Path C:\\tmp\\aptos.zip -DestinationPath C:\\tmp -Force; Move-Item -Path C:\\tmp\\aptos.exe -Destination ${path}"`
);
} else if (os === "MacOS") {
// Install the CLI with brew.
execSyncShell("brew install aptos", { encoding: "utf8" });
} else {
// On Linux, we check what version of OpenSSL we're working with to figure out
// which binary to download.
let osVersion = "x86_64";
let opensSslVersion = "1.0.0";
try {
opensSslVersion = getCurrentOpenSSLVersion();
} catch (error) {
console.log(
"Could not determine OpenSSL version, assuming older version (1.x.x)"
);
}

if (opensSslVersion.startsWith("3.")) {
osVersion = "22.04-x86_64";
}
console.log(`Downloading CLI binary ${os}-${osVersion}`);
const url = `${GH_CLI_DOWNLOAD_URL}/${PNAME}-v${latestCLIVersion}/${PNAME}-${latestCLIVersion}-${os}-${osVersion}.zip`;
// Download the zip file, extract it, and move the binary to the correct location.
execSync(
`curl -L -o /tmp/aptos.zip ${url}; unzip -o -q /tmp/aptos.zip -d /tmp; mv /tmp/aptos ${path};`
);
}
};
28 changes: 28 additions & 0 deletions bin/tasks/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { spawn } from "child_process";
import { existsSync } from "fs";

import { getOS } from "../utils/getUserOs.js";
import { getLocalBinPath } from "../utils/getLocalBinPath.js";

export const runCLI = async () => {
const path = getLocalBinPath();
if (!existsSync(path)) {
console.log(
"Aptos CLI not installed, run `npx aptos --install` to install"
);
return;
}
const os = getOS();

// Spawn a child process to execute the binary with the provided arguments.
if (os === "Windows") {
spawn(path, process.argv.slice(2), {
stdio: "inherit",
shell: true,
});
} else {
spawn(path, process.argv.slice(2), {
stdio: "inherit",
});
}
};
33 changes: 33 additions & 0 deletions bin/tasks/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { existsSync } from "fs";

import { execSyncShell } from "../utils/execSyncShell.js";
import { getLatestVersion } from "../utils/getAptosCliLatestVersion.js";
import { installCli } from "./install.js";
import { getLocalBinPath } from "../utils/getLocalBinPath.js";

export const updateCli = async () => {
const path = getLocalBinPath();
if (!existsSync(path)) {
console.log(
"Aptos CLI not installed, run `npx aptos --install` to install"
);
return;
}
// Look up the latest version.
const latestVersion = await getLatestVersion();
// Get the current version of the CLI.
const currentVersion = execSyncShell(`${path} --version`, {
encoding: "utf8",
})
.trim()
.split(" ")[1];
// Check if the installed version is the latest version.
if (currentVersion !== latestVersion) {
console.log(
`A newer version of the CLI is available: ${latestVersion}, installing...`
);
installCli();
} else {
console.log(`CLI is up to date`);
}
};
Loading

0 comments on commit 88e5c78

Please sign in to comment.