Add a script to publish records from a given directory #62
@ -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,32 +50,46 @@ 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 {
|
||||
let record;
|
||||
@ -97,6 +119,12 @@ function getArgs (): any {
|
||||
type: 'string',
|
||||
demandOption: true
|
||||
})
|
||||
.option('type', {
|
||||
alias: 't',
|
||||
describe: 'Record type',
|
||||
type: 'string',
|
||||
demandOption: true
|
||||
})
|
||||
.help().argv;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user