-
Notifications
You must be signed in to change notification settings - Fork 0
/
beds-dev-cli
executable file
·199 lines (174 loc) · 6.49 KB
/
beds-dev-cli
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
#!/usr/bin/env node
"use strict";
const cmd = require('commander');
const cp = require('child_process');
const fs = require('fs');
const config = require('./mock_server/doumi_config');
const server = require('./mock_server/server');
var chalk = require('chalk');
var error = chalk.bold.red;
const gitignoreTpl = require('./gitignoreTpl');
const pcCommonUri = 'git@git.corp.doumi.com:doumi_browser/common_pc.git';
const mCommonUri = 'git@git.corp.doumi.com:doumi_browser/common_m.git';
let projectRootDir = process.cwd() + '/';
cmd.version('1.0.0')
cmd.command('init <pc|m>')
.description('在非空目录创建新项目并初始化')
.option('-f, --force', '强制在当前目录创建项目')
.action((type, options) => {
if (options.force) {
init(type);
} else {
if (fs.readdirSync(projectRootDir).length) {
console.log(' 当前目录不为空,请选择空目录创建项目!');
} else {
init(type);
}
}
})
cmd.command('update')
.description('更新common模块')
.action(() => {
try {
console.log('开始更新...')
let bedsLog = require(projectRootDir + '.beds.log.json');
let projectType = bedsLog.projectType;
if (!projectType) {
throw new Error('not type')
}
cloneCommonModule(bedsLog.projectType).then(() => {
genBedsLog(projectType);
console.log('更新完成!');
});
} catch (e) {
console.log(error(' 请先初始化项目!如果项目已进行过初始化,请联系guoyang@doumi.com'));
}
})
cmd.command('server')
.description('启动mock-server')
.action(() => {
startupServer();
})
cmd.parse(process.argv);
if (!process.argv.slice(2).length) {
cmd.help();
}
function init(type) {
console.log('生成项目目录结构...');
const dirs = [
projectRootDir,
projectRootDir + 'view/',
projectRootDir + 'proto_view/',
projectRootDir + 'static/',
projectRootDir + 'static/css/',
projectRootDir + 'static/less/',
projectRootDir + 'static/js/',
projectRootDir + 'static/img/'
];
dirs.forEach(dir => createDir(dir));
console.log('创建完毕');
//暂时弃用.common目录结构暂时从其他beds项目拷贝
/*console.log('克隆通用组件...');
cloneCommonModule(type).then(() => {
genMockServerConfig();
genGitignore();
genBedsLog(type);
console.log('创建完毕!');
})*/
}
//创建目录
function createDir(path) {
if (!fs.existsSync(path)) {
fs.mkdirSync(path)
}
}
function cloneCommonModule(type) {
return new Promise((resolve, reject) => {
let uri = '';
switch (type) {
case 'pc' :
uri = pcCommonUri;
break;
case 'm' :
uri = mCommonUri;
break;
}
if (fs.existsSync('./common')) {
cp.exec('rm -rf ./common', function () {
cloneCommon();
});
} else {
cloneCommon();
}
function cloneCommon() {
let cmdStr = 'mkdir .tmp_common && cd .tmp_common && git init && git remote add -f origin ' + uri + ' && git config core.sparsecheckout true && echo common >> .git/info/sparse-checkout && git pull origin master'
cp.exec(cmdStr, {cwd: process.cwd()}, function (error, so, sr) {
if (error) {
console.log(chalk.red(error));
} else {
//console.log(so.trim());
cp.exec('mv .tmp_common/common ./ && rm -rf .tmp_common', {cwd: process.cwd()}, function (error, so, sr) {
if (error) {
console.log(chalk.red(error));
} else {
resolve();
}
})
}
});
}
})
}
//设置php接口服务域名、端口
function genMockServerConfig() {
console.log('生成mock-server配置文件...');
let remoteApiConfig =
`//远程接口服务配置
exports.remoteApiConfig = {
serverDomain : '', //接口服务器域名,例: m.doumi.com
serverPort : 80, //接口服务器端口
accessToken : {'doumi_melon' : ''} //接口服务器访问token。登陆后,token为cookie字段doumi_melon的值
}
exports.mockServerPort = '8900'; //本地mock-server端口
exports.traditionJsPathReg = /\\/js_lib\\//; //传统js路径正则。对于传统js不做模块化和es6转译操作
`
fs.writeFileSync('./mock-server.config.js', remoteApiConfig, {})
}
//启动mock-server
function startupServer() {
if (!fs.existsSync(projectRootDir + 'mock-server.config.js')) {
genMockServerConfig();
}
let mockServerConfig = require(projectRootDir + 'mock-server.config.js');
try {
config.remoteRootPathConfig.beApiRootPath = mockServerConfig.remoteApiConfig.serverDomain;
config.remoteRootPathConfig.beApiServerPort = mockServerConfig.remoteApiConfig.serverPort;
config.remoteRootPathConfig.beAccessToken = mockServerConfig.remoteApiConfig.accessToken;
config.mockServerConfig.port = mockServerConfig.mockServerPort;
config.traditionJsPathReg = mockServerConfig.traditionJsPathReg;
if (typeof mockServerConfig.mockServerPort == "undefined" || typeof mockServerConfig.traditionJsPathReg == "undefined") {
throw '配置文件异常';
}
} catch (e) {
console.log(error('mock-server.config.js配置文件存在异常!将进行重写..'));
genMockServerConfig();
console.log('重写完成,请再次启动服务!');
process.exit(0);
}
console.log(chalk.blue('(mock-server相关的配置信息请在mock-server-config.js中进行修改,修改后需重启server)'))
server.start(config);
console.log('mock-server启动成功!\n端口号(port):', config.mockServerConfig.port);
console.log('请使用 http://<ip|localhost|127.0.0.1>:' + config.mockServerConfig.port + '/<view-path> 进行访问')
}
//生成gitignore
function genGitignore() {
console.log('生成.gitignore文件..');
fs.writeFileSync('./.gitignore', gitignoreTpl)
}
function genBedsLog(type) {
var logJson = {
'projectType': type,
'latestUpdateCommon': new Date()
}
fs.writeFileSync('./.beds.log.json', JSON.stringify(logJson));
}