diff --git a/src/publish-endponit-records.ts b/src/publish-endponit-records.ts index fae97d7..571715d 100644 --- a/src/publish-endponit-records.ts +++ b/src/publish-endponit-records.ts @@ -7,9 +7,15 @@ import { Registry } from '@cerc-io/registry-sdk'; import { getConfig, getGasAndFees, getConnectionInfo, txOutput } from './util'; +const recordTypeToRecordField = new Map([ + ['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,30 +50,44 @@ 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 - const deploymentRecordsDir = path.resolve(recordsDir, 'deployments'); + // 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 deploymentResult = await registry.setRecord({ privateKey: userKey, record: deploymentRecord, bondId }, userKey, fee); + const filePath = path.resolve(deploymentRecordsDir, file); + const deploymentRecord = readRecord(filePath); - console.log(`Published deployment record from ${deploymentRecordFile}`); - txOutput(deploymentResult, JSON.stringify(deploymentResult, undefined, 2), argv.output, argv.verbose); - } + // 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 record from ${file}`); + txOutput(deploymentResult, JSON.stringify(deploymentResult, undefined, 2), argv.output, argv.verbose); } } @@ -97,6 +119,12 @@ function getArgs (): any { type: 'string', demandOption: true }) + .option('type', { + alias: 't', + describe: 'Record type', + type: 'string', + demandOption: true + }) .help().argv; }