This repository was archived by the owner on Jul 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarter.config.js
More file actions
202 lines (189 loc) · 5.75 KB
/
starter.config.js
File metadata and controls
202 lines (189 loc) · 5.75 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
197
198
199
200
201
202
/**
* @fileOverview Spec for forge-react-starter, can be used as a template to setup a new starter
*
* @requires @arcblock/forge-wallet
*/
/* eslint-disable import/order */
/* eslint-disable no-console */
/* eslint-disable object-curly-newline */
const ip = require('ip');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const shell = require('shelljs');
const camelCase = require('lodash/camelCase');
const { execSync } = require('child_process');
const { types } = require('@arcblock/mcrypto');
const { fromRandom, WalletType } = require('@arcblock/forge-wallet');
const { name, version } = require('./package.json');
const debug = require('debug')(name);
const defaults = {
appName: 'Forge React Starter',
appDescription: 'React starter application that runs on forge powered chain',
appPort: 3030,
mongoUri: 'mongodb://127.0.0.1:27017/forge-react-starter',
};
const questions = [
{
type: 'text',
name: 'appName',
message: 'Application name:',
default: defaults.appName,
validate: input => {
if (!input) return 'Application name should not be empty';
return true;
},
},
{
type: 'text',
name: 'appDescription',
message: 'Application description:',
default: defaults.appDescription,
validate: input => {
if (!input) return 'Application descripiton should not be empty';
return true;
},
},
{
type: 'text',
name: 'appPort',
message: 'Application listening port:',
default: defaults.appPort,
validate: input => {
if (!input) return 'Application listening port should not be empty';
return true;
},
},
{
type: 'text',
name: 'mongoUri',
message: 'Mongodb URI:',
default: defaults.mongoUri,
validate: input => {
if (!input) return 'Mongodb connection string:';
return true;
},
},
];
module.exports = {
/**
* Set starter version
* @variable
* @public
*/
name,
/**
* Set starter version
* @variable
* @public
*/
version,
/**
* Set default parameters for creating project from this starter
* Should set defaults for all questions declared in `questions` array
* @optional
* @variable
* @public
*/
defaults,
/**
* List of questions used by inquire to collect parameters
* @optional
* @variable
* @public
*/
questions,
/**
* List of files/folders to exclude form the starter folder when creating new projects
* @variable
* @public
*/
blacklist: [__filename],
/**
* On project folder created and files synced
* You can create new files/modifying existing files in the project folder
*
* @param {object} config - project creating config
* @param {string} config.targetDir - the project folder
* @param {string} config.chainHost - the graphql endpoint the application running on
* @param {string} config.chainId - the chainId of the application running on
* @param {GraphQLClient} config.client - GraphQLClient instance that can be used to send request to the chain
* @param {object} config.symbols - ui elements that can be used to print logs
* @param {string} config.__starter__ - checkout `answers` for other collected parameters
* @function
* @public
*/
async onConfigured(config) {
const { chainHost, chainId, targetDir, appName, appDescription, appPort, mongoUri, client, symbols } = config;
const ipAddress = ip.address();
// Declare application on chain
const wallet = fromRandom(
WalletType({
role: types.RoleType.ROLE_APPLICATION,
pk: types.KeyType.ED25519,
hash: types.HashType.SHA3,
})
);
debug('application wallet', wallet.toJSON());
const hash = await client.sendDeclareTx({
tx: {
chainId,
itx: {
moniker: camelCase(appName),
},
},
wallet,
});
debug('application declare tx', hash);
console.log(`${symbols.success} application account declared on chain: ${wallet.toAddress()}`);
// Generate config
const configPath = path.join(`${targetDir}`, '.env');
const configContent = `MONGO_URI="${mongoUri}"
CHAIN_ID="${chainId}"
CHAIN_HOST="${chainHost.replace('127.0.0.1', ipAddress).replace('localhost', ipAddress)}"
APP_NAME="${appName}"
APP_DESCRIPTION="${appDescription}"
APP_PORT="${appPort}"
APP_SK="${wallet.secretKey}"
APP_ID="${wallet.toAddress()}"
APP_TOKEN_SECRET="${wallet.publicKey.slice(16)}"
APP_TOKEN_TTL="1d"
BASE_URL="http://${ipAddress}:${appPort}"`;
fs.writeFileSync(configPath, configContent);
console.log(`${symbols.success} application config generated ${configPath}`);
},
/**
* On project created, files set
* You can install dependencies for the project in this hook
*
* @param {object} config - project creating config
* @param {string} config.targetDir - the project folder
* @param {object} config.symbols - ui elements that can be used to print logs
* @function
* @public
*/
onCreated(config) {
const { targetDir, symbols } = config;
const pm = shell.which('yarn') ? 'yarn' : 'npm';
console.log(`${symbols.info} installing application dependencies...`);
execSync(`cd ${targetDir} && ${pm} install`, { stdio: [0, 1, 2] });
},
/**
* On project ready to run
* You can print basic steps for users to start the application
*
* @param {object} config - project creating config
* @param {string} config.targetDir - the project folder
* @param {object} config.symbols - ui elements that can be used to print logs
* @function
* @public
*/
onComplete(config) {
const { targetDir } = config;
const pm = shell.which('yarn') ? 'yarn' : 'npm';
shell.echo('');
shell.echo(chalk.cyan(`cd ${targetDir}`));
shell.echo(chalk.cyan(`${pm} start`));
shell.echo('');
},
};