-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
executable file
·202 lines (190 loc) · 5.9 KB
/
main.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
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
/*
* exports toolkit
* @author genify([email protected])
* @author huntbao
*/
'use strict';
let fs = require('fs');
let util = require('util');
let _fs = require('./lib/util/file');
let _path = require('./lib/util/path');
let _io = require('./lib/util/io');
let _log = require('./lib/util/logger');
let Builder = require('./lib/nei/builder');
let _logger = _log.logger;
class Main {
/**
* load data from nei server
* @param {string|number} pid - nei project id
* @param {function} callback - load success callback
* @param {function} [errorCallback] - load error callback
* @return {undefined}
*/
loadData(pid, callback, errorCallback) {
let api = util.format(
(require('./package.json').nei || {}).api,
pid
);
_logger.info('load nei data from %s', api);
_io.download(api, (data) => {
let json = this.parseData(data);
if (json) {
callback(json);
} else {
if (errorCallback) {
errorCallback();
} else {
process.exit(1);
}
}
});
}
/**
* parse nei string data to json
* @param {string} data - nei string data
* @return {object|undefined}
*/
parseData(data) {
_logger.info('parse nei string data');
let json;
try {
json = JSON.parse(data);
} catch (ex) {
_logger.debug('string data from nei \n%s', data);
return _logger.error('nei string data parsing error\n%s', ex.stack);
}
if (json.code !== 200) {
return _logger.error('illegal string data from nei %j', json);
}
json = json.result;
if (!json.timestamp) {
return _logger.error('illegal string data from nei %j', json);
}
return json;
}
/**
* build nei project
* @param {object} config - config object
* @return {undefined}
*/
build(config) {
let cwd = process.cwd() + '/';
config.outputRoot = _path.absolute(config.project + '/', cwd);
if (config.deployRoot) {
config.deployRoot = _path.absolute(config.deployRoot + '/', config.outputRoot);
}
if (config.tMockRoot) {
config.tMockRoot = _path.absolute(config.tMockRoot + '/', config.outputRoot);
}
if (config.iMockRoot) {
config.iMockRoot = _path.absolute(config.iMockRoot + '/', config.outputRoot);
}
let existNeiConf = `${config.outputRoot}nei.${config.id}/nei.json`;
let action = config.action;
// check if exists nei.json file
let msg;
if (_fs.exist(existNeiConf)) {
if (action === 'build') {
msg = 'use "nei update" to update nei project with id[%s]';
}
} else {
if (action === 'update') {
msg = 'use "nei build" to build nei project with id[%s]';
}
}
if (msg) {
_logger.error(msg, config.id);
return process.exit(1);
}
let existConf = {};
if (action === 'update') {
existConf = require(existNeiConf);
} else {
existConf.updateTime = 0;
}
config = Object.assign(existConf, config);
let name;
if (config.template === 'mobile') {
name = `./lib/nei/mobile.${config.lang}.js`;
} else {
name = './lib/nei/webapp.js';
}
this.loadData(config.id, (data) => {
let Builder = require(name);
let builder = new Builder(config);
builder[action](data);
});
}
/**
* build empty mobile project
* @param {object} config - config object
* @return {undefined}
*/
buildEmpty(config) {
let cwd = process.cwd() + '/';
config.outputRoot = _path.absolute(config.project + '/', cwd);
let name = `./lib/nei/mobile.${config.lang}.js`;
let Builder = require(name);
let builder = new Builder(config);
builder.build();
}
/**
* update nei project
* @param {object} config - config object
* @return {undefined}
*/
update(config) {
let cwd = process.cwd() + '/';
let project = _path.absolute(
config.project + '/', cwd
);
let list = fs.readdirSync(project);
if (!list || !list.length) {
return _logger.error('no nei project found in %s', project);
}
_logger.error('check to update all nei project');
// check nei config directory
let reg = /^nei\.([\d]+)$/;
list.forEach((name) => {
if (_fs.isdir(project + name + '/') && reg.test(name)) {
config.id = RegExp.$1;
this.build(config);
}
});
}
/**
* generate mock data
* @param {object} config - config object
* @return {undefined}
*/
mock(config) {
let cwd = process.cwd() + '/';
config.outputRoot = _path.absolute(
config.output + '/', cwd
);
this.loadData(config.id, (data) => {
let builder = new Builder(config);
builder.mock(data);
});
}
/**
* export mobile models and requests
* @param {object} config - config object
* @return {undefined}
*/
mobile(config) {
let cwd = process.cwd() + '/';
config.outputRoot = _path.absolute(
config.output + '/', cwd
);
let lang = config.lang;
if (!/^(oc|java)$/.test(lang)) {
return _logger.error('not supported language %s', lang);
}
this.loadData(config.id, (data) => {
let builder = new (require(`./lib/nei/mobile.${lang}.js`))(config);
builder.model(data);
});
}
}
module.exports = new Main();