Skip to content

Commit 23c82cd

Browse files
committed
inital fablo init
Signed-off-by: OsamaRab3 <osrab3@gmail.com>
1 parent 79cd27a commit 23c82cd

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed

src/commands/init/index.ts

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
import { Args, Command } from '@oclif/core'
2+
import * as chalk from "chalk";
3+
import { GlobalJson, FabloConfigJson, ChaincodeJson } from "../../types/FabloConfigJson";
4+
import * as path from 'path';
5+
import * as fs from 'fs-extra';
6+
import { version } from "../../../package.json";
7+
import { printSplash } from '../../fablolog';
8+
9+
function getDefaultFabloConfig(): FabloConfigJson {
10+
return {
11+
$schema: `https://github.com/hyperledger-labs/fablo/releases/download/${version}/schema.json`,
12+
global: {
13+
fabricVersion: "3.1.0",
14+
tls: true,
15+
peerDevMode: false,
16+
},
17+
orgs: [
18+
{
19+
organization: {
20+
name: "Orderer",
21+
domain: "orderer.example.com",
22+
mspName: "OrdererMSP",
23+
},
24+
ca: {
25+
prefix: "ca",
26+
db: "sqlite",
27+
},
28+
orderers: [
29+
{
30+
groupName: "group1",
31+
type: "BFT",
32+
instances: 2,
33+
prefix: "orderer",
34+
},
35+
],
36+
},
37+
{
38+
organization: {
39+
name: "Org1",
40+
domain: "org1.example.com",
41+
mspName: "Org1MSP",
42+
},
43+
ca: {
44+
prefix: "ca",
45+
db: "sqlite",
46+
},
47+
orderers: [],
48+
peer: {
49+
instances: 2,
50+
db: "LevelDb",
51+
prefix: "peer",
52+
},
53+
},
54+
],
55+
channels: [
56+
{
57+
name: "my-channel1",
58+
orgs: [
59+
{
60+
name: "Org1",
61+
peers: ["peer0", "peer1"],
62+
},
63+
],
64+
},
65+
],
66+
chaincodes: [],
67+
hooks: {},
68+
};
69+
}
70+
71+
export default class Init extends Command {
72+
static override description =
73+
"Creates simple Fablo config in current directory with optional Node.js, chaincode, REST API and dev mode";
74+
75+
static args = {
76+
feat: Args.string({
77+
description: 'feature node,dev,ccass,gateway,rest',
78+
multiple: true
79+
})
80+
}
81+
async copySampleConfig(): Promise<void> {
82+
let fabloConfigJson = getDefaultFabloConfig();
83+
const { args } = await this.parse(Init);
84+
85+
86+
const features: string[] = Array.isArray(args.feat) ? args.feat : args.feat ? [args.feat] : [];
87+
88+
const flags: Record<string, true> = features.reduce<Record<string, true>>((acc, v) => {
89+
acc[v] = true;
90+
return acc;
91+
}, {});
92+
93+
if (flags.ccaas) {
94+
if (flags.dev || flags.node) {
95+
this.log(chalk.red("Error: --ccaas flag cannot be used together with --dev or --node flags"));
96+
process.exit(1);
97+
}
98+
this.log("Creating sample CCAAS chaincode");
99+
printSplash();
100+
101+
const chaincodeConfig: ChaincodeJson = {
102+
name: "chaincode1",
103+
version: "0.0.1",
104+
channel: "my-channel1",
105+
lang: "ccaas",
106+
image: "ghcr.io/fablo-io/fablo-sample-kv-node-chaincode:2.2.0",
107+
privateData: [],
108+
};
109+
fabloConfigJson = {
110+
...fabloConfigJson,
111+
chaincodes: [...fabloConfigJson.chaincodes, chaincodeConfig],
112+
};
113+
}
114+
if (flags.node) {
115+
console.log("Creating sample Node.js chaincode");
116+
printSplash();
117+
const source = path.join(__dirname, '../../../../samples/chaincodes/chaincode-kv-node');
118+
const destination = path.join(process.cwd(), 'chaincodes');
119+
fs.copySync(source, destination);
120+
121+
// fs.copySync(source, path.join(process.cwd(), 'chaincodes'));
122+
fs.writeFileSync(path.join(process.cwd(), 'samples/chaincodes/chaincode-kv-node/.nvmrc'), '12');
123+
124+
// force build on Node 12, since dev deps (@theledger/fabric-mock-stub) may not work on 16
125+
// fs.write(destination("chaincodes/chaincode-kv-node/.nvmrc"), "12");
126+
127+
const chaincodeConfig: ChaincodeJson = flags.dev
128+
? {
129+
name: "chaincode1",
130+
version: "0.0.1",
131+
channel: "my-channel1",
132+
lang: "ccaas",
133+
image: "hyperledger/fabric-nodeenv:${FABRIC_NODEENV_VERSION:-2.5}",
134+
chaincodeMountPath: "$CHAINCODES_BASE_DIR/chaincodes/chaincode-kv-node",
135+
chaincodeStartCommand: "npm run start:watch:ccaas",
136+
privateData: [],
137+
}
138+
: {
139+
name: "chaincode1",
140+
version: "0.0.1",
141+
channel: "my-channel1",
142+
lang: "node",
143+
directory: "./chaincodes/chaincode-kv-node",
144+
privateData: [],
145+
};
146+
147+
const postGenerateHook = flags.dev ? { postGenerate: "npm i --prefix ./chaincodes/chaincode-kv-node" } : {};
148+
149+
fabloConfigJson = {
150+
...fabloConfigJson,
151+
chaincodes: [...fabloConfigJson.chaincodes, chaincodeConfig],
152+
hooks: { ...fabloConfigJson.hooks, ...postGenerateHook },
153+
};
154+
}
155+
156+
if (flags.gateway) {
157+
console.log("Creating sample Node.js gateway");
158+
printSplash();
159+
const src = path.join(__dirname, '../../../../samples/gateway');
160+
const dest = path.join(process.cwd(), 'gateway');
161+
fs.copySync(src, dest);
162+
this.log('✔ Gateway generated successfully!');
163+
}
164+
165+
if (flags.rest) {
166+
const orgs = fabloConfigJson.orgs.map((org) => ({ ...org, tools: { fabloRest: true } }));
167+
fabloConfigJson = { ...fabloConfigJson, orgs };
168+
}
169+
170+
const engine = flags.kubernetes || flags.k8s ? "kubernetes" : "docker";
171+
172+
const global: GlobalJson = {
173+
...fabloConfigJson.global,
174+
engine,
175+
};
176+
fabloConfigJson = { ...fabloConfigJson, global };
177+
const rootPath = process.cwd();
178+
const outputFile = path.join(rootPath, 'fablo-config.json');
179+
// fs.write(this.destinationPath("fablo-config.json"), JSON.stringify(fabloConfigJson, undefined, 2));
180+
fs.writeFileSync(outputFile, JSON.stringify(fabloConfigJson, null, 2));
181+
182+
this.log("===========================================================");
183+
this.log(chalk.bold("Sample config file created! :)"));
184+
this.log("You can start your network with 'fablo up' command");
185+
this.log("===========================================================");
186+
187+
}
188+
189+
public async run(): Promise<void> {
190+
191+
await this.copySampleConfig();
192+
}
193+
}

0 commit comments

Comments
 (0)