-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.js
More file actions
196 lines (173 loc) · 5.65 KB
/
dev.js
File metadata and controls
196 lines (173 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import child_process from 'child_process';
import arg from 'arg';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
import * as enrollAdmin from '../tools/admins/dist/enrollAdmin.js';
import * as bootstrap from './bootstrap.js';
/**
* __filename and __dirname are not defined in NPM modules on Node 15.
* We construct them manually. We may use babel later, which populate those variables correclty.
*/
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
/**
* Vercel provide us with a function for parsing node program execution arguments.
*/
const args = arg({
// Types
'--skip-network': Boolean,
'--run-network': Boolean,
'--skip-contracts': Boolean,
'--run-contracts': Boolean,
'--skip-bootstrap': Boolean,
'--run-bootstrap': Boolean,
'--skip-nerves': Boolean,
'--run-nerves': Boolean,
'--skip-webapp': Boolean,
'--run-webapp': Boolean,
'--skip-all': Boolean,
'--verbose': Boolean,
'--config-file': String,
// Aliases
'-v': '--verbose',
});
/**
* They determine the execution path of the related programs.
*/
const CWD_SCRIPTS = __dirname;
const CWD_NERVES = path.join(__dirname, '../nerves');
const CWD_WEBAPP = path.join(__dirname, '../webapp/webbapp-react');
const BLOCKOTUS = path.join(__dirname, '../');
/**
* Those environment variables are used by Hyperledger Fabric, Nerves, and Webapp, and are passed to the node child process.
*/
const ENV = {
BLOCKOTUS,
PATH: `${BLOCKOTUS}network/bin:${process.env.PATH}`,
FABRIC_CFG_PATH: `${BLOCKOTUS}network/config`,
FABRIC_LOGGING_SPEC: `DEBUG`,
NERVES_PORT: 3001,
CORE_PEER_TLS_ENABLED: `true`,
CORE_PEER_LOCALMSPID: `Org1MSP`,
CORE_PEER_TLS_ROOTCERT_FILE: `${BLOCKOTUS}network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt`,
CORE_PEER_MSPCONFIGPATH: `${BLOCKOTUS}network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp`,
CORE_PEER_ADDRESS: `localhost:7051`,
};
const WEBAPP_PORT = 3000;
/**
* When not in verbose, we do not show any program output, except for errors.
*/
const STDIO = args['--verbose']
? ['inherit', 'inherit', 'inherit']
: ['ignore', 'ignore', 'inherit'];
/**
* Hyperledger send all the output on the error canal, which is annoying.
* In this case, we ingore all outputs.
*/
const STDIO_NETWORK_CONTRACTS = args['--verbose']
? ['inherit', 'inherit', 'inherit']
: ['ignore', 'ignore', 'ignore'];
/**
* Console.log function, only active if --verbose argument is set.
*/
const verbose = log => args['--verbose'] && console.log(log);
/**
* Serialize an array of promise. It result in a one by one sorted chain of promise execution.
*/
const serial = funcs =>
funcs.reduce((promise, func) =>
typeof func === 'function'
? promise.then(result => func().then(Array.prototype.concat.bind(result)))
: null
, Promise.resolve([]));
/**
* Start Hyperledger Fabric network executables.
*/
const network = () => {
console.log('***** starting network *****');
child_process.execSync(
'./startNetwork.sh',
{ stdio: STDIO_NETWORK_CONTRACTS, cwd: CWD_SCRIPTS, env: ENV }
);
verbose('done.');
}
/**
* Compile and deploy chaincode contracts to the network.
*/
const contracts = async () => {
console.log('***** deploying contracts *****');
const CONFIG = await import(args['--config-file'] || '../.blockotus.js');
child_process.execSync(
'rm -rf ./versions/*',
{ stdio: STDIO, cwd: CWD_SCRIPTS, env: ENV }
);
const promisesContracts = CONFIG.default.organs.map(o => {
return new Promise((resolve) => {
try{
o.lang === 'typescript' && child_process.execSync(
`rm -rf ../organs/${o.name}/chaincode/typescript/dist/*`,
{ stdio: STDIO_NETWORK_CONTRACTS, cwd: CWD_SCRIPTS, env: ENV }
);
} catch (e) { console.log('ERROR deleting typescript dist', e) }
try{
child_process.execSync(
`./startFabric.sh ${o.name} ${o.lang}`,
{ stdio: STDIO_NETWORK_CONTRACTS, cwd: CWD_SCRIPTS, env: ENV }
);
} catch (e) { console.log('ERROR with ./startFabric', e) }
resolve();
});
});
await serial(promisesContracts);
verbose('done.');
}
/**
* Enroll an admin.
* Create indexes on the CouchDB database.
* Create a bunch of users and test primary functions.
*/
const boot = async () => {
try {
await enrollAdmin.main();
} catch (e) { }
console.log('***** booting in 5 seconds... *****');
return new Promise((resolve) => {
setTimeout(async () => {
await bootstrap.start();
resolve();
}, 5000);
});
}
/**
* Start the API.
*/
const nerves = () => {
console.log('***** starting nerves *****');
child_process.execSync(
'yarn start &',
{ stdio: STDIO, cwd: CWD_NERVES, env: ENV }
);
verbose('done.');
}
/**
* Start the webapp.
*/
const webapp = () => {
console.log('***** starting webapp *****');
child_process.exec(
'yarn start',
{ stdio: STDIO, cwd: CWD_WEBAPP, env: { ...ENV, PORT: WEBAPP_PORT } },
);
console.log(`Webapp listening on PORT: ${WEBAPP_PORT}`);
verbose('done.');
}
const dev = async () => {
!args['--skip-network'] && (!args['--skip-all'] || args['--run-network']) && network();
!args['--skip-contracts'] && (!args['--skip-all'] || args['--run-contracts']) && await contracts();
!args['--skip-bootstrap'] && (!args['--skip-all'] || args['--run-bootstrap']) && await boot();
!args['--skip-nerves'] && (!args['--skip-all'] || args['--run-nerves']) && nerves();
!args['--skip-webapp'] && (!args['--skip-all'] || args['--run-webapp']) && webapp();
console.log('Organism running.');
verbose('You rock baby.');
}
dev();