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

78 lines
2.1 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);
const { rpcEndpoint, gqlEndpoint, userKey, bondId, chainId } = getConnectionInfo(argv, registryConfig);
const registry = new Registry(gqlEndpoint, rpcEndpoint, chainId);
const fee = getGasAndFees(argv, registryConfig);
await fs.readdir(recordsDir, async (err, files) => {
if (err) {
console.error('Error reading directory:', err);
return;
}
// Filter JSON files
const jsonFiles = files.filter(file => path.extname(file).toLowerCase() === '.json');
// Read content of each JSON file
for (let i = 0; i < jsonFiles.length; i++) {
const file = jsonFiles[i];
const filePath = path.resolve(recordsDir, file);
let record;
try {
const data = fs.readFileSync(filePath, 'utf8');
record = JSON.parse(data);
} catch (error) {
console.error(`Error reading file ${file}:`, err);
}
// TODO: validate the records using a schema
const result = await registry.setRecord({ privateKey: userKey, record, bondId }, userKey, fee);
console.log(`Published record ${file}`);
txOutput(result, JSON.stringify(result, undefined, 2), argv.output, argv.verbose);
}
});
}
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');
});