Add a script to publish records from a given directory #62

Merged
ashwin merged 20 commits from deep-stack/laconic-registry-cli:pm-endpoint-records into laconic2 2024-05-09 10:35:55 +00:00
Showing only changes of commit 4aba3d0168 - Show all commits

View File

@ -7,9 +7,15 @@ import { Registry } from '@cerc-io/registry-sdk';
import { getConfig, getGasAndFees, getConnectionInfo, txOutput } from './util';
const recordTypeToRecordField = new Map<string, string>([
['WatcherRecord', 'watcher'],
['SubgraphRecord', 'subgraph'],
['ServiceRecord', 'service']
]);
async function main () {
const argv = getArgs();
const { records: recordsDir, config } = argv;
const { records: recordsDir, config, type: recordType } = argv;
const { services: { registry: registryConfig } } = getConfig(config as string);
@ -31,6 +37,8 @@ async function main () {
const jsonFiles = files.filter(file => path.extname(file).toLowerCase() === '.json');
// Read record from each JSON file
console.log('**************************************');
console.log(`Publishing records from ${recordsDir}`);
for (let i = 0; i < jsonFiles.length; i++) {
const file = jsonFiles[i];
@ -42,31 +50,45 @@ async function main () {
console.log(`Published record from ${file}`);
txOutput(result, JSON.stringify(result, undefined, 2), argv.output, argv.verbose);
}
// Find the deployment record using filename
// Check if deployment record files exist
const deploymentRecordsDir = path.resolve(recordsDir, 'deployments');
if (!fs.statSync(deploymentRecordsDir).isDirectory()) {
return;
}
if (fs.statSync(deploymentRecordsDir).isDirectory()) {
const recordName = path.basename(file, path.extname(file));
const deploymentRecordFile = `${recordName}Deployment.json`;
const deploymentRecordFilePath = path.resolve(deploymentRecordsDir, deploymentRecordFile);
console.log('**************************************');
console.log(`Publishing deployment records from ${deploymentRecordsDir}`);
// Check if deployment record exists
if (fs.existsSync(deploymentRecordFilePath)) {
console.log(`Deployment record ${deploymentRecordFile} found`);
const deploymentRecord = readRecord(deploymentRecordFilePath);
// List record files
const deploymentFiles = fs.readdirSync(deploymentRecordsDir);
const deploymentJsonFiles = deploymentFiles.filter(file => path.extname(file).toLowerCase() === '.json');
// Set watcher field to watcher record id
deploymentRecord.watcher = result.id;
for (let i = 0; i < deploymentJsonFiles.length; i++) {
const file = deploymentJsonFiles[i];
// Publish deployment record
const filePath = path.resolve(deploymentRecordsDir, file);
const deploymentRecord = readRecord(filePath);
// Find record using name and given type
const recordName = deploymentRecord.name;
const queryResult = await registry.queryRecords({ type: recordType, name: recordName }, true);
if (queryResult.length === 0) {
throw new Error(`Record not found, type: ${recordType}, name: ${recordName}`);
}
const recordId = queryResult[0].id;
// Set record field to record id
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
deploymentRecord[recordTypeToRecordField.get(recordType)!] = recordId;
// Publish record
const deploymentResult = await registry.setRecord({ privateKey: userKey, record: deploymentRecord, bondId }, userKey, fee);
console.log(`Published deployment record from ${deploymentRecordFile}`);
console.log(`Published record from ${file}`);
txOutput(deploymentResult, JSON.stringify(deploymentResult, undefined, 2), argv.output, argv.verbose);
}
}
}
}
function readRecord (filePath: string): any {
@ -97,6 +119,12 @@ function getArgs (): any {
type: 'string',
demandOption: true
})
.option('type', {
alias: 't',
describe: 'Record type',
type: 'string',
demandOption: true
})
.help().argv;
}