Read and publish record files recursively

This commit is contained in:
Prathamesh Musale 2024-05-08 15:47:42 +05:30
parent 7bdeab22f8
commit 52a16c1c6b
2 changed files with 43 additions and 12 deletions

View File

@ -32,11 +32,11 @@
## Run
* Publish watcher records from [`demo/records`](./demo/records):
* Publish records from [`demo/records`](./demo/records):
```bash
# Publishes records and corresponding 'deployment' records from the given directory
yarn ts-node demo/scripts/publish-records.ts --config config.yml --records demo/records/watcher
# Publishes records and corresponding 'deployment' records
yarn ts-node demo/scripts/publish-records.ts --config config.yml --records demo/records
```
### Example

View File

@ -14,6 +14,11 @@ const recordTypeToRecordField = new Map<string, string>([
['ServiceRecord', 'service']
]);
let registry: Registry;
let fee: any;
let userKey: string;
let bondId: string;
async function main () {
const argv = getArgs();
const { records: recordsDir, config } = argv;
@ -28,11 +33,38 @@ async function main () {
throw new Error('bondId not set in config');
}
const { rpcEndpoint, gqlEndpoint, userKey, bondId, chainId } = getConnectionInfo(argv, registryConfig);
let rpcEndpoint, gqlEndpoint, chainId: string;
({ rpcEndpoint, gqlEndpoint, userKey, bondId, chainId } = getConnectionInfo(argv, registryConfig));
const registry = new Registry(gqlEndpoint, rpcEndpoint, chainId);
const fee = getGasAndFees(argv, registryConfig);
registry = new Registry(gqlEndpoint, rpcEndpoint, chainId);
fee = getGasAndFees(argv, registryConfig);
await processDir(path.resolve(recordsDir));
}
async function processDir (directoryPath: string): Promise<void> {
const files = fs.readdirSync(directoryPath);
// Check if any JSON record file exists in the directory
if (files.some(file => file.endsWith('.json'))) {
await publishRecordsFromDir(directoryPath);
// Skip further recursion in the current dir
return;
}
// Recursively iterate through subdirectories
for (let i = 0; i < files.length; i++) {
const file = files[i];
const filePath = path.join(directoryPath, file);
if (fs.statSync(filePath).isDirectory()) {
await processDir(filePath);
}
}
}
async function publishRecordsFromDir (recordsDir: string): Promise<void> {
// List record files
const files = fs.readdirSync(recordsDir);
const jsonFiles = files.filter(file => path.extname(file).toLowerCase() === '.json');
@ -51,8 +83,8 @@ async function main () {
// Publish record
const result = await registry.setRecord({ privateKey: userKey, record, bondId }, userKey, fee);
console.log(`Published record from ${file}`);
txOutput(result, JSON.stringify(result, undefined, 2), argv.output, argv.verbose);
console.log(`Published record ${file}`);
txOutput(result, JSON.stringify(result, undefined, 2), '', false);
recordType = record.type;
}
@ -62,8 +94,7 @@ async function main () {
if (!fs.statSync(deploymentRecordsDir).isDirectory()) {
return;
}
console.log('**************************************');
console.log('--------------------------------------');
console.log(`Publishing deployment records from ${deploymentRecordsDir}`);
// List record files
@ -94,8 +125,8 @@ async function main () {
// Publish record
const deploymentResult = await registry.setRecord({ privateKey: userKey, record: deploymentRecord, bondId }, userKey, fee);
console.log(`Published record from ${file}`);
txOutput(deploymentResult, JSON.stringify(deploymentResult, undefined, 2), argv.output, argv.verbose);
console.log(`Published record ${file}`);
txOutput(deploymentResult, JSON.stringify(deploymentResult, undefined, 2), '', false);
}
}