Add a script to publish records from a given directory #62
@ -32,11 +32,11 @@
|
|||||||
|
|
||||||
## Run
|
## Run
|
||||||
|
|
||||||
* Publish watcher records from [`demo/records`](./demo/records):
|
* Publish records from [`demo/records`](./demo/records):
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Publishes records and corresponding 'deployment' records from the given directory
|
# Publishes records and corresponding 'deployment' records
|
||||||
yarn ts-node demo/scripts/publish-records.ts --config config.yml --records demo/records/watcher
|
yarn ts-node demo/scripts/publish-records.ts --config config.yml --records demo/records
|
||||||
```
|
```
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
|
@ -14,6 +14,11 @@ const recordTypeToRecordField = new Map<string, string>([
|
|||||||
['ServiceRecord', 'service']
|
['ServiceRecord', 'service']
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
let registry: Registry;
|
||||||
|
let fee: any;
|
||||||
|
let userKey: string;
|
||||||
|
let bondId: string;
|
||||||
|
|
||||||
async function main () {
|
async function main () {
|
||||||
const argv = getArgs();
|
const argv = getArgs();
|
||||||
const { records: recordsDir, config } = argv;
|
const { records: recordsDir, config } = argv;
|
||||||
@ -28,11 +33,38 @@ async function main () {
|
|||||||
throw new Error('bondId not set in config');
|
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);
|
registry = new Registry(gqlEndpoint, rpcEndpoint, chainId);
|
||||||
const fee = getGasAndFees(argv, registryConfig);
|
fee = getGasAndFees(argv, registryConfig);
|
||||||
|
|
||||||
|
await processDir(path.resolve(recordsDir));
|
||||||
|
}
|
||||||
|
|
||||||
|
async function processDir (directoryPath: string): Promise<void> {
|
||||||
|
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<void> {
|
||||||
// List record files
|
// List record files
|
||||||
const files = fs.readdirSync(recordsDir);
|
const files = fs.readdirSync(recordsDir);
|
||||||
const jsonFiles = files.filter(file => path.extname(file).toLowerCase() === '.json');
|
const jsonFiles = files.filter(file => path.extname(file).toLowerCase() === '.json');
|
||||||
@ -51,8 +83,8 @@ async function main () {
|
|||||||
// Publish record
|
// Publish record
|
||||||
const result = await registry.setRecord({ privateKey: userKey, record, bondId }, userKey, fee);
|
const result = await registry.setRecord({ privateKey: userKey, record, bondId }, userKey, fee);
|
||||||
|
|
||||||
console.log(`Published record from ${file}`);
|
console.log(`Published record ${file}`);
|
||||||
txOutput(result, JSON.stringify(result, undefined, 2), argv.output, argv.verbose);
|
txOutput(result, JSON.stringify(result, undefined, 2), '', false);
|
||||||
|
|
||||||
recordType = record.type;
|
recordType = record.type;
|
||||||
}
|
}
|
||||||
@ -62,8 +94,7 @@ async function main () {
|
|||||||
if (!fs.statSync(deploymentRecordsDir).isDirectory()) {
|
if (!fs.statSync(deploymentRecordsDir).isDirectory()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
console.log('--------------------------------------');
|
||||||
console.log('**************************************');
|
|
||||||
console.log(`Publishing deployment records from ${deploymentRecordsDir}`);
|
console.log(`Publishing deployment records from ${deploymentRecordsDir}`);
|
||||||
|
|
||||||
// List record files
|
// List record files
|
||||||
@ -94,8 +125,8 @@ async function main () {
|
|||||||
// Publish record
|
// 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 record from ${file}`);
|
console.log(`Published record ${file}`);
|
||||||
txOutput(deploymentResult, JSON.stringify(deploymentResult, undefined, 2), argv.output, argv.verbose);
|
txOutput(deploymentResult, JSON.stringify(deploymentResult, undefined, 2), '', false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user