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';
|
import { getConfig, getGasAndFees, getConnectionInfo, txOutput } from './util';
|
||||||
|
|
||||||
|
const recordTypeToRecordField = new Map<string, string>([
|
||||||
|
['WatcherRecord', 'watcher'],
|
||||||
|
['SubgraphRecord', 'subgraph'],
|
||||||
|
['ServiceRecord', 'service']
|
||||||
|
]);
|
||||||
|
|
||||||
async function main () {
|
async function main () {
|
||||||
const argv = getArgs();
|
const argv = getArgs();
|
||||||
const { records: recordsDir, config } = argv;
|
const { records: recordsDir, config, type: recordType } = argv;
|
||||||
|
|
||||||
const { services: { registry: registryConfig } } = getConfig(config as string);
|
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');
|
const jsonFiles = files.filter(file => path.extname(file).toLowerCase() === '.json');
|
||||||
|
|
||||||
// Read record from each JSON file
|
// Read record from each JSON file
|
||||||
|
console.log('**************************************');
|
||||||
|
console.log(`Publishing records from ${recordsDir}`);
|
||||||
for (let i = 0; i < jsonFiles.length; i++) {
|
for (let i = 0; i < jsonFiles.length; i++) {
|
||||||
const file = jsonFiles[i];
|
const file = jsonFiles[i];
|
||||||
|
|
||||||
@ -42,31 +50,45 @@ async function main () {
|
|||||||
|
|
||||||
console.log(`Published record from ${file}`);
|
console.log(`Published record from ${file}`);
|
||||||
txOutput(result, JSON.stringify(result, undefined, 2), argv.output, argv.verbose);
|
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');
|
const deploymentRecordsDir = path.resolve(recordsDir, 'deployments');
|
||||||
|
if (!fs.statSync(deploymentRecordsDir).isDirectory()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (fs.statSync(deploymentRecordsDir).isDirectory()) {
|
console.log('**************************************');
|
||||||
const recordName = path.basename(file, path.extname(file));
|
console.log(`Publishing deployment records from ${deploymentRecordsDir}`);
|
||||||
const deploymentRecordFile = `${recordName}Deployment.json`;
|
|
||||||
const deploymentRecordFilePath = path.resolve(deploymentRecordsDir, deploymentRecordFile);
|
|
||||||
|
|
||||||
// Check if deployment record exists
|
// List record files
|
||||||
if (fs.existsSync(deploymentRecordFilePath)) {
|
const deploymentFiles = fs.readdirSync(deploymentRecordsDir);
|
||||||
console.log(`Deployment record ${deploymentRecordFile} found`);
|
const deploymentJsonFiles = deploymentFiles.filter(file => path.extname(file).toLowerCase() === '.json');
|
||||||
const deploymentRecord = readRecord(deploymentRecordFilePath);
|
|
||||||
|
|
||||||
// Set watcher field to watcher record id
|
for (let i = 0; i < deploymentJsonFiles.length; i++) {
|
||||||
deploymentRecord.watcher = result.id;
|
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);
|
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);
|
txOutput(deploymentResult, JSON.stringify(deploymentResult, undefined, 2), argv.output, argv.verbose);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function readRecord (filePath: string): any {
|
function readRecord (filePath: string): any {
|
||||||
@ -97,6 +119,12 @@ function getArgs (): any {
|
|||||||
type: 'string',
|
type: 'string',
|
||||||
demandOption: true
|
demandOption: true
|
||||||
})
|
})
|
||||||
|
.option('type', {
|
||||||
|
alias: 't',
|
||||||
|
describe: 'Record type',
|
||||||
|
type: 'string',
|
||||||
|
demandOption: true
|
||||||
|
})
|
||||||
.help().argv;
|
.help().argv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user