From 10299752396e5dfea927ed909be00ce22cbee143 Mon Sep 17 00:00:00 2001 From: kth-tw <88102592+kth-tw@users.noreply.github.com> Date: Tue, 7 Dec 2021 10:57:06 +0800 Subject: [PATCH 1/3] replace 'config-yaml/configtxOrgs.json' with 'config-yaml/orgs/[peer/orderer]-[orgName].json' --- src/instance/bdkFile.ts | 31 +++++++++++++++++++++++++----- src/model/prompts/util.ts | 20 +++++++------------ src/model/yaml/network/configtx.ts | 13 +++++-------- src/service/channel.ts | 14 -------------- src/service/network.ts | 7 ++++--- src/service/orderer.ts | 10 ++++------ src/service/org.ts | 17 ++++++++++++++++ src/service/peer.ts | 9 ++++----- test/service/network.test.ts | 6 +++++- 9 files changed, 72 insertions(+), 55 deletions(-) diff --git a/src/instance/bdkFile.ts b/src/instance/bdkFile.ts index be95f49e..53176e3e 100644 --- a/src/instance/bdkFile.ts +++ b/src/instance/bdkFile.ts @@ -4,7 +4,7 @@ import YAML from 'js-yaml' import { parse, stringify } from 'envfile' import CryptoConfigYaml from '../model/yaml/network/cryptoConfigYaml' import ConnectionConfigYaml from '../model/yaml/network/connectionConfigYaml' -import ConfigtxYaml, { ConfigtxOrgs } from '../model/yaml/network/configtx' +import ConfigtxYaml, { ConfigtxOrgs, OrdererOrganizationInterface, PeerOrganizationInterface } from '../model/yaml/network/configtx' import { ConfigEnvType } from '../model/type/config.type' import OrdererDockerComposeYaml from '../model/yaml/docker-compose/ordererDockerComposeYaml' import PeerDockerComposeYaml from '../model/yaml/docker-compose/peerDockerComposeYaml' @@ -63,6 +63,10 @@ export default class BdkFile { fs.mkdirSync(`${this.bdkPath}/config-yaml`, { recursive: true }) } + private createConfigYamlOrgsFolder () { + fs.mkdirSync(`${this.bdkPath}/config-yaml/orgs`, { recursive: true }) + } + private createTlsFolder (orgDomainName: string) { fs.mkdirSync(`${this.bdkPath}/tlsca/${orgDomainName}`, { recursive: true }) } @@ -81,9 +85,14 @@ export default class BdkFile { fs.writeFileSync(`${this.bdkPath}/config-yaml/configtx.yaml`, configtxYaml.getYamlString()) } - public createConfigtxOrgs (configtxOrgs: ConfigtxOrgs) { - this.createConfigYamlFolder() - fs.writeFileSync(`${this.bdkPath}/config-yaml/configtxOrgs.json`, JSON.stringify(configtxOrgs)) + public createConfigtxPeerOrg (peerOrg: PeerOrganizationInterface) { + this.createConfigYamlOrgsFolder() + fs.writeFileSync(`${this.bdkPath}/config-yaml/orgs/peer-${peerOrg.Name}.json`, JSON.stringify(peerOrg)) + } + + public createConfigtxOrdererOrg (OrdererOrg: OrdererOrganizationInterface) { + this.createConfigYamlOrgsFolder() + fs.writeFileSync(`${this.bdkPath}/config-yaml/orgs/orderer-${OrdererOrg.Name}.json`, JSON.stringify(OrdererOrg)) } public getOrdererServerCertToBase64 (hostname: string, domain: string) { @@ -91,7 +100,19 @@ export default class BdkFile { } public getConfigtxOrgs (): ConfigtxOrgs { - return JSON.parse(fs.readFileSync(`${this.bdkPath}/config-yaml/configtxOrgs.json`).toString()) + const configtxOrgs: ConfigtxOrgs = { + ordererOrgs: {}, + peerOrgs: {}, + } + fs.readdirSync(`${this.bdkPath}/config-yaml/orgs`).forEach((filename: string) => { + if (/^orderer-.*\.json$/.test(filename)) { + configtxOrgs.ordererOrgs[filename.replace(/^orderer-/, '').replace(/\.json$/, '')] = JSON.parse(fs.readFileSync(`${this.bdkPath}/config-yaml/orgs/${filename}`).toString()) + } else if (/^peer-.*\.json$/.test(filename)) { + configtxOrgs.peerOrgs[filename.replace(/^peer-/, '').replace(/\.json$/, '')] = JSON.parse(fs.readFileSync(`${this.bdkPath}/config-yaml/orgs/${filename}`).toString()) + } + }, + ) + return configtxOrgs } public copyOrdererOrgTLSCa (hostname: string, domain: string) { diff --git a/src/model/prompts/util.ts b/src/model/prompts/util.ts index ae1688e1..8cd33e8d 100644 --- a/src/model/prompts/util.ts +++ b/src/model/prompts/util.ts @@ -4,7 +4,6 @@ import Channel from '../../service/channel' import { Config } from '../../config' import Chaincode from '../../service/chaincode' import { logger } from '../../util' -import { ConfigtxOrgs } from '../yaml/network/configtx' export const joinedChannelChoice = async (channel: Channel): Promise => { const listJoinedChannelResult = await channel.listJoinedChannel() @@ -44,18 +43,13 @@ export const getChaincodeList = (config: Config): {name: string; version: number : [] } -const getConfigtxOrgsJson = (config: Config): ConfigtxOrgs => { - try { - const hostBasePath = `${config.infraConfig.bdkPath}/${config.networkName}` - return fs.existsSync(`${hostBasePath}/config-yaml/configtxOrgs.json`) ? JSON.parse(fs.readFileSync(`${hostBasePath}/config-yaml/configtxOrgs.json`).toString()) : { ordererOrgs: {}, peerOrgs: {} } - } catch { - return { ordererOrgs: {}, peerOrgs: {} } - } -} export const getOrdererList = (config: Config): string[] => { try { - const configtxOrgsJson = getConfigtxOrgsJson(config) - return Object.values(configtxOrgsJson.ordererOrgs).map(x => x.OrdererEndpoints).reduce((prev, curr) => (prev.concat(curr)), []) + const hostBasePath = `${config.infraConfig.bdkPath}/${config.networkName}` + const ordererOrgs = fs.readdirSync(`${hostBasePath}/config-yaml/orgs`) + .filter((filename: string) => (/^orderer-.*\.json$/.test(filename))) + .map((filename: string) => (JSON.parse(fs.readFileSync(`${hostBasePath}/config-yaml/orgs/${filename}`).toString()))) + return ordererOrgs.map(x => x.OrdererEndpoints).reduce((prev, curr) => (prev.concat(curr)), []) } catch { return [] } @@ -72,8 +66,8 @@ export const getChannelList = (config: Config): string[] => { export const getOrgNames = (config: Config): string[] => { try { - const configtxOrgsJson = getConfigtxOrgsJson(config) - return Object.keys(configtxOrgsJson.peerOrgs) + const hostBasePath = `${config.infraConfig.bdkPath}/${config.networkName}` + return fs.readdirSync(`${hostBasePath}/config-yaml/orgs`).filter(x => /^peer-.*\.json$/.test(x)).map(x => x.replace(/^peer-/, '').replace(/\.json$/, '')) } catch { return [] } diff --git a/src/model/yaml/network/configtx.ts b/src/model/yaml/network/configtx.ts index b385ef23..27988bbb 100644 --- a/src/model/yaml/network/configtx.ts +++ b/src/model/yaml/network/configtx.ts @@ -16,7 +16,7 @@ interface OrganizationInterface { ID: string // ID to load the MSP definition as MSPDir: string // MSPDir is the filesystem path which contains the MSP configuration } -interface OrdererOrganizationInterface extends OrganizationInterface { +export interface OrdererOrganizationInterface extends OrganizationInterface { Policies: { // Policies defines the set of policies at this level of the config tree. For organization policies, their canonical path is usually /Channel/// Readers: PolicyInterface Writers: PolicyInterface @@ -24,7 +24,7 @@ interface OrdererOrganizationInterface extends OrganizationInterface { } OrdererEndpoints: string[] } -interface PeerOrganizationInterface extends OrganizationInterface { +export interface PeerOrganizationInterface extends OrganizationInterface { Policies: { // Policies defines the set of policies at this level of the config tree. For organization policies, their canonical path is usually /Channel/// Readers: PolicyInterface Writers: PolicyInterface @@ -270,6 +270,7 @@ class ConfigtxYaml extends BdkYaml { this.ordererOrgs[payload.name] = newOrdererOrg this.value.Organizations.push(newOrdererOrg) + return newOrdererOrg } public addPeerOrg (payload: { name: string; mspDir: string; domain: string; anchorPeers: {hostname: string; port?: number}[]}) { @@ -281,7 +282,7 @@ class ConfigtxYaml extends BdkYaml { // ports -> [7051, 7151] const newPeerOrg: PeerOrganizationInterface = { Name: payload.name, - ID: `${payload.name}`, + ID: payload.name, MSPDir: payload.mspDir, Policies: { Readers: { @@ -306,6 +307,7 @@ class ConfigtxYaml extends BdkYaml { this.peerOrgs[payload.name] = newPeerOrg this.value.Organizations.push(newPeerOrg) + return newPeerOrg } public addSystemChannelProfile (payload: { name: string; etcdRaftConsenters: EtcdRaftConsentersInterface[]; ordererOrgs: string[]; consortiums: { [cousortiumName: string]: string[] }; batchTimeout?: string; BatchSize?: BatchSizeInterface }) { @@ -373,11 +375,6 @@ class ConfigtxYaml extends BdkYaml { } } - public exportOrgs () { - const configtxOrgs: ConfigtxOrgs = { ordererOrgs: this.ordererOrgs, peerOrgs: this.peerOrgs } - return configtxOrgs - } - public importOrgs (data: ConfigtxOrgs) { this.ordererOrgs = data.ordererOrgs this.peerOrgs = data.peerOrgs diff --git a/src/service/channel.ts b/src/service/channel.ts index 0e7a4f60..b4429a45 100644 --- a/src/service/channel.ts +++ b/src/service/channel.ts @@ -272,20 +272,6 @@ export default class Channel extends AbstractService { } } - /** - * @description 由 config.yaml 建立 peer org 的 json 檔案 - * @param orgName - peer org 的名稱 - * @returns 在 blockchain network 資料夾底下 org-json/[peer org 名稱].json 檔案 - */ - // create new org configtx yaml - public async createNewOrgConfigTx (orgName: string) { - logger.info(`[*] Generate ${orgName} config json file: configtxgen ${this.config.infraConfig.bdkPath}/${this.config.networkName}/${orgName}/${orgName}.json`) - - const orgJson = (await (new FabricTools(this.config, this.infra)).createNewOrgConfigTx(orgName)).stdout.match(/{.*}/s)?.[0] || '' - - this.bdkFile.createOrgConfigJson(orgName, orgJson) - } - /** * fetch channel config to artifact/${channelName}/${channelName}_config_bock.pb */ diff --git a/src/service/network.ts b/src/service/network.ts index 9a558d40..c49743a0 100644 --- a/src/service/network.ts +++ b/src/service/network.ts @@ -200,13 +200,14 @@ export default class Network extends AbstractService { const etcdRaftConsenters: { Host: string; Port: number; ClientTLSCert: string; ServerTLSCert: string }[] = [] dto.ordererOrgs.forEach(ordererOrg => { - configtxYaml.addOrdererOrg({ + const newOrg = configtxYaml.addOrdererOrg({ name: ordererOrg.name, mspDir: `${this.config.infraConfig.dockerPath}/ordererOrganizations/${ordererOrg.domain}/msp`, domain: ordererOrg.domain, hostname: ordererOrg.hostname, ports: ordererOrg.ports?.map(x => x.port), }) + this.bdkFile.createConfigtxOrdererOrg(newOrg) ordererOrg.hostname.forEach((hostname, i) => { etcdRaftConsenters.push({ @@ -219,12 +220,13 @@ export default class Network extends AbstractService { }) dto.peerOrgs.forEach(peerOrg => { - configtxYaml.addPeerOrg({ + const newOrg = configtxYaml.addPeerOrg({ name: peerOrg.name, mspDir: `${this.config.infraConfig.dockerPath}/peerOrganizations/${peerOrg.domain}/msp`, domain: peerOrg.domain, anchorPeers: [{ hostname: `peer0.${peerOrg.domain}`, port: peerOrg?.ports?.[0]?.port }], }) + this.bdkFile.createConfigtxPeerOrg(newOrg) }) configtxYaml.addSystemChannelProfile({ @@ -237,7 +239,6 @@ export default class Network extends AbstractService { }) this.bdkFile.createConfigtx(configtxYaml) - this.bdkFile.createConfigtxOrgs(configtxYaml.exportOrgs()) return configtxYaml } diff --git a/src/service/orderer.ts b/src/service/orderer.ts index a2c73549..d956118a 100644 --- a/src/service/orderer.ts +++ b/src/service/orderer.ts @@ -11,6 +11,7 @@ import { OrdererAddType, ConsenterType, OrdererUpType, OrdererAddOrgToChannelTyp import { InfraRunnerResultType } from '../instance/infra/InfraRunner.interface' import { OrgOrdererCreateType } from '../model/type/org.type' import { AbstractService } from './Service.abstract' +import Org from './org' export default class Orderer extends AbstractService { /** @@ -62,16 +63,17 @@ export default class Orderer extends AbstractService { const ports = ordererOrg.ports?.map(port => port.port) - configtxYaml.addOrdererOrg({ + const newOrg = configtxYaml.addOrdererOrg({ name: ordererOrg.name, mspDir: `${this.config.infraConfig.dockerPath}/ordererOrganizations/${ordererOrg.domain}/msp`, domain: ordererOrg.domain, hostname: ordererOrg.hostname, ports, }) + this.bdkFile.createConfigtxOrdererOrg(newOrg) + await (new Org(this.config, this.infra)).createNewOrgConfigTx(ordererOrg.name, configtxYaml) const ordererOrgConsenter: ConsenterType[] = [] - ordererOrg.hostname.forEach((hostname, index) => { const serverCertBase64 = this.bdkFile.getOrdererServerCertToBase64(hostname, ordererOrg.domain) @@ -82,11 +84,7 @@ export default class Orderer extends AbstractService { serverTlsCert: serverCertBase64, }) }) - - this.bdkFile.createConfigtx(configtxYaml) this.bdkFile.createOrdererOrgConsenter(ordererOrg.name, JSON.stringify(ordererOrgConsenter)) - - await (new Channel(this.config, this.infra)).createNewOrgConfigTx(ordererOrg.name) } } diff --git a/src/service/org.ts b/src/service/org.ts index c06093cf..37f55dda 100644 --- a/src/service/org.ts +++ b/src/service/org.ts @@ -1,6 +1,8 @@ import { logger } from '../util' import { OrgJsonType } from '../model/type/org.type' import { AbstractService } from './Service.abstract' +import FabricTools from '../instance/fabricTools' +import ConfigtxYaml from '../model/yaml/network/configtx' export default class Org extends AbstractService { /** @@ -27,4 +29,19 @@ export default class Org extends AbstractService { this.bdkFile.createExportOrgConfigJson(exportPeerOrgJson, path) } + + /** + * @description 由 config.yaml 建立 peer org 的 json 檔案 + * @param orgName - peer org 的名稱 + * @returns 在 blockchain network 資料夾底下 org-json/[peer org 名稱].json 檔案 + */ + // create new org configtx yaml + public async createNewOrgConfigTx (orgName: string, configtxYaml: ConfigtxYaml) { + logger.info(`[*] Generate ${orgName} config json file: configtxgen ${this.config.infraConfig.bdkPath}/${this.config.networkName}/org-json/${orgName}.json`) + + this.bdkFile.createConfigtx(configtxYaml) + const orgJson = (await (new FabricTools(this.config, this.infra)).createNewOrgConfigTx(orgName)).stdout.match(/{.*}/s)?.[0] || '' + + this.bdkFile.createOrgConfigJson(orgName, orgJson) + } } diff --git a/src/service/peer.ts b/src/service/peer.ts index 717583fe..f27ddae0 100644 --- a/src/service/peer.ts +++ b/src/service/peer.ts @@ -14,6 +14,7 @@ import { PeerUpType, PeerDownType, PeerAddType, PeerAddOrgToChannelType, PeerApp import { InfraRunnerResultType } from '../instance/infra/InfraRunner.interface' import { OrgPeerCreateType } from '../model/type/org.type' import { AbstractService } from './Service.abstract' +import Org from './org' export default class Peer extends AbstractService { /** @@ -63,16 +64,14 @@ export default class Peer extends AbstractService { for (const peerOrg of peerOrgs) { logger.info(`[*] Peer create configtx: ${peerOrg.name}`) - configtxYaml.addPeerOrg({ + const newOrg = configtxYaml.addPeerOrg({ name: peerOrg.name, mspDir: `${this.config.infraConfig.dockerPath}/peerOrganizations/${peerOrg.domain}/msp`, domain: peerOrg.domain, anchorPeers: [{ hostname: `${this.config.hostname}.${this.config.orgDomainName}`, port: peerOrg?.ports?.[+(this.config.hostname.slice(4, 0))]?.port }], }) - - this.bdkFile.createConfigtx(configtxYaml) - - await (new Channel(this.config, this.infra)).createNewOrgConfigTx(peerOrg.name) + this.bdkFile.createConfigtxPeerOrg(newOrg) + await (new Org(this.config, this.infra)).createNewOrgConfigTx(peerOrg.name, configtxYaml) } } diff --git a/test/service/network.test.ts b/test/service/network.test.ts index 4d518284..19aa0733 100644 --- a/test/service/network.test.ts +++ b/test/service/network.test.ts @@ -35,7 +35,11 @@ // await network.create(networkCreateDto) // let files_should_exist = [ // 'config-yaml/configtx.yaml', -// 'config-yaml/configtxOrgs.json', +// 'config-yaml/orgs/peer-Ben.json', +// 'config-yaml/orgs/peer-Grace.json', +// 'config-yaml/orgs/peer-Eugene.json', +// 'config-yaml/orgs/orderer-BenOrderer.json', +// 'config-yaml/orgs/orderer-GraceOrderer.json', // 'config-yaml/crypto-config.yaml', // 'docker-compose/docker-compose-orderer-orderer0.ben.cathaybc.com.yaml', // 'docker-compose/docker-compose-orderer-orderer1.ben.cathaybc.com.yaml', From 7316c358f7d66cfcb3d5d2a4efd7f6d0c904b437 Mon Sep 17 00:00:00 2001 From: kth-tw <88102592+kth-tw@users.noreply.github.com> Date: Tue, 7 Dec 2021 11:56:34 +0800 Subject: [PATCH 2/3] rename 'orgConfigTx' to 'OrgDefinitionJson' --- src/instance/bdkFile.ts | 6 +++--- src/instance/fabricTools.ts | 2 +- src/service/orderer.ts | 4 ++-- src/service/org.ts | 10 +++++----- src/service/peer.ts | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/instance/bdkFile.ts b/src/instance/bdkFile.ts index 53176e3e..d9539252 100644 --- a/src/instance/bdkFile.ts +++ b/src/instance/bdkFile.ts @@ -205,17 +205,17 @@ export default class BdkFile { fs.mkdirSync(`${this.bdkPath}/chaincode`, { recursive: true }) } - public createOrgConfigJson (name: string, orgJson: string) { + public createOrgDefinitionJson (name: string, orgJson: string) { fs.mkdirSync(`${this.bdkPath}/org-json`, { recursive: true }) fs.writeFileSync(`${this.bdkPath}/org-json/${name}.json`, orgJson) } - public createOrdererOrgConsenter (name: string, consenterJson: string) { + public createOrdererOrgConsenterJson (name: string, consenterJson: string) { fs.mkdirSync(`${this.bdkPath}/org-json`, { recursive: true }) fs.writeFileSync(`${this.bdkPath}/org-json/${name}-consenter.json`, consenterJson) } - public createExportOrgConfigJson (exportOrgJson: OrgJsonType, file: string) { + public createExportOrgDefinitionJson (exportOrgJson: OrgJsonType, file: string) { const splitFilePath = file.split('/', 3) for (let i = 0; i < splitFilePath.length - 1; i++) { fs.mkdirSync(`${splitFilePath[i]}`, { recursive: true }) diff --git a/src/instance/fabricTools.ts b/src/instance/fabricTools.ts index dea419df..462e1f1a 100644 --- a/src/instance/fabricTools.ts +++ b/src/instance/fabricTools.ts @@ -126,7 +126,7 @@ export default class FabricTools extends AbstractInstance { options) } - public async createNewOrgConfigTx (orgName: string, options?: OptionsType): Promise { + public async printOrgDefinitionJson (orgName: string, options?: OptionsType): Promise { const result = await this.infraRunCommand([ 'configtxgen', '-printOrg', `${orgName}`, diff --git a/src/service/orderer.ts b/src/service/orderer.ts index d956118a..ed7d5e11 100644 --- a/src/service/orderer.ts +++ b/src/service/orderer.ts @@ -71,7 +71,7 @@ export default class Orderer extends AbstractService { ports, }) this.bdkFile.createConfigtxOrdererOrg(newOrg) - await (new Org(this.config, this.infra)).createNewOrgConfigTx(ordererOrg.name, configtxYaml) + await (new Org(this.config, this.infra)).createOrgDefinitionJson(ordererOrg.name, configtxYaml) const ordererOrgConsenter: ConsenterType[] = [] ordererOrg.hostname.forEach((hostname, index) => { @@ -84,7 +84,7 @@ export default class Orderer extends AbstractService { serverTlsCert: serverCertBase64, }) }) - this.bdkFile.createOrdererOrgConsenter(ordererOrg.name, JSON.stringify(ordererOrgConsenter)) + this.bdkFile.createOrdererOrgConsenterJson(ordererOrg.name, JSON.stringify(ordererOrgConsenter)) } } diff --git a/src/service/org.ts b/src/service/org.ts index 37f55dda..7e6a5865 100644 --- a/src/service/org.ts +++ b/src/service/org.ts @@ -12,7 +12,7 @@ export default class Org extends AbstractService { data.forEach(org => { logger.info(`[*] Import org config: ${org.name}`) - this.bdkFile.createOrgConfigJson(org.name, org.json) + this.bdkFile.createOrgDefinitionJson(org.name, org.json) }) } @@ -27,7 +27,7 @@ export default class Org extends AbstractService { json: orgJson, } - this.bdkFile.createExportOrgConfigJson(exportPeerOrgJson, path) + this.bdkFile.createExportOrgDefinitionJson(exportPeerOrgJson, path) } /** @@ -36,12 +36,12 @@ export default class Org extends AbstractService { * @returns 在 blockchain network 資料夾底下 org-json/[peer org 名稱].json 檔案 */ // create new org configtx yaml - public async createNewOrgConfigTx (orgName: string, configtxYaml: ConfigtxYaml) { + public async createOrgDefinitionJson (orgName: string, configtxYaml: ConfigtxYaml) { logger.info(`[*] Generate ${orgName} config json file: configtxgen ${this.config.infraConfig.bdkPath}/${this.config.networkName}/org-json/${orgName}.json`) this.bdkFile.createConfigtx(configtxYaml) - const orgJson = (await (new FabricTools(this.config, this.infra)).createNewOrgConfigTx(orgName)).stdout.match(/{.*}/s)?.[0] || '' + const orgJson = (await (new FabricTools(this.config, this.infra)).printOrgDefinitionJson(orgName)).stdout.match(/{.*}/s)?.[0] || '' - this.bdkFile.createOrgConfigJson(orgName, orgJson) + this.bdkFile.createOrgDefinitionJson(orgName, orgJson) } } diff --git a/src/service/peer.ts b/src/service/peer.ts index f27ddae0..8b20901a 100644 --- a/src/service/peer.ts +++ b/src/service/peer.ts @@ -71,7 +71,7 @@ export default class Peer extends AbstractService { anchorPeers: [{ hostname: `${this.config.hostname}.${this.config.orgDomainName}`, port: peerOrg?.ports?.[+(this.config.hostname.slice(4, 0))]?.port }], }) this.bdkFile.createConfigtxPeerOrg(newOrg) - await (new Org(this.config, this.infra)).createNewOrgConfigTx(peerOrg.name, configtxYaml) + await (new Org(this.config, this.infra)).createOrgDefinitionJson(peerOrg.name, configtxYaml) } } From 940840cd43cde82cfd8ddf8f2859de8f7f8215ec Mon Sep 17 00:00:00 2001 From: kth-tw <88102592+kth-tw@users.noreply.github.com> Date: Tue, 7 Dec 2021 14:26:59 +0800 Subject: [PATCH 3/3] beautify regex --- src/instance/bdkFile.ts | 10 ++++++---- src/model/prompts/util.ts | 7 ++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/instance/bdkFile.ts b/src/instance/bdkFile.ts index d9539252..0166ddb6 100644 --- a/src/instance/bdkFile.ts +++ b/src/instance/bdkFile.ts @@ -105,10 +105,12 @@ export default class BdkFile { peerOrgs: {}, } fs.readdirSync(`${this.bdkPath}/config-yaml/orgs`).forEach((filename: string) => { - if (/^orderer-.*\.json$/.test(filename)) { - configtxOrgs.ordererOrgs[filename.replace(/^orderer-/, '').replace(/\.json$/, '')] = JSON.parse(fs.readFileSync(`${this.bdkPath}/config-yaml/orgs/${filename}`).toString()) - } else if (/^peer-.*\.json$/.test(filename)) { - configtxOrgs.peerOrgs[filename.replace(/^peer-/, '').replace(/\.json$/, '')] = JSON.parse(fs.readFileSync(`${this.bdkPath}/config-yaml/orgs/${filename}`).toString()) + const peerOrg = filename.match(/(?<=^peer-).*(?=\.json$)/)?.[0] + const ordererOrg = filename.match(/(?<=^orderer-).*(?=\.json$)/)?.[0] + if (ordererOrg) { + configtxOrgs.ordererOrgs[ordererOrg] = JSON.parse(fs.readFileSync(`${this.bdkPath}/config-yaml/orgs/${filename}`).toString()) + } else if (peerOrg) { + configtxOrgs.peerOrgs[peerOrg] = JSON.parse(fs.readFileSync(`${this.bdkPath}/config-yaml/orgs/${filename}`).toString()) } }, ) diff --git a/src/model/prompts/util.ts b/src/model/prompts/util.ts index 8cd33e8d..3c49835a 100644 --- a/src/model/prompts/util.ts +++ b/src/model/prompts/util.ts @@ -67,7 +67,12 @@ export const getChannelList = (config: Config): string[] => { export const getOrgNames = (config: Config): string[] => { try { const hostBasePath = `${config.infraConfig.bdkPath}/${config.networkName}` - return fs.readdirSync(`${hostBasePath}/config-yaml/orgs`).filter(x => /^peer-.*\.json$/.test(x)).map(x => x.replace(/^peer-/, '').replace(/\.json$/, '')) + const orgs: string[] = [] + fs.readdirSync(`${hostBasePath}/config-yaml/orgs`).forEach(fileName => { + const org = fileName.match(/(?<=^peer-).*(?=\.json$)/)?.[0] + org && orgs.push(org) + }) + return orgs } catch { return [] }