11import * as Generator from "yeoman-generator" ;
22import * as chalk from "chalk" ;
3- import { GlobalJson , FabloConfigJson } from "../types/FabloConfigJson" ;
3+ import { GlobalJson , FabloConfigJson , OrgJson } from "../types/FabloConfigJson" ;
44
55const DEFAULT_FABLO_CONFIG : FabloConfigJson = {
6- " $schema" : "https://github.com/hyperledger-labs/fablo/releases/download/2.2.0/schema.json" ,
6+ $schema : "https://github.com/hyperledger-labs/fablo/releases/download/2.2.0/schema.json" ,
77 global : {
8- fabricVersion : "2.5.9" ,
9- tls : false
8+ fabricVersion : "2.3.3" ,
9+ tls : true ,
10+ peerDevMode : false ,
11+ tools : {
12+ explorer : true ,
13+ } ,
1014 } ,
1115 orgs : [
1216 {
1317 organization : {
1418 name : "Orderer" ,
15- domain : "orderer.example.com"
19+ domain : "orderer.example.com" ,
20+ mspName : "OrdererMSP" ,
21+ } ,
22+ ca : {
23+ prefix : "ca" ,
24+ db : "postgres" ,
1625 } ,
1726 orderers : [
1827 {
1928 groupName : "group1" ,
20- type : "solo" ,
21- instances : 1
22- }
23- ]
29+ prefix : "orderer" ,
30+ type : "raft" ,
31+ instances : 1 ,
32+ } ,
33+ ] ,
2434 } ,
2535 {
2636 organization : {
2737 name : "Org1" ,
28- domain : "org1.example.com"
38+ domain : "org1.example.com" ,
39+ mspName : "Org1MSP" ,
40+ } ,
41+ ca : {
42+ prefix : "ca" ,
43+ db : "postgres" ,
2944 } ,
45+ orderers : [ ] ,
3046 peer : {
31- instances : 2 ,
32- db : "LevelDb"
33- }
34- }
47+ instances : 1 ,
48+ prefix : "peer" ,
49+ db : "CouchDb" ,
50+ } ,
51+ tools : {
52+ fabloRest : true ,
53+ } ,
54+ } ,
3555 ] ,
3656 channels : [
3757 {
3858 name : "my-channel1" ,
3959 orgs : [
4060 {
4161 name : "Org1" ,
42- peers : [
43- "peer0" ,
44- "peer1"
45- ]
46- }
47- ]
48- }
62+ peers : [ "peer0" ] ,
63+ } ,
64+ ] ,
65+ } ,
4966 ] ,
5067 chaincodes : [
5168 {
5269 name : "chaincode1" ,
5370 version : "0.0.1" ,
5471 lang : "node" ,
5572 channel : "my-channel1" ,
56- directory : "./chaincodes/chaincode-kv-node"
57- }
58- ]
73+ directory : "./chaincodes/chaincode-kv-node" ,
74+ privateData : [ ] ,
75+ } ,
76+ ] ,
77+ hooks : {
78+ postGenerate :
79+ "perl -i -pe 's/MaxMessageCount: 10/MaxMessageCount: 1/g' \"./fablo-target/fabric-config/configtx.yaml\"" ,
80+ } ,
5981} ;
6082
6183export default class InitGenerator extends Generator {
6284 constructor ( readonly args : string [ ] , opts : Generator . GeneratorOptions ) {
6385 super ( args , opts ) ;
6486 }
6587
88+ private hasArg ( arg : string ) : boolean {
89+ return this . args . includes ( arg ) ;
90+ }
91+
6692 async copySampleConfig ( ) : Promise < void > {
67- let fabloConfigJson = { ...DEFAULT_FABLO_CONFIG } ;
68-
69- const shouldInitWithNodeChaincode = this . args . length && this . args . find ( ( v ) => v === "node" ) ;
70- if ( shouldInitWithNodeChaincode ) {
71- console . log ( "Creating sample Node.js chaincode" ) ;
72- this . fs . copy ( this . templatePath ( "chaincodes" ) , this . destinationPath ( "chaincodes" ) ) ;
73- // force build on Node 12, since dev deps (@theledger/fabric-mock-stub) may not work on 16
74- this . fs . write ( this . destinationPath ( "chaincodes/chaincode-kv-node/.nvmrc" ) , "12" ) ;
75- } else {
76- fabloConfigJson = { ...fabloConfigJson , chaincodes : [ ] } ;
77- }
78-
79- const shouldAddFabloRest = this . args . length && this . args . find ( ( v ) => v === "rest" ) ;
80- if ( shouldAddFabloRest ) {
81- const orgs = fabloConfigJson . orgs . map ( ( org : any ) => ( { ...org , tools : { fabloRest : true } } ) ) ;
82- fabloConfigJson = { ...fabloConfigJson , orgs } ;
83- } else {
84- const orgs = fabloConfigJson . orgs . map ( ( org : any ) => ( { ...org , tools : { } } ) ) ;
93+ try {
94+ let fabloConfigJson : FabloConfigJson = { ...DEFAULT_FABLO_CONFIG } ;
95+
96+ const shouldInitWithNodeChaincode = this . hasArg ( "node" ) ;
97+ if ( shouldInitWithNodeChaincode ) {
98+ console . log ( "Creating sample Node.js chaincode" ) ;
99+ this . fs . copy ( this . templatePath ( "chaincodes" ) , this . destinationPath ( "chaincodes" ) ) ;
100+ this . fs . write ( this . destinationPath ( "chaincodes/chaincode-kv-node/.nvmrc" ) , "12" ) ;
101+ } else {
102+ fabloConfigJson = { ...fabloConfigJson , chaincodes : [ ] } ;
103+ }
104+
105+ const shouldAddFabloRest = this . hasArg ( "rest" ) ;
106+ const orgs = fabloConfigJson . orgs . map ( ( org : OrgJson ) => ( {
107+ ...org ,
108+ tools : shouldAddFabloRest ? { fabloRest : true } : { } ,
109+ } ) ) ;
85110 fabloConfigJson = { ...fabloConfigJson , orgs } ;
111+
112+ const shouldUseKubernetes = this . hasArg ( "kubernetes" ) || this . hasArg ( "k8s" ) ;
113+ const shouldRunInDevMode = this . hasArg ( "dev" ) ;
114+ const global : GlobalJson = {
115+ ...fabloConfigJson . global ,
116+ engine : shouldUseKubernetes ? "kubernetes" : "docker" ,
117+ peerDevMode : shouldRunInDevMode ,
118+ } ;
119+ fabloConfigJson = { ...fabloConfigJson , global } ;
120+
121+ this . fs . write ( this . destinationPath ( "fablo-config.json" ) , JSON . stringify ( fabloConfigJson , undefined , 2 ) ) ;
122+
123+ this . on ( "end" , ( ) => {
124+ console . log ( "===========================================================" ) ;
125+ console . log ( chalk . bold ( "Sample config file created! :)" ) ) ;
126+ console . log ( "You can start your network with 'fablo up' command" ) ;
127+ console . log ( "===========================================================" ) ;
128+ } ) ;
129+ } catch ( error ) {
130+ console . error ( chalk . red ( "Error creating configuration:" ) , error ) ;
131+ throw error ;
86132 }
87-
88- const shouldUseKubernetes = this . args . length && this . args . find ( ( v ) => v === "kubernetes" || v === "k8s" ) ;
89- const shouldRunInDevMode = this . args . length && this . args . find ( ( v ) => v === "dev" ) ;
90- const global : GlobalJson = {
91- ...fabloConfigJson . global ,
92- engine : shouldUseKubernetes ? "kubernetes" : "docker" ,
93- peerDevMode : ! ! shouldRunInDevMode ,
94- } ;
95- fabloConfigJson = { ...fabloConfigJson , global } ;
96-
97- this . fs . write ( this . destinationPath ( "fablo-config.json" ) , JSON . stringify ( fabloConfigJson , undefined , 2 ) ) ;
98-
99- this . on ( "end" , ( ) => {
100- console . log ( "===========================================================" ) ;
101- console . log ( chalk . bold ( "Sample config file created! :)" ) ) ;
102- console . log ( "You can start your network with 'fablo up' command" ) ;
103- console . log ( "===========================================================" ) ;
104- } ) ;
105133 }
106- }
134+ }
0 commit comments