@layerzerolabs/evm-sdks-build
The EVM SDKs Build Tools package provides a set of utilities and tools for building and managing EVM-based SDKs. It includes functions for copying files, populating directories, and managing concurrency with semaphores.
Features
- File Management: Copy files from source packages to target directories.
- Directory Population: Populate directories with specified files.
- Concurrency Control: Manage concurrent operations with semaphores.
Installation
To install the EVM SDKs Build Tools package, you can use npm or yarn:
npm install @layerzerolabs/evm-sdks-build
or
yarn add @layerzerolabs/evm-sdks-build
Usage
File Management
Asynchronously copies files from source packages to a target root directory based on the provided directory structure.
- packageFiles: An object mapping package names to their directory structures.
- callerRootFile: The absolute path to the root file of the caller.
- Returns: A promise that resolves when the operation is complete.
import { copyPackageFiles, PackageFiles } from "@layerzerolabs/evm-sdks-build";
const packageFiles: PackageFiles = {
"@layerzerolabs/package1": ["file1.js", "file2.js"],
"@layerzerolabs/package2": {
dir1: ["file3.js"],
dir2: ["file4.js", "file5.js"],
},
};
const callerRootFile = __filename;
copyPackageFiles(packageFiles, callerRootFile).then(() => {
console.log("Files copied successfully.");
});
Directory Population
Populates the target directory with files from the specified source packages.
- copyTargets: An object mapping source packages to their respective file patterns.
- callerRootFile: The absolute path to the root file of the caller.
- Returns: A promise that resolves when the operation is complete.
import { populate, CopyTargets } from "@layerzerolabs/evm-sdks-build";
const copyTargets: CopyTargets = {
"@layerzerolabs/package1": {
artifacts: ["**/*.json"],
deployments: ["**/*.json"],
},
"@layerzerolabs/package2": {
"artifacts-tron": ["**/*.json"],
"artifacts-zk": ["**/*.json"],
},
};
const callerRootFile = __filename;
populate(copyTargets, callerRootFile).then(() => {
console.log("Directories populated successfully.");
});
Concurrency Control
Semaphore for controlling access to a resource by multiple processes.
- constructor(max: number): Initializes the semaphore with a maximum number of concurrent locks.
- acquire(): Acquires a lock on the semaphore.
- release(): Releases a lock on the semaphore.
import { Semaphore } from '@layerzerolabs/evm-sdks-build'
const semaphore = new Semaphore(3)
async function task(id: number) {
await semaphore.acquire()
console.log(`Task ${id} acquired semaphore.`)
await new Promise((resolve) => setTimeout(resolve, 1000))
console.log(`Task ${id} releasing semaphore.`)
semaphore.release()
}
for (let i = 1 i <= 5 i++) {
task(i)
}