@offckb/cli 0.1.0-rc7 → 0.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.
package/dist/cmd/clean.js CHANGED
@@ -1,12 +1,16 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.clean = void 0;
4
7
  const const_1 = require("../cfg/const");
5
8
  const util_1 = require("../util");
9
+ const fs_1 = __importDefault(require("fs"));
6
10
  function clean() {
7
11
  if ((0, util_1.isFolderExists)(const_1.devnetDataPath)) {
8
12
  try {
9
- (0, util_1.removeFolderSync)(const_1.devnetDataPath);
13
+ fs_1.default.rmSync(const_1.devnetDataPath, { recursive: true });
10
14
  console.log(`Chain data cleaned.`);
11
15
  }
12
16
  catch (error) {
@@ -1 +1,15 @@
1
1
  export declare function installDependency(): Promise<void>;
2
+ export declare function downloadBinaryAndUnzip(): Promise<void>;
3
+ export declare function downloadAndSaveCKBBinary(tempFilePath: string): Promise<void>;
4
+ export declare function unZipFile(filePath: string, extractDir: string, useTar?: boolean): Promise<void>;
5
+ export declare function decompressTarGzAsync(tarballPath: string, destinationDir: string): Promise<void>;
6
+ export declare function getInstalledVersion(): string | null;
7
+ export declare function isVersionOutdated(installedVersion: string): boolean;
8
+ export declare function getOS(): string;
9
+ export declare function getArch(): string;
10
+ export declare function getExtension(): 'tar.gz' | 'zip';
11
+ export declare function buildDownloadUrl(version: string, opt?: {
12
+ os?: string;
13
+ arch?: string;
14
+ ext?: string;
15
+ }): string;
@@ -35,7 +35,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
35
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
36
36
  };
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.installDependency = void 0;
38
+ exports.buildDownloadUrl = exports.getExtension = exports.getArch = exports.getOS = exports.isVersionOutdated = exports.getInstalledVersion = exports.decompressTarGzAsync = exports.unZipFile = exports.downloadAndSaveCKBBinary = exports.downloadBinaryAndUnzip = exports.installDependency = void 0;
39
39
  const axios_1 = __importDefault(require("axios"));
40
40
  const child_process_1 = require("child_process");
41
41
  const fs = __importStar(require("fs"));
@@ -43,6 +43,7 @@ const path = __importStar(require("path"));
43
43
  const semver_1 = __importDefault(require("semver"));
44
44
  const os_1 = __importDefault(require("os"));
45
45
  const adm_zip_1 = __importDefault(require("adm-zip"));
46
+ const tar = __importStar(require("tar"));
46
47
  const const_1 = require("../cfg/const");
47
48
  const BINARY = const_1.ckbBinPath;
48
49
  const MINIMAL_VERSION = const_1.minimalRequiredCKBVersion;
@@ -53,7 +54,6 @@ function installDependency() {
53
54
  if (version) {
54
55
  if (isVersionOutdated(version)) {
55
56
  console.log(`${BINARY} version ${version} is outdated, download and install the new version ${MINIMAL_VERSION}..`);
56
- console.log(buildDownloadUrl(MINIMAL_VERSION));
57
57
  }
58
58
  else {
59
59
  return;
@@ -62,22 +62,24 @@ function installDependency() {
62
62
  else {
63
63
  console.log(`${BINARY} not found, download and install the new version ${MINIMAL_VERSION}..`);
64
64
  }
65
+ yield downloadBinaryAndUnzip();
66
+ });
67
+ }
68
+ exports.installDependency = installDependency;
69
+ function downloadBinaryAndUnzip() {
70
+ return __awaiter(this, void 0, void 0, function* () {
65
71
  const arch = getArch();
66
72
  const osname = getOS();
73
+ const ext = getExtension();
67
74
  const ckbVersionOSName = `ckb_v${MINIMAL_VERSION}_${arch}-${osname}`;
68
75
  try {
69
- const downloadURL = buildDownloadUrl(MINIMAL_VERSION);
70
- const response = yield axios_1.default.get(downloadURL, {
71
- responseType: 'arraybuffer',
72
- });
73
- const tempFilePath = path.join(os_1.default.tmpdir(), `${ckbVersionOSName}.zip`);
74
- fs.writeFileSync(tempFilePath, response.data);
76
+ const tempFilePath = path.join(os_1.default.tmpdir(), `${ckbVersionOSName}.${ext}`);
77
+ yield downloadAndSaveCKBBinary(tempFilePath);
75
78
  // Unzip the file
76
- const zip = new adm_zip_1.default(tempFilePath);
77
79
  const extractDir = path.join(const_1.targetEnvironmentPath, `ckb_v${MINIMAL_VERSION}`);
78
- zip.extractAllTo(extractDir, /*overwrite*/ true);
79
- const sourcePath = path.join(extractDir, ckbVersionOSName);
80
+ yield unZipFile(tempFilePath, extractDir, ext === 'tar.gz');
80
81
  // Install the extracted files
82
+ const sourcePath = path.join(extractDir, ckbVersionOSName);
81
83
  fs.renameSync(sourcePath, const_1.ckbFolderPath); // Move binary to desired location
82
84
  fs.chmodSync(const_1.ckbBinPath, '755'); // Make the binary executable
83
85
  console.log('CKB installed successfully.');
@@ -87,7 +89,53 @@ function installDependency() {
87
89
  }
88
90
  });
89
91
  }
90
- exports.installDependency = installDependency;
92
+ exports.downloadBinaryAndUnzip = downloadBinaryAndUnzip;
93
+ function downloadAndSaveCKBBinary(tempFilePath) {
94
+ return __awaiter(this, void 0, void 0, function* () {
95
+ const downloadURL = buildDownloadUrl(MINIMAL_VERSION);
96
+ const response = yield axios_1.default.get(downloadURL, {
97
+ responseType: 'arraybuffer',
98
+ });
99
+ fs.writeFileSync(tempFilePath, response.data);
100
+ });
101
+ }
102
+ exports.downloadAndSaveCKBBinary = downloadAndSaveCKBBinary;
103
+ function unZipFile(filePath, extractDir, useTar = false) {
104
+ return __awaiter(this, void 0, void 0, function* () {
105
+ // Ensure the destination directory exists, if not create it
106
+ if (!fs.existsSync(extractDir)) {
107
+ fs.mkdirSync(extractDir, { recursive: true });
108
+ }
109
+ if (useTar === true) {
110
+ return yield decompressTarGzAsync(filePath, extractDir);
111
+ }
112
+ const zip = new adm_zip_1.default(filePath);
113
+ zip.extractAllTo(extractDir, true);
114
+ });
115
+ }
116
+ exports.unZipFile = unZipFile;
117
+ function decompressTarGzAsync(tarballPath, destinationDir) {
118
+ return __awaiter(this, void 0, void 0, function* () {
119
+ return new Promise((resolve, reject) => {
120
+ // Create a readable stream from the .tar.gz file
121
+ const tarballStream = fs.createReadStream(tarballPath);
122
+ // Extract the contents of the .tar.gz file to the destination directory
123
+ tarballStream
124
+ .pipe(tar.x({
125
+ cwd: destinationDir,
126
+ }))
127
+ .on('error', (err) => {
128
+ console.error('Error extracting tarball:', err);
129
+ reject(err); // Reject with error if extraction fails
130
+ })
131
+ .on('finish', () => {
132
+ console.log('Extraction complete.');
133
+ resolve(); // Resolve when extraction completes
134
+ });
135
+ });
136
+ });
137
+ }
138
+ exports.decompressTarGzAsync = decompressTarGzAsync;
91
139
  function getInstalledVersion() {
92
140
  try {
93
141
  const versionOutput = (0, child_process_1.execSync)(`${BINARY} --version`, {
@@ -103,9 +151,11 @@ function getInstalledVersion() {
103
151
  return null;
104
152
  }
105
153
  }
154
+ exports.getInstalledVersion = getInstalledVersion;
106
155
  function isVersionOutdated(installedVersion) {
107
156
  return semver_1.default.lt(installedVersion, MINIMAL_VERSION);
108
157
  }
158
+ exports.isVersionOutdated = isVersionOutdated;
109
159
  function getOS() {
110
160
  const platform = os_1.default.platform();
111
161
  if (platform === 'darwin') {
@@ -121,6 +171,7 @@ function getOS() {
121
171
  throw new Error('Unsupported operating system');
122
172
  }
123
173
  }
174
+ exports.getOS = getOS;
124
175
  function getArch() {
125
176
  const arch = os_1.default.arch();
126
177
  if (arch === 'x64') {
@@ -133,8 +184,19 @@ function getArch() {
133
184
  throw new Error('Unsupported architecture');
134
185
  }
135
186
  }
136
- function buildDownloadUrl(version) {
137
- const os = getOS();
138
- const arch = getArch();
139
- return `https://github.com/nervosnetwork/ckb/releases/download/v${version}/ckb_v${version}_${arch}-${os}.zip`;
187
+ exports.getArch = getArch;
188
+ function getExtension() {
189
+ const platform = os_1.default.platform();
190
+ if (platform === 'linux') {
191
+ return 'tar.gz';
192
+ }
193
+ return 'zip';
194
+ }
195
+ exports.getExtension = getExtension;
196
+ function buildDownloadUrl(version, opt = {}) {
197
+ const os = opt.os || getOS();
198
+ const arch = opt.arch || getArch();
199
+ const extension = opt.ext || getExtension();
200
+ return `https://github.com/nervosnetwork/ckb/releases/download/v${version}/ckb_v${version}_${arch}-${os}.${extension}`;
140
201
  }
202
+ exports.buildDownloadUrl = buildDownloadUrl;
package/dist/util.d.ts CHANGED
@@ -3,8 +3,8 @@ export declare function copyFolderSync(source: string, destination: string): voi
3
3
  export declare function copyFileSync(source: string, target: string): void;
4
4
  export declare function copyFilesWithExclusion(sourceDir: string, destinationDir: string, excludedFolders: string[]): Promise<void>;
5
5
  export declare function copyRecursive(source: string, destination: string, excludedFolders: string[]): Promise<void>;
6
- export declare function removeFolderSync(folderPath: string): void;
7
- export declare function gitCloneAndDownloadFolderSync(repoUrl: string, branch: string, subFolderName: string, targetPath: string): void;
6
+ export declare function isGitInstalled(): boolean;
7
+ export declare function gitCloneAndDownloadFolderSync(repoUrl: string, branch: string, subFolderName: string, targetPath: string): undefined;
8
8
  export interface TemplateOption {
9
9
  name: string;
10
10
  value: string;
package/dist/util.js CHANGED
@@ -35,7 +35,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
35
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
36
36
  };
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.loadTemplateOpts = exports.gitCloneAndDownloadFolderSync = exports.removeFolderSync = exports.copyRecursive = exports.copyFilesWithExclusion = exports.copyFileSync = exports.copyFolderSync = exports.isFolderExists = void 0;
38
+ exports.loadTemplateOpts = exports.gitCloneAndDownloadFolderSync = exports.isGitInstalled = exports.copyRecursive = exports.copyFilesWithExclusion = exports.copyFileSync = exports.copyFolderSync = exports.isFolderExists = void 0;
39
39
  const child_process_1 = require("child_process");
40
40
  const fs = __importStar(require("fs"));
41
41
  const path = __importStar(require("path"));
@@ -58,7 +58,7 @@ function isFolderExists(folderPath) {
58
58
  exports.isFolderExists = isFolderExists;
59
59
  function copyFolderSync(source, destination) {
60
60
  if (!fs.existsSync(destination)) {
61
- fs.mkdirSync(destination);
61
+ fs.mkdirSync(destination, { recursive: true });
62
62
  }
63
63
  const files = fs.readdirSync(source);
64
64
  for (const file of files) {
@@ -128,41 +128,47 @@ function copyRecursive(source, destination, excludedFolders) {
128
128
  });
129
129
  }
130
130
  exports.copyRecursive = copyRecursive;
131
- function removeFolderSync(folderPath) {
132
- if (fs.existsSync(folderPath)) {
133
- fs.readdirSync(folderPath).forEach((file) => {
134
- const curPath = path.join(folderPath, file);
135
- if (fs.lstatSync(curPath).isDirectory()) {
136
- // Recursive call for directories
137
- removeFolderSync(curPath);
138
- }
139
- else {
140
- // Delete files
141
- fs.unlinkSync(curPath);
142
- }
143
- });
144
- // Remove the directory itself
145
- fs.rmdirSync(folderPath);
131
+ function isGitInstalled() {
132
+ try {
133
+ (0, child_process_1.execSync)('git --version');
134
+ return true;
135
+ }
136
+ catch (error) {
137
+ return false;
146
138
  }
147
139
  }
148
- exports.removeFolderSync = removeFolderSync;
140
+ exports.isGitInstalled = isGitInstalled;
149
141
  function gitCloneAndDownloadFolderSync(repoUrl, branch, subFolderName, targetPath) {
150
142
  console.log('start cloning the dapp template..');
151
143
  const tempFolder = path.resolve(const_1.dappTemplatePath, 'temp-clone-folder');
144
+ if (!isGitInstalled()) {
145
+ console.log('Git is not installed, please check https://git-scm.com/');
146
+ return process.exit(1);
147
+ }
152
148
  // Empty the temp folder if it exists
153
149
  if (fs.existsSync(tempFolder)) {
154
- fs.rmdirSync(tempFolder, { recursive: true });
150
+ fs.rmSync(tempFolder, { recursive: true });
155
151
  }
156
152
  // Create the temp folder
157
153
  fs.mkdirSync(tempFolder, { recursive: true });
158
- const cloneCommand = `
159
- git clone -n --depth=1 --single-branch --branch ${branch} --filter=tree:0 \
160
- ${repoUrl} ${tempFolder}
161
- cd ${tempFolder}
162
- git sparse-checkout set ${subFolderName}
163
- git checkout
164
- `;
165
- (0, child_process_1.execSync)(cloneCommand);
154
+ // Clone the repository
155
+ try {
156
+ const cloneCommand = `git clone -n --depth=1 --single-branch --branch ${branch} --filter=tree:0 ${repoUrl} ${tempFolder}`;
157
+ (0, child_process_1.execSync)(cloneCommand);
158
+ }
159
+ catch (error) {
160
+ console.error('Error:', error);
161
+ process.exit(1);
162
+ }
163
+ // checkout the examples sub folder
164
+ try {
165
+ (0, child_process_1.execSync)(`git sparse-checkout set ${subFolderName}`, { cwd: tempFolder });
166
+ (0, child_process_1.execSync)(`git checkout`, { cwd: tempFolder });
167
+ }
168
+ catch (error) {
169
+ console.error('Error:', error);
170
+ process.exit(1);
171
+ }
166
172
  // Ensure targetPath exists and is a directory
167
173
  if (!fs.existsSync(targetPath) || !fs.statSync(targetPath).isDirectory()) {
168
174
  fs.mkdirSync(targetPath, { recursive: true });
@@ -171,7 +177,7 @@ git checkout
171
177
  copyFolderSync(source, targetPath);
172
178
  // Empty the temp folder if it exists
173
179
  if (fs.existsSync(tempFolder)) {
174
- fs.rmdirSync(tempFolder, { recursive: true });
180
+ fs.rmSync(tempFolder, { recursive: true });
175
181
  }
176
182
  console.log(`Folder ${subFolderName} downloaded successfully from ${repoUrl} and moved to ${targetPath}`);
177
183
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@offckb/cli",
3
- "version": "0.1.0-rc7",
3
+ "version": "0.1.0",
4
4
  "description": "ckb development network for your first try",
5
5
  "author": "Retric Su <retric@cryptape.com>",
6
6
  "license": "MIT",
@@ -48,6 +48,7 @@
48
48
  "@types/adm-zip": "^0.5.5",
49
49
  "@types/node": "^20.11.19",
50
50
  "@types/semver": "^7.5.7",
51
+ "@types/tar": "^6.1.11",
51
52
  "@typescript-eslint/eslint-plugin": "^7.0.2",
52
53
  "@typescript-eslint/parser": "^7.0.2",
53
54
  "eslint": "^8.57.0",
@@ -64,6 +65,7 @@
64
65
  "axios": "^1.6.7",
65
66
  "child_process": "^1.0.2",
66
67
  "commander": "^12.0.0",
67
- "semver": "^7.6.0"
68
+ "semver": "^7.6.0",
69
+ "tar": "^6.2.1"
68
70
  }
69
71
  }