Replace repository URL in records with published repo record id
All checks were successful
Lint / lint (18.x) (pull_request) Successful in 1m7s
Tests / cli_tests (18.x) (pull_request) Successful in 8m13s

This commit is contained in:
Prathamesh Musale 2024-06-21 15:59:09 +05:30
parent 08d6351d25
commit 120f36d07d

View File

@ -4,14 +4,23 @@ import path from 'path';
import assert from 'assert';
import { hideBin } from 'yargs/helpers';
import { StdFee } from '@cosmjs/stargate';
import { Registry } from '@cerc-io/registry-sdk';
import { getConfig, getGasAndFees, getConnectionInfo, txOutput } from '../../src/util';
enum RecordType {
RepositoryRecord = 'RepositoryRecord',
ServiceRecord = 'ServiceRecord',
StackRecord = 'StackRecord',
SubgraphRecord = 'SubgraphRecord',
WatcherRecord = 'WatcherRecord',
}
const recordTypeToRecordField = new Map<string, string>([
['WatcherRecord', 'watcher'],
['SubgraphRecord', 'subgraph'],
['ServiceRecord', 'service']
[RecordType.WatcherRecord, 'watcher'],
[RecordType.SubgraphRecord, 'subgraph'],
[RecordType.ServiceRecord, 'service']
]);
let registry: Registry;
@ -81,7 +90,7 @@ async function publishRecordsFromDir (recordsDir: string): Promise<void> {
const record = readRecord(filePath);
// Publish record
const result = await registry.setRecord({ privateKey: userKey, record, bondId }, userKey, fee);
const result = await publishRecord(userKey, bondId, fee, record);
console.log(`Published record ${file}`);
txOutput(result, JSON.stringify(result, undefined, 2), '', false);
@ -142,6 +151,25 @@ function readRecord (filePath: string): any {
return record;
}
async function publishRecord (userKey: string, bondId: string, fee: StdFee, record: any): Promise<any> {
if (record.repository) {
const repoUrl = record.repository;
const queryResult = await registry.queryRecords({ type: RecordType.RepositoryRecord, url: repoUrl }, true);
if (queryResult.length === 0) {
throw new Error(`Record not found, type: ${RecordType.RepositoryRecord}, url: ${repoUrl}`);
}
// Assume the first query result
const repoRecordId = queryResult[0].id;
// Replace repository URL with the repo record id
record.repository = repoRecordId;
}
return registry.setRecord({ privateKey: userKey, record, bondId }, userKey, fee);
}
function getArgs (): any {
return yargs(hideBin(process.argv)).parserConfiguration({
'parse-numbers': false