Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ program
.command('deploy')
.description('Deploy contracts to different networks, only supports devnet and testnet')
.option('--network <network>', 'Specify the network to deploy to', 'devnet')
.option('--target <target>', 'Specify the relative bin target folder to deploy to')
.option('--target <target>', 'Specify the script binaries file/folder path to deploy')
.option('--config <config>', 'Specify the offckb.config.ts file path for deployment', undefined)
.option('-t, --type-id', 'Specify if use upgradable type id to deploy the script')
.option('--privkey <privkey>', 'Specify the private key to deploy scripts')
Expand Down
7 changes: 3 additions & 4 deletions src/cmd/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NetworkOption, Network } from '../type/base';
import path from 'path';
import { deployerAccount } from '../cfg/account';
import { listBinaryFilesInFolder, isAbsolutePath } from '../util/fs';
import { isAbsolutePath, getBinaryFilesFromPath } from '../util/fs';
import { validateNetworkOpt } from '../util/validator';
import { deployBinaries, getToDeployBinsPath, recordDeployResult } from '../deploy';
import { CKB } from '../sdk/ckb';
Expand Down Expand Up @@ -30,9 +30,8 @@ export async function deploy(
const configPath = opt.config;
const targetFolder = opt.target;
if (targetFolder) {
const binFolder = isAbsolutePath(targetFolder) ? targetFolder : path.resolve(process.cwd(), targetFolder);
const bins = listBinaryFilesInFolder(binFolder);
const binPaths = bins.map((bin) => path.resolve(binFolder, bin));
const binFilesOrFolder = isAbsolutePath(targetFolder) ? targetFolder : path.resolve(process.cwd(), targetFolder);
const binPaths = getBinaryFilesFromPath(binFilesOrFolder);
const results = await deployBinaries(binPaths, privateKey, enableTypeId, ckb);

// record the deployed contract infos
Expand Down
9 changes: 5 additions & 4 deletions src/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DeploymentOptions, generateDeploymentToml } from '../deploy/toml';
import { DeploymentRecipe, generateDeploymentMigrationFile, Migration } from '../deploy/migration';
import { genMyScriptsJsonFile } from '../scripts/gen';
import { OffCKBConfigFile } from '../template/offckb-config';
import { listBinaryFilesInFolder, readFileToUint8Array, isAbsolutePath } from '../util/fs';
import { readFileToUint8Array, isAbsolutePath, getBinaryFilesFromPath } from '../util/fs';
import path from 'path';
import fs from 'fs';
import { Network } from '../type/base';
Expand All @@ -19,11 +19,12 @@ export function getToDeployBinsPath(userOffCKBConfigPath: string) {
const match = fileContent.match(/contractBinFolder:\s*['"]([^'"]+)['"]/);
if (match && match[1]) {
const contractBinFolderValue = match[1];
const binFolderPath = isAbsolutePath(contractBinFolderValue)
const binFileOrFolderPath = isAbsolutePath(contractBinFolderValue)
? contractBinFolderValue
: path.resolve(userOffCKBConfigPath, contractBinFolderValue);
const bins = listBinaryFilesInFolder(binFolderPath);
return bins.map((bin) => path.resolve(binFolderPath, bin));

const bins = getBinaryFilesFromPath(binFileOrFolderPath);
return bins;
} else {
console.log('contractBinFolder value not found in offckb.config.ts');
return [];
Expand Down
25 changes: 18 additions & 7 deletions src/util/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,33 @@ export async function readFileToUint8Array(filePath: string): Promise<Uint8Array
});
}

export function getBinaryFilesFromPath(fileOrFolderPath: string): string[] {
if (!fs.existsSync(fileOrFolderPath)) {
throw new Error(`File or Folder not exits ${fileOrFolderPath}`);
}
// Check if the provided path is a directory
if (fs.lstatSync(fileOrFolderPath).isDirectory()) {
return listBinaryFilesInFolder(fileOrFolderPath);
}

if (fs.lstatSync(fileOrFolderPath).isFile() && isBinaryFile(fileOrFolderPath)) {
return [fileOrFolderPath];
}

throw new Error(`${fileOrFolderPath} is not a valid path to deploy scripts.`);
}

export function listBinaryFilesInFolder(folderPath: string): string[] {
// Check if the provided path is a directory
if (!fs.existsSync(folderPath) || !fs.lstatSync(folderPath).isDirectory()) {
throw new Error(`${folderPath} is not a valid directory.`);
}

// Read the contents of the directory
const files = fs.readdirSync(folderPath);
const files = fs.readdirSync(folderPath).map((f) => path.join(folderPath, f));

// Filter out only the binary files (assuming they have extensions like .exe, .bin, .dll, etc.)
const binaryFiles = files.filter((file) => {
const filePath = path.join(folderPath, file);
// Check if the file is a regular file and not a directory
return fs.statSync(filePath).isFile() && isBinaryFile(filePath);
});

const binaryFiles = files.filter((file) => fs.statSync(file).isFile() && isBinaryFile(file));
return binaryFiles;
}

Expand Down