-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
executable file
·160 lines (143 loc) · 5.36 KB
/
index.js
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
#!/usr/bin/env node
require("babel-register");
let path = require('path');
let program = require('commander');
let chalk = require('chalk');
let mkdirp = require('mkdirp');
let cpFile = require('cp-file');
let figlet = require('figlet');
let clear = require('clear');
let fs = require('fs');
let os = require('os');
let touch = require("touch");
let emoji = require('node-emoji');
let repl = require("repl");
let ini = require('ini');
let Wormhole = require('./lib/Wormhole').default;
let clone = require('git-clone');
let cmd = require('node-cmd');
program
.version('0.2.5');
program
.command('new <name>')
.option('-s, --scaffold <scaffold>', 'The framework to use. Options include react, angular, vuejs, nextjs and node.')
.option('-r, --restURL <restURL>', 'The rest URL to use. default: https://trest.example.com/v1/')
.option('-e, --environment <environment>', 'environment of running WormholeREST instance. Ex: production, staging. (Default: development)')
.description(`create a new Wormhole application`)
.action((name, options) => {
if(fs.existsSync(`./${name}`)) {
console.log(chalk.red(`Project ${name} already exists`));
process.exit(1);
}
let config;
let environment = fetchOption('environment=development', config, options);
let restURL = fetchOption('restURL=https://trest.example.com/v1/', config, options);
if(options && options.scaffold) {
let scaffold = options.scaffold.toLowerCase();
let repo;
let conf = {};
if(scaffold === 'node') {
repo = 'https://github.com/Bitcoin-com/wormhole-scaffold-node.git';
} else if(scaffold === 'angular') {
repo = 'https://github.com/Bitcoin-com/wormhole-scaffold-angular.git';
} else if(scaffold === 'next') {
repo = 'https://github.com/Bitcoin-com/wormhole-scaffold-next.git';
} else if(scaffold === 'react') {
repo = 'https://github.com/Bitcoin-com/wormhole-scaffold-react.git';
} else if(scaffold === 'vue') {
repo = 'https://github.com/Bitcoin-com/wormhole-scaffold-vue.git';
} else {
console.log(chalk.red(`Scaffold ${scaffold} not supported`));
process.exit(1)
}
if(options && options.repo) {
scaffold = 'custom repo';
repo = options.repo.toLowerCase();
}
clear();
console.log(
chalk.blue(
figlet.textSync('Wormhole', {
font: '3-D',
horizontalLayout: 'full'
})
)
);
console.log(chalk.blue(`Scaffolding ${scaffold} app in ${name}`));
clone(repo, `./${name}`, [conf], (res) => {
if(res == "Error: 'git clone' failed with status 128") {
console.log(chalk.red('Must create new app in to an empty directory'));
} else {
console.log(chalk.green('All done.'), emoji.get(':white_check_mark:'));
console.log(chalk.blue('Now `cd` in to your new project and run `npm install && npm start`'), emoji.get(':rocket:'));
}
});
return;
}
clear();
console.log(
chalk.blue(
figlet.textSync('Wormhole', {
font: '3-D',
horizontalLayout: 'full'
})
)
);
console.log(chalk.green(`Creating ${name}/ directory`));
console.log(chalk.green(`Creating src/ directory: ./${name}/src`));
mkdirp(`./${name}/src`, (err) => {});
console.log(chalk.green(`Creating tests/ directory: ./${name}/tests`));
mkdirp(`./${name}/tests`, (err) => {});
console.log(chalk.green(`Creating wormhole.js configuration file: ./${name}/wormhole.js`));
mkdirp(`./${name}`, (err) => {});
touch(`./${name}/wormhole.js`);
fs.writeFileSync( `./${name}/wormhole.js`, `exports.config = {
networks: {
${environment}: {
restURL: "${restURL}"
}
}
};
`);
fs.appendFileSync(`./${name}/.gitignore`, '.console_history');
console.log(chalk.blue('All done.'), emoji.get(':white_check_mark:'));
console.log(chalk.blue('Go get em! Remember--with great power comes great responsibility.'), emoji.get(':rocket:'));
}
);
program
.command('console')
.option('-e, --environment <environment>', 'environment of running BITBOX instance. Ex: production, staging. (Default: development)')
.description('Run a console with Bitcoin Cash RPC commands available')
.action((options) => {
let config;
try {
config = require(process.cwd() + '/wormhole.js').config;
} catch(err) {
console.log(chalk.red('Console command must be run inside a wormholecash project'));
process.exit(1);
}
let replServer = repl.start('> ');
let historyFile = path.join(process.cwd(), '.console_history');
require('repl.history')(replServer, historyFile);
let environment = fetchOption('environment=development', config, options);
replServer.context.Wormhole = new Wormhole(config.networks[environment]);
}
);
function fetchOption(kv, config, options) {
let parts = kv.split('=');
let key = parts[0];
let defaultVal = parts[1];
if(options && options[key]) {
return options[key];
} else if(config && config.new && config.new[key]) {
return config.new[key];
} else {
return defaultVal;
}
}
program
.parse(process.argv);
// print help if no command given
if (!process.argv.slice(2).length) {
program.outputHelp()
}