laconic-registry-cli/src/publish-endponit-records.ts

110 lines
3.3 KiB
TypeScript
Raw Normal View History

import yargs from 'yargs';
import fs from 'fs';
import path from 'path';
import { hideBin } from 'yargs/helpers';
import { Registry } from '@cerc-io/registry-sdk';
import { getConfig, getGasAndFees, getConnectionInfo, txOutput } from './util';
async function main () {
const argv = getArgs();
const { records: recordsDir, config } = argv;
const { services: { registry: registryConfig } } = getConfig(config as string);
if (registryConfig.userKey == null) {
throw new Error('userKey not set in config');
}
if (registryConfig.bondId == null) {
throw new Error('bondId not set in config');
}
const { rpcEndpoint, gqlEndpoint, userKey, bondId, chainId } = getConnectionInfo(argv, registryConfig);
const registry = new Registry(gqlEndpoint, rpcEndpoint, chainId);
const fee = getGasAndFees(argv, registryConfig);
// List record files
const files = fs.readdirSync(recordsDir);
const jsonFiles = files.filter(file => path.extname(file).toLowerCase() === '.json');
// Read record from each JSON file
for (let i = 0; i < jsonFiles.length; i++) {
const file = jsonFiles[i];
const filePath = path.resolve(recordsDir, file);
const record = readRecord(filePath);
// 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);
// Find the deployment record using filename
const deploymentRecordsDir = path.resolve(recordsDir, 'deployments');
if (fs.statSync(deploymentRecordsDir).isDirectory()) {
const recordName = path.basename(file, path.extname(file));
const deploymentRecordFile = `${recordName}Deployment.json`;
const deploymentRecordFilePath = path.resolve(deploymentRecordsDir, deploymentRecordFile);
// Check if deployment record exists
if (fs.existsSync(deploymentRecordFilePath)) {
console.log(`Deployment record ${deploymentRecordFile} found`);
const deploymentRecord = readRecord(deploymentRecordFilePath);
// Set watcher field to watcher record id
deploymentRecord.watcher = result.id;
// Publish deployment record
const deploymentResult = await registry.setRecord({ privateKey: userKey, record: deploymentRecord, bondId }, userKey, fee);
console.log(`Published deployment record from ${deploymentRecordFile}`);
txOutput(deploymentResult, JSON.stringify(deploymentResult, undefined, 2), argv.output, argv.verbose);
}
}
}
}
function readRecord (filePath: string): any {
let record;
try {
const data = fs.readFileSync(filePath, 'utf8');
record = JSON.parse(data);
} catch (err) {
console.error(`Error reading file ${filePath}:`, err);
}
return record;
}
function getArgs (): any {
return yargs(hideBin(process.argv)).parserConfiguration({
'parse-numbers': false
}).usage('Usage: $0 [options]')
.option('config', {
alias: 'c',
describe: 'Config',
type: 'string',
demandOption: true
})
.option('records', {
alias: 'r',
describe: 'Endpoint records',
type: 'string',
demandOption: true
})
.help().argv;
}
main()
.catch(err => {
console.error(err);
})
.finally(() => {
console.log('Done');
});