Skip to content

Commit aa9550e

Browse files
authored
Merge pull request #25 from PortalNetwork/refactor-dev
Refactor dev
2 parents 7ded26b + c4209dd commit aa9550e

File tree

14 files changed

+206
-183
lines changed

14 files changed

+206
-183
lines changed

docs/tutorial.rst

+22
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,25 @@ __
5555
Yout can use object spread syntax to replace them into your components, or just use them like a normal object.
5656

5757
.. image:: https://user-images.githubusercontent.com/11625554/45738864-17445980-bc24-11e8-912b-eedf4a97b3c6.png
58+
59+
----------------
60+
``kaizen build``
61+
----------------
62+
63+
To build the kaizen's dapp, and the built code will be output into the build folder
64+
65+
---------------
66+
``kaizen init``
67+
---------------
68+
69+
To setup the configuration you need, like IPFS provider.
70+
71+
----------------
72+
``kaizen publish``
73+
----------------
74+
75+
To upload your dapp to the IPFS. You shall execute `kaizen build` before publish
76+
77+
78+
79+

kaizen

+20-47
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
const clear = require('clear');
44
const chalk = require('chalk');
55
const figlet = require('figlet');
6-
const newDapp = require("./lib/NewDapp");
7-
const {
8-
add: pluginAdd,
9-
build: build,
10-
ipfsPublish: ipfsPublish,
11-
init: init
12-
} = require('./lib/plugin');
6+
const plugin = require('./lib/plugin');
7+
const initialization = require('./lib/initialization');
8+
const build = require('./lib/build');
9+
const ipfs = require('./lib/ipfs');
10+
const kaizenNew = require('./lib/new');
1311

1412
// to clear terminal screen
1513
clear();
@@ -27,45 +25,11 @@ console.log(
2725

2826
const argv = require('yargs')
2927
.usage('Kaizen - a dapp framework for Blockchain\n\nUsage: $0 <command> [options]')
30-
.command('init', 'Initialization environment', (yargs) => {
31-
init.init();
32-
})
33-
.command('new', 'create a dapp with boilerplate', (yargs) => {
34-
return yargs
35-
.option('n', {
36-
alias: 'name',
37-
describe: 'clone a file'
38-
})
39-
.option('b', {
40-
alias: 'boilerplate',
41-
describe: 'dapp boilerplate of vue or react',
42-
choices: ['vue', 'react'],
43-
default: 'vue'
44-
})
45-
.demandOption(['name'], 'Please enter your project name')
46-
.example(
47-
'kaizen new -n <package name> -b <vue or react>'
48-
)
49-
}, (argv) => {
50-
const { name, boilerplate } = argv;
51-
newDapp({ name, boilerplate });
52-
})
53-
.command('config', 'environment variables of dapps')
54-
.command('plugin', 'list all the plugin to dapp')
55-
.command('plugin add [plugin-name]', pluginAdd.description, (yargs) => pluginAdd.yargs(yargs), (argv) => pluginAdd.argv(argv))
56-
.command('plugin:remove', 'remove an plugin to dapp', (yargs) => {
57-
return yargs
58-
.demandOption(['name'], 'Please enter a plugin name')
59-
.example(
60-
'kaizen plugin:remove <plugin name>'
61-
)
62-
})
63-
.command('publish', 'scripts used to publish dapp', (yargs) => {
64-
ipfsPublish.ipfsPublish(yargs);
65-
})
66-
.command('build', 'build dapp package', (yargs) => {
67-
build.build(yargs);
68-
})
28+
.command('init', initialization.init.description, initialization.init.yargs, initialization.init.argv)
29+
.command('new', kaizenNew.create.description, kaizenNew.create.yargs, kaizenNew.create.argv)
30+
.command('plugin add [plugin-name]', plugin.add.description, plugin.add.yargs, plugin.add.argv)
31+
.command('publish', ipfs.publish.description, ipfs.publish.yargs)
32+
.command('build', build.description, build.yargs, build.argv)
6933
.demandCommand(1, 'You need at least one command before moving on')
7034
.options({
7135
'v': {
@@ -78,4 +42,13 @@ const argv = require('yargs')
7842
}
7943
})
8044
.epilog('copyright Portal Network 2018')
81-
.argv;
45+
.argv;
46+
47+
48+
49+
/**
50+
* TODO: kaizen new with choose blockchain e.g. ethereum, wanchain
51+
*/
52+
53+
54+

lib/plugin/Log.js lib/Log.js

File renamed without changes.

lib/initialization/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.init = require('./init.js');

lib/initialization/init.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const Log = require('../Log');
4+
const KAIZEN_CONFIG_FILE = "kaizen.json";
5+
6+
exports.description = 'initialize kaizen environment';
7+
8+
exports.yargs = function(yargs) {
9+
yargs.example('kaizen init');
10+
}
11+
12+
exports.argv = function (argv) {
13+
const targetPath = path.resolve('./', KAIZEN_CONFIG_FILE);
14+
15+
if(fs.existsSync(targetPath) === false) {
16+
console.error('[ERROR]: please use kaizen new to create new project first.');
17+
return;
18+
}
19+
20+
const sourcePath = path.resolve(__dirname, '../../config/', KAIZEN_CONFIG_FILE);
21+
const sourceConfig = JSON.parse(fs.readFileSync(sourcePath));
22+
const targetConfig = JSON.parse(fs.readFileSync(targetPath));
23+
const newConfig = Object.assign({}, targetConfig, sourceConfig);
24+
fs.writeFileSync(targetPath, JSON.stringify(newConfig));
25+
Log.SuccessLog("complete initialization");
26+
}

lib/ipfs/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.publish = require('./publish.js');

lib/ipfs/publish.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const Log = require('../Log');
4+
const ipfsAPI = require('ipfs-api');
5+
const JSONFile = require('jsonfile');
6+
const openBrowser = require('opn');
7+
const BuildPath = 'build';
8+
const kaizenfile = "kaizen.json";
9+
10+
function fsExistsSync() {
11+
try {
12+
fs.accessSync(BuildPath, fs.constants.R_OK | fs.constants.W_OK);
13+
return true;
14+
} catch (err) {
15+
return false;
16+
}
17+
}
18+
19+
function loopFilesInFolder(path, files) {
20+
const readdirSyncs = fs.readdirSync(path);
21+
readdirSyncs.forEach(item => {
22+
if (item.includes('.DS_Store')) return;
23+
switch (fs.statSync(`${path}/${item}`).isDirectory()) {
24+
case true:
25+
files = loopFilesInFolder(`${path}/${item}`, files);
26+
break;
27+
case false:
28+
files.push(`${path}/${item}`);
29+
break
30+
}
31+
});
32+
33+
return files;
34+
}
35+
36+
function getIPFSContentObject(filePath, targetPath) {
37+
return ({
38+
path: `public${filePath.replace(targetPath, '')}`,
39+
content: fs.readFileSync(filePath)
40+
});
41+
}
42+
43+
exports.description = 'publish you app to the IPFS';
44+
45+
exports.yargs = function (yargs) {
46+
if (!fsExistsSync()) {
47+
Log.ErrorLog("Build This folder does not exist");
48+
}
49+
50+
const targetPath = `${path.resolve('./', BuildPath)}`;
51+
52+
try {
53+
(async function(yargs, targetPath) {
54+
const kaizenConfig = await JSONFile.readFile(kaizenfile)
55+
const ipfs = ipfsAPI(kaizenConfig.provider);
56+
console.log('=== uploading to the IPFS ===')
57+
const files = loopFilesInFolder(targetPath, []).map(item => getIPFSContentObject(item, targetPath));
58+
const hashes = await ipfs.files.add(files, { recursive: false });
59+
const { hash, } = hashes[hashes.length - 1];
60+
const iphsUrl = `https://ipfs.infura.io/ipfs/${hash}`;
61+
openBrowser(iphsUrl);
62+
Log.SuccessLog(`ipfs url => ${iphsUrl}`)
63+
process.exit();
64+
})(yargs, targetPath);
65+
} catch (err) {
66+
Log.ErrorLog(err)
67+
}
68+
}
69+

lib/NewDapp.js lib/new/create.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
11
const cmd = require('node-cmd');
22
const fs = require("fs");
33
const del = require('del');
4-
const Log = require('./plugin/Log');
4+
const Log = require('../Log');
55

6-
module.exports = (argv) => {
6+
exports.description = 'create a dapp with boilerplate';
7+
8+
exports.yargs = function (yargs) {
9+
yargs
10+
.option('n', {
11+
alias: 'name',
12+
describe: 'clone a file'
13+
})
14+
.option('b', {
15+
alias: 'boilerplate',
16+
describe: 'dapp boilerplate of vue or react',
17+
choices: ['vue', 'react'],
18+
default: 'vue'
19+
})
20+
.demandOption(['name'], 'Please enter your project name')
21+
.example(
22+
'kaizen new -n <package name> -b <vue or react>'
23+
)
24+
}
25+
26+
exports.argv = function (argv) {
727
let f2eFramework = '';
828
let rename = '';
929
switch (argv.boilerplate) {
1030
case 'vue':
1131
f2eFramework = 'https://github.com/PortalNetwork/vue-truffle.git';
1232
rename = 'vue-truffle';
1333
break;
14-
1534
case 'react':
1635
f2eFramework = 'https://github.com/PortalNetwork/react-truffle-metamask.git';
1736
rename = 'react-truffle-metamask';

lib/new/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.create = require('./create.js');

lib/plugin/add.js

+43-16
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ const path = require('path');
33
const cmd = require('node-cmd');
44
const ncp = require('ncp').ncp;
55

6-
exports.description = 'attach an plugin to dapp';
6+
exports.description = 'attach a plugin to dapp';
77

88
exports.yargs = function (yargs) {
9-
return yargs
9+
yargs
1010
.option('b', {
1111
alias: 'boilerplate',
1212
describe: 'dapp boilerplate of vue or react',
@@ -19,12 +19,20 @@ exports.yargs = function (yargs) {
1919

2020
exports.argv = function (argv) {
2121
const { pluginName, boilerplate, } = argv;
22+
23+
const configPath = path.resolve('./', 'kaizen.json');
24+
if(fs.existsSync(configPath) === false) {
25+
console.log('[ERROR]: please use kaizen new to create new project first.')
26+
return;
27+
}
28+
29+
const kaizenConfig = JSON.parse(fs.readFileSync(configPath));
2230
switch (pluginName) {
2331
case 'noia':
24-
handleNOIA();
32+
handleNOIA(kaizenConfig);
2533
break;
2634
case 'bluzelle':
27-
handleBluzelle(boilerplate);
35+
handleBluzelle(kaizenConfig, boilerplate);
2836
break;
2937
}
3038
}
@@ -54,20 +62,21 @@ function handleError(error) {
5462
console.error(`[ERROR]: ${error.message}`);
5563
}
5664

57-
function reactMixBluzelle() {
65+
function reactMixBluzelle(kaizenConfig) {
5866
const sourceFolder = path.resolve(__dirname, '../../templates');
5967
const targetFolder = path.resolve('./', 'node_modules/pn-react-bluzelle');
60-
68+
6169
if (!fs.existsSync(targetFolder)) {
6270
fs.mkdirSync(targetFolder);
6371
}
6472

6573
const readStream = fs.createReadStream(path.resolve(sourceFolder, 'pn-react-bluzelle.js'));
6674
const writeStream = fs.createWriteStream(path.resolve(targetFolder, 'index.js'));
6775
readStream.pipe(writeStream);
76+
updateConfig(kaizenConfig, 'pn-react-bluzelle');
6877
}
6978

70-
function vueMixBluzelle() {
79+
function vueMixBluzelle(kaizenConfig) {
7180
const sourceFolder = path.resolve(__dirname, '../../templates');
7281
const targetFolder = path.resolve('./', 'node_modules/pn-vue-bluzelle');
7382

@@ -78,12 +87,26 @@ function vueMixBluzelle() {
7887
const readStream = fs.createReadStream(path.resolve(sourceFolder, 'pn-vue-bluzelle.js'));
7988
const writeStream = fs.createWriteStream(path.resolve(targetFolder, 'index.js'));
8089
readStream.pipe(writeStream);
90+
updateConfig(kaizenConfig, 'pn-vue-bluzelle');
91+
}
92+
93+
function updateConfig(kaizenConfig, plugin) {
94+
if(!kaizenConfig.plugins) {
95+
kaizenConfig.plugins = [];
96+
}
97+
98+
if(kaizenConfig.plugins.includes(plugin)) {
99+
console.log('sfsfd')
100+
return;
101+
}
102+
103+
kaizenConfig.plugins.push(plugin);
104+
fs.writeFileSync(path.resolve('./', 'kaizen.json'), JSON.stringify(kaizenConfig));
81105
}
82106

83-
function handleNOIA() {
107+
function handleNOIA(kaizenConfig) {
84108
try {
85-
const package = JSON.parse(fs.readFileSync('./kaizen.json', 'utf8'));
86-
const checkResult = packageChecker(package);
109+
const checkResult = packageChecker(kaizenConfig);
87110
if (checkResult.isValid === false) {
88111
console.error('[ERROR]: please use kaizen new to create new project first');
89112
return;
@@ -99,7 +122,12 @@ function handleNOIA() {
99122
}
100123

101124
ncp('./temp-noia-sdk/packages', './', function (error) {
125+
if (error) {
126+
handleError(error);
127+
return;
128+
}
102129
cmd.run('rm -rf ./temp-noia-sdk');
130+
updateConfig(kaizenConfig, 'NOIA');
103131
});
104132
});
105133

@@ -108,10 +136,9 @@ function handleNOIA() {
108136
}
109137
}
110138

111-
function handleBluzelle(boilerplate) {
139+
function handleBluzelle(kaizenConfig, boilerplate) {
112140
try {
113-
const package = JSON.parse(fs.readFileSync('./kaizen.json', 'utf8'));
114-
const checkResult = packageChecker(package);
141+
const checkResult = packageChecker(kaizenConfig);
115142
if (checkResult.isValid === false) {
116143
console.error('[ERROR]: please use kaizen new to create new project first.');
117144
return;
@@ -125,12 +152,12 @@ function handleBluzelle(boilerplate) {
125152
return;
126153
}
127154

128-
switch(boilerplate) {
155+
switch (boilerplate) {
129156
case 'react':
130-
reactMixBluzelle();
157+
reactMixBluzelle(kaizenConfig);
131158
break;
132159
case 'vue':
133-
vueMixBluzelle();
160+
vueMixBluzelle(kaizenConfig);
134161
break;
135162
default:
136163
handleError(new Error('please specify boilerplate'));

0 commit comments

Comments
 (0)