diff --git a/demo/README.md b/demo/README.md index 63a0f55..093f587 100644 --- a/demo/README.md +++ b/demo/README.md @@ -32,11 +32,11 @@ ## Run -* Publish watcher records from [`demo/records`](./demo/records): +* Publish records from [`demo/records`](./demo/records): ```bash - # Publishes records and corresponding 'deployment' records from the given directory - yarn ts-node demo/scripts/publish-records.ts --config config.yml --records demo/records/watcher + # Publishes records and corresponding 'deployment' records + yarn ts-node demo/scripts/publish-records.ts --config config.yml --records demo/records ``` ### Example diff --git a/demo/scripts/publish-records.ts b/demo/scripts/publish-records.ts index 152df51..aa4c1c6 100644 --- a/demo/scripts/publish-records.ts +++ b/demo/scripts/publish-records.ts @@ -14,6 +14,11 @@ const recordTypeToRecordField = new Map([ ['ServiceRecord', 'service'] ]); +let registry: Registry; +let fee: any; +let userKey: string; +let bondId: string; + async function main () { const argv = getArgs(); const { records: recordsDir, config } = argv; @@ -28,11 +33,38 @@ async function main () { throw new Error('bondId not set in config'); } - const { rpcEndpoint, gqlEndpoint, userKey, bondId, chainId } = getConnectionInfo(argv, registryConfig); + let rpcEndpoint, gqlEndpoint, chainId: string; + ({ rpcEndpoint, gqlEndpoint, userKey, bondId, chainId } = getConnectionInfo(argv, registryConfig)); - const registry = new Registry(gqlEndpoint, rpcEndpoint, chainId); - const fee = getGasAndFees(argv, registryConfig); + registry = new Registry(gqlEndpoint, rpcEndpoint, chainId); + fee = getGasAndFees(argv, registryConfig); + await processDir(path.resolve(recordsDir)); +} + +async function processDir (directoryPath: string): Promise { + const files = fs.readdirSync(directoryPath); + + // Check if any JSON record file exists in the directory + if (files.some(file => file.endsWith('.json'))) { + await publishRecordsFromDir(directoryPath); + + // Skip further recursion in the current dir + return; + } + + // Recursively iterate through subdirectories + for (let i = 0; i < files.length; i++) { + const file = files[i]; + const filePath = path.join(directoryPath, file); + + if (fs.statSync(filePath).isDirectory()) { + await processDir(filePath); + } + } +} + +async function publishRecordsFromDir (recordsDir: string): Promise { // List record files const files = fs.readdirSync(recordsDir); const jsonFiles = files.filter(file => path.extname(file).toLowerCase() === '.json'); @@ -51,8 +83,8 @@ async function main () { // 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); + console.log(`Published record ${file}`); + txOutput(result, JSON.stringify(result, undefined, 2), '', false); recordType = record.type; } @@ -62,8 +94,7 @@ async function main () { if (!fs.statSync(deploymentRecordsDir).isDirectory()) { return; } - - console.log('**************************************'); + console.log('--------------------------------------'); console.log(`Publishing deployment records from ${deploymentRecordsDir}`); // List record files @@ -94,8 +125,8 @@ async function main () { // 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); + console.log(`Published record ${file}`); + txOutput(deploymentResult, JSON.stringify(deploymentResult, undefined, 2), '', false); } }