Skip to content

Commit

Permalink
fix: make offckb node/list-hashes a one step cmd (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
RetricSu authored Feb 26, 2024
1 parent 1019cef commit 628f96d
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 14 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@

ckb development environment for professionals

## Install

```sh
git clone https://github.com/RetricSu/offckb.git
cd offckb && alias offckb='yarn start'
```

eventually you will do simple
```sh
npm install -g offckb // not yet, todo
```

## Usage

```sh
offckb node // start the devnet of CKB
offckb init // init a typescript boilerplate with lumos to get started with to build CKB DAPP,think 'hardhat init'
offckb list-hashes // list scripts hashes, equals `ckb list-hashes`
offckb list-accounts // todo, list accounts with prefund CKB tokens
```

### Built-in scripts

- [x] xUDT https://github.com/nervosnetwork/rfcs/pull/428
Expand Down
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { installDependency } from "./cmd/install";
import { genkey } from "./cmd/genkey";
import { listHashes } from "./cmd/list-hashes";
import { node } from "./cmd/node";
import { initChain } from "./cmd/init-chain";
import { initChainIfNeeded } from "./cmd/init-chain";
import { buildLumosConfig } from "./cmd/build-lumos-config";
import { init } from "./cmd/init";

Expand Down Expand Up @@ -35,7 +35,7 @@ program.command("node").description("Use the CKB to start devnet").action(node);
program
.command("init-chain")
.description("Use the CKB to init devnet")
.action(initChain);
.action(initChainIfNeeded);

program
.command("build-lumos-config")
Expand Down
9 changes: 8 additions & 1 deletion src/cmd/init-chain.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import * as fs from "fs";
import { devnetPath, devnetSourcePath } from "../cfg/const";
import path from "path";
import { isFolderExists } from "../util";

export async function initChain() {
export async function initChainIfNeeded() {
if (!isFolderExists(devnetPath)) {
await doInitChain();
}
}

async function doInitChain() {
await copyFilesWithExclusion(devnetSourcePath, devnetPath, ["data"]);
console.debug(`init devnet config folder: ${devnetPath}`);
copyAndEditMinerToml();
Expand Down
30 changes: 21 additions & 9 deletions src/cmd/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import * as path from "path";
import semver from "semver";
import os from "os";
import AdmZip from "adm-zip";
import { ckbBinPath, ckbFolderPath, minimalRequiredCKBVersion, targetEnvironmentPath } from "../cfg/const";
import {
ckbBinPath,
ckbFolderPath,
minimalRequiredCKBVersion,
targetEnvironmentPath,
} from "../cfg/const";

const BINARY = ckbBinPath;
const MINIMAL_VERSION = minimalRequiredCKBVersion;
Expand All @@ -15,27 +20,36 @@ export async function installDependency() {
const version = getInstalledVersion();
if (version) {
if (isVersionOutdated(version)) {
console.log(`${BINARY} version ${version} is outdated, download and install the new version ${MINIMAL_VERSION}..`);
console.log(buildDownloadUrl(MINIMAL_VERSION))
console.log(
`${BINARY} version ${version} is outdated, download and install the new version ${MINIMAL_VERSION}..`
);
console.log(buildDownloadUrl(MINIMAL_VERSION));
} else {
return;
}
} else {
console.log(`${BINARY} not found, download and install the new version ${MINIMAL_VERSION}..`);
console.log(
`${BINARY} not found, download and install the new version ${MINIMAL_VERSION}..`
);
}

const arch = getArch();
const osname = getOS();
const ckbVersionOSName = `ckb_v${MINIMAL_VERSION}_${arch}-${osname}`;
const ckbVersionOSName = `ckb_v${MINIMAL_VERSION}_${arch}-${osname}`;
try {
const downloadURL = buildDownloadUrl(MINIMAL_VERSION);
const response = await axios.get(downloadURL, { responseType: "arraybuffer" });
const response = await axios.get(downloadURL, {
responseType: "arraybuffer",
});
const tempFilePath = path.join(os.tmpdir(), `${ckbVersionOSName}.zip`);
fs.writeFileSync(tempFilePath, response.data);

// Unzip the file
const zip = new AdmZip(tempFilePath);
const extractDir = path.join(targetEnvironmentPath, `ckb_v${MINIMAL_VERSION}`);
const extractDir = path.join(
targetEnvironmentPath,
`ckb_v${MINIMAL_VERSION}`
);
zip.extractAllTo(extractDir, /*overwrite*/ true);
const sourcePath = path.join(extractDir, ckbVersionOSName);

Expand All @@ -49,7 +63,6 @@ export async function installDependency() {
}
}


function getInstalledVersion(): string | null {
try {
const versionOutput = execSync(`${BINARY} --version`, {
Expand All @@ -62,7 +75,6 @@ function getInstalledVersion(): string | null {
}
return null;
} catch (error) {
console.error("Error retrieving installed version:", error);
return null;
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/cmd/list-hashes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { execSync } from "child_process";
import { ckbBinPath, devnetPath } from "../cfg/const";
import { installDependency } from "./install";
import { initChainIfNeeded } from "./init-chain";

export async function listHashes() {
await installDependency();
await initChainIfNeeded();

export function listHashes() {
const cmd = `${ckbBinPath} list-hashes -C ${devnetPath}`;
try {
execSync(cmd, {
Expand Down
7 changes: 6 additions & 1 deletion src/cmd/node.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { exec } from "child_process";
import { ckbBinPath, devnetPath } from "../cfg/const";
import { initChainIfNeeded } from "./init-chain";
import { installDependency } from "./install";

export async function node() {
await installDependency();
await initChainIfNeeded();

export function node() {
const ckbCmd = `${ckbBinPath} run -C ${devnetPath}`;
const minerCmd = `${ckbBinPath} miner -C ${devnetPath}`;
try {
Expand Down
15 changes: 15 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as fs from "fs";

export function isFolderExists(folderPath: string): boolean {
try {
// Check if the path exists
fs.accessSync(folderPath, fs.constants.F_OK);

// Check if it's a directory
const stats = fs.statSync(folderPath);
return stats.isDirectory();
} catch (error) {
// If the access or stat fails, or if it's not a directory, return false
return false;
}
}

0 comments on commit 628f96d

Please sign in to comment.