Update records publishing script to support YAML records #68

Merged
ashwin merged 1 commits from pm-yaml-records into laconic2 2024-06-25 06:36:47 +00:00
Showing only changes of commit 917c1d19f3 - Show all commits

View File

@ -1,6 +1,7 @@
import yargs from 'yargs'; import yargs from 'yargs';
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import yaml from 'js-yaml';
import assert from 'assert'; import assert from 'assert';
import { hideBin } from 'yargs/helpers'; import { hideBin } from 'yargs/helpers';
@ -54,10 +55,8 @@ async function main () {
async function processDir (directoryPath: string): Promise<void> { async function processDir (directoryPath: string): Promise<void> {
const files = fs.readdirSync(directoryPath); const files = fs.readdirSync(directoryPath);
// Check if any JSON record file exists in the directory const dirHasRecords = await publishRecordsFromDir(directoryPath);
if (files.some(file => file.endsWith('.json'))) { if (dirHasRecords) {
await publishRecordsFromDir(directoryPath);
// Skip further recursion in the current dir // Skip further recursion in the current dir
return; return;
} }
@ -73,21 +72,25 @@ async function processDir (directoryPath: string): Promise<void> {
} }
} }
async function publishRecordsFromDir (recordsDir: string): Promise<void> { async function publishRecordsFromDir (recordsDir: string): Promise<boolean> {
// 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 recordFiles = files.filter(file => ['.json', '.yaml', '.yml'].includes(path.extname(file).toLowerCase()));
if (recordFiles.length === 0) {
return false;
}
// Read record from each JSON file // Read record from each JSON file
console.log('**************************************'); console.log('**************************************');
console.log(`Publishing records from ${recordsDir}`); console.log(`Publishing records from ${recordsDir}`);
let recordType; let recordType;
for (let i = 0; i < jsonFiles.length; i++) { for (let i = 0; i < recordFiles.length; i++) {
const file = jsonFiles[i]; const file = recordFiles[i];
const filePath = path.resolve(recordsDir, file); const filePath = path.resolve(recordsDir, file);
const record = readRecord(filePath); const record = await readRecord(filePath);
// Publish record // Publish record
const result = await publishRecord(userKey, bondId, fee, record); const result = await publishRecord(userKey, bondId, fee, record);
@ -101,7 +104,7 @@ async function publishRecordsFromDir (recordsDir: string): Promise<void> {
// Check if deployment record files exist // Check if deployment record files exist
const deploymentRecordsDir = path.resolve(recordsDir, 'deployments'); const deploymentRecordsDir = path.resolve(recordsDir, 'deployments');
if (!fs.existsSync(deploymentRecordsDir) || !fs.statSync(deploymentRecordsDir).isDirectory()) { if (!fs.existsSync(deploymentRecordsDir) || !fs.statSync(deploymentRecordsDir).isDirectory()) {
return; return true;
} }
console.log('--------------------------------------'); console.log('--------------------------------------');
console.log(`Publishing deployment records from ${deploymentRecordsDir}`); console.log(`Publishing deployment records from ${deploymentRecordsDir}`);
@ -114,7 +117,7 @@ async function publishRecordsFromDir (recordsDir: string): Promise<void> {
const file = deploymentJsonFiles[i]; const file = deploymentJsonFiles[i];
const filePath = path.resolve(deploymentRecordsDir, file); const filePath = path.resolve(deploymentRecordsDir, file);
const deploymentRecord = readRecord(filePath); const deploymentRecord = await readRecord(filePath);
// Find record using name and given type // Find record using name and given type
const recordName = deploymentRecord.name; const recordName = deploymentRecord.name;
@ -137,13 +140,30 @@ async function publishRecordsFromDir (recordsDir: string): Promise<void> {
console.log(`Published record ${file}`); console.log(`Published record ${file}`);
txOutput(deploymentResult, JSON.stringify(deploymentResult, undefined, 2), '', false); txOutput(deploymentResult, JSON.stringify(deploymentResult, undefined, 2), '', false);
} }
return true;
} }
function readRecord (filePath: string): any { async function readRecord (filePath: string): Promise<any> {
let record; let record;
try { try {
const fileExt = path.extname(filePath).toLowerCase();
const data = fs.readFileSync(filePath, 'utf8'); const data = fs.readFileSync(filePath, 'utf8');
record = JSON.parse(data);
if (fileExt === '.json') {
// JSON file
record = JSON.parse(data);
} else {
// YAML file
({ record } = await yaml.load(data) as any);
// Convert sub-objects (other than arrays) to a JSON automatically.
for (const [k, v] of Object.entries(record)) {
if (v && typeof v === 'object' && !Array.isArray(v)) {
record[k] = JSON.stringify(v);
}
}
}
} catch (err) { } catch (err) {
console.error(`Error reading file ${filePath}:`, err); console.error(`Error reading file ${filePath}:`, err);
} }
@ -152,7 +172,8 @@ function readRecord (filePath: string): any {
} }
async function publishRecord (userKey: string, bondId: string, fee: StdFee, record: any): Promise<any> { async function publishRecord (userKey: string, bondId: string, fee: StdFee, record: any): Promise<any> {
if (record.repository) { // Replace repository URL with record id (if type is one of RecordType)
if (record.repository && Object.values(RecordType).includes(record.type)) {
const repoUrl = record.repository; const repoUrl = record.repository;
const queryResult = await registry.queryRecords({ type: RecordType.RepositoryRecord, url: repoUrl }, true); const queryResult = await registry.queryRecords({ type: RecordType.RepositoryRecord, url: repoUrl }, true);