Skip to content

Commit edfc5ce

Browse files
authored
Merge pull request #189 from ckb-devrel/v0.3.x
refactor(deploy): allow to pass bin file path as --target (#188)
2 parents d10b429 + 1f8bab4 commit edfc5ce

4 files changed

Lines changed: 27 additions & 16 deletions

File tree

src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ program
125125
.command('deploy')
126126
.description('Deploy contracts to different networks, only supports devnet and testnet')
127127
.option('--network <network>', 'Specify the network to deploy to', 'devnet')
128-
.option('--target <target>', 'Specify the relative bin target folder to deploy to')
128+
.option('--target <target>', 'Specify the script binaries file/folder path to deploy')
129129
.option('--config <config>', 'Specify the offckb.config.ts file path for deployment', undefined)
130130
.option('-t, --type-id', 'Specify if use upgradable type id to deploy the script')
131131
.option('--privkey <privkey>', 'Specify the private key to deploy scripts')

src/cmd/deploy.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NetworkOption, Network } from '../type/base';
22
import path from 'path';
33
import { deployerAccount } from '../cfg/account';
4-
import { listBinaryFilesInFolder, isAbsolutePath } from '../util/fs';
4+
import { isAbsolutePath, getBinaryFilesFromPath } from '../util/fs';
55
import { validateNetworkOpt } from '../util/validator';
66
import { deployBinaries, getToDeployBinsPath, recordDeployResult } from '../deploy';
77
import { CKB } from '../sdk/ckb';
@@ -30,9 +30,8 @@ export async function deploy(
3030
const configPath = opt.config;
3131
const targetFolder = opt.target;
3232
if (targetFolder) {
33-
const binFolder = isAbsolutePath(targetFolder) ? targetFolder : path.resolve(process.cwd(), targetFolder);
34-
const bins = listBinaryFilesInFolder(binFolder);
35-
const binPaths = bins.map((bin) => path.resolve(binFolder, bin));
33+
const binFilesOrFolder = isAbsolutePath(targetFolder) ? targetFolder : path.resolve(process.cwd(), targetFolder);
34+
const binPaths = getBinaryFilesFromPath(binFilesOrFolder);
3635
const results = await deployBinaries(binPaths, privateKey, enableTypeId, ckb);
3736

3837
// record the deployed contract infos

src/deploy/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { DeploymentOptions, generateDeploymentToml } from '../deploy/toml';
22
import { DeploymentRecipe, generateDeploymentMigrationFile, Migration } from '../deploy/migration';
33
import { genMyScriptsJsonFile } from '../scripts/gen';
44
import { OffCKBConfigFile } from '../template/offckb-config';
5-
import { listBinaryFilesInFolder, readFileToUint8Array, isAbsolutePath } from '../util/fs';
5+
import { readFileToUint8Array, isAbsolutePath, getBinaryFilesFromPath } from '../util/fs';
66
import path from 'path';
77
import fs from 'fs';
88
import { Network } from '../type/base';
@@ -19,11 +19,12 @@ export function getToDeployBinsPath(userOffCKBConfigPath: string) {
1919
const match = fileContent.match(/contractBinFolder:\s*['"]([^'"]+)['"]/);
2020
if (match && match[1]) {
2121
const contractBinFolderValue = match[1];
22-
const binFolderPath = isAbsolutePath(contractBinFolderValue)
22+
const binFileOrFolderPath = isAbsolutePath(contractBinFolderValue)
2323
? contractBinFolderValue
2424
: path.resolve(userOffCKBConfigPath, contractBinFolderValue);
25-
const bins = listBinaryFilesInFolder(binFolderPath);
26-
return bins.map((bin) => path.resolve(binFolderPath, bin));
25+
26+
const bins = getBinaryFilesFromPath(binFileOrFolderPath);
27+
return bins;
2728
} else {
2829
console.log('contractBinFolder value not found in offckb.config.ts');
2930
return [];

src/util/fs.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,33 @@ export async function readFileToUint8Array(filePath: string): Promise<Uint8Array
100100
});
101101
}
102102

103+
export function getBinaryFilesFromPath(fileOrFolderPath: string): string[] {
104+
if (!fs.existsSync(fileOrFolderPath)) {
105+
throw new Error(`File or Folder not exits ${fileOrFolderPath}`);
106+
}
107+
// Check if the provided path is a directory
108+
if (fs.lstatSync(fileOrFolderPath).isDirectory()) {
109+
return listBinaryFilesInFolder(fileOrFolderPath);
110+
}
111+
112+
if (fs.lstatSync(fileOrFolderPath).isFile() && isBinaryFile(fileOrFolderPath)) {
113+
return [fileOrFolderPath];
114+
}
115+
116+
throw new Error(`${fileOrFolderPath} is not a valid path to deploy scripts.`);
117+
}
118+
103119
export function listBinaryFilesInFolder(folderPath: string): string[] {
104120
// Check if the provided path is a directory
105121
if (!fs.existsSync(folderPath) || !fs.lstatSync(folderPath).isDirectory()) {
106122
throw new Error(`${folderPath} is not a valid directory.`);
107123
}
108124

109125
// Read the contents of the directory
110-
const files = fs.readdirSync(folderPath);
126+
const files = fs.readdirSync(folderPath).map((f) => path.join(folderPath, f));
111127

112128
// Filter out only the binary files (assuming they have extensions like .exe, .bin, .dll, etc.)
113-
const binaryFiles = files.filter((file) => {
114-
const filePath = path.join(folderPath, file);
115-
// Check if the file is a regular file and not a directory
116-
return fs.statSync(filePath).isFile() && isBinaryFile(filePath);
117-
});
118-
129+
const binaryFiles = files.filter((file) => fs.statSync(file).isFile() && isBinaryFile(file));
119130
return binaryFiles;
120131
}
121132

0 commit comments

Comments
 (0)