forked from 75team/thinkjs-cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
236 lines (231 loc) · 6.04 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
var fs = require('fs');
var child_process = require('child_process');
var os = require('os');
var path = require('path');
require('thinkjs/lib/Common/common.js');
var argvs = process.argv;
var params = {};
argvs.slice(2).forEach(function(arg){
switch(arg){
case '-h':
case '--help':
params.help = true;
break;
case '-v':
case '--version':
params.version = true;
break;
case '-s':
case '--silent':
params.silent = true;
break;
default:
if (arg.indexOf('-') !== 0) {
params.path = arg;
}
break;
}
})
if (params.version) {
var chars = [
" _______ _ _ _ _ _____ ",
"|__ __| | (_) | | | |/ ____|",
" | | | |__ _ _ __ | | __ | | (___ ",
" | | | '_ \\| | '_ \\| |/ / | |\\___ \\ ",
" | | | | | | | | | | < |__| |____) |",
" |_| |_| |_|_|_| |_|_|\\_\\____/|_____/ ",
" "
].join("\n");
console.log('\n v' + getVersion() + '\n');
console.log(chars);
}else if (params.path) {
createProject();
}else{
showHelp();
}
/**
* [showHelp description]
* @return {[type]} [description]
*/
function showHelp(){
var data = [
'',
'Usage: thinkjs [options] <project-path>',
'',
'Options:',
'',
' -h, --help output usage information',
' -v, --version output thinkjs version',
" -s, --silent create project, can't start service",
''
].join('\n ');
console.log(data);
}
/**
* 获取目录下的所有文件
* @param {[type]} dir [description]
* @param {[type]} prefix [description]
* @return {[type]} [description]
*/
function getFiles(dir, prefix){
if (!fs.existsSync(dir)) {
return [];
}
prefix = prefix || '';
var files = fs.readdirSync(dir);
var result = [];
files.forEach(function(item){
var stat = fs.statSync(dir + '/' + item);
if (stat.isFile()) {
result.push(prefix + item);
}else if(stat.isDirectory()){
result = result.concat(getFiles(path.normalize(dir + '/' + item), path.normalize(prefix + '/' + item + '/')));
}
})
return result;
}
/**
* 获取版本号
* @return {[type]} [description]
*/
function getVersion(){
if (params.path) {
var packageFile = params.path + '/node_modules/thinkjs/package.json';
if (isFile(packageFile)) {
var content = fs.readFileSync(packageFile);
content = JSON.parse(content);
return content.version;
}
};
var packageFile = __dirname + '/node_modules/thinkjs/package.json';
var content = fs.readFileSync(packageFile);
content = JSON.parse(content);
return content.version;
}
/**
* 创建项目
* @return {[type]} [description]
*/
function createProject(){
var projectpath = params.path;
mkdir(projectpath, '0755');
var files = fs.readdirSync(projectpath);
if (files.indexOf('App') > -1 || files.indexOf('www') > -1) {
console.log('path `' + projectpath + '` is a thinkjs project');
return false;
}
mkpath(projectpath);
copyFiles(projectpath);
console.log('Application create finished');
copyThinkJS(projectpath);
if (!params.silent) {
startService(projectpath);
}
}
/**
* 创建项目目录
* @param {[type]} projectpath [description]
* @return {[type]} [description]
*/
function mkpath(projectpath){
var paths = [
'/www/resource/',
'/www/resource/js',
'/www/resource/css',
'/www/resource/module',
'/www/resource/swf',
'/www/resource/font',
'/www/resource/img',
'/www/resource/other',
'/App/Lib/Model',
'/App/Lib/Controller/Home',
'/App/Common',
'/App/Conf',
'/App/Runtime/Cache',
'/App/Runtime/Data',
'/App/Runtime/Temp',
'/App/Runtime/Log',
'/App/View'
];
paths.forEach(function(item){
mkdir(projectpath + item, '0755');
});
}
/**
* 拷贝文件
* @return {[type]} [description]
*/
function copyFiles(projectpath){
var prefixPath = __dirname + '/tmp';
var sourceFiles = [
prefixPath + '/IndexController.js',
prefixPath + '/BaseController.js',
prefixPath + '/index_index.html',
prefixPath + '/common.js',
prefixPath + '/ctrl.sh',
prefixPath + '/config.js',
prefixPath + '/index.js',
prefixPath + '/gitignore.log',
prefixPath + '/package.json',
prefixPath + '/bowerrc.json',
prefixPath + '/README.md'
];
var dstFiles = [
'/App/Lib/Controller/Home/IndexController.js',
'/App/Lib/Controller/Home/BaseController.js',
'/App/View/Home/index_index.html',
'/App/Common/common.js',
'/ctrl.sh',
'/App/Conf/config.js',
'/www/index.js',
'/.gitignore',
'/package.json',
'/.bowerrc',
'/README.md'
];
dstFiles.forEach(function(file, i) {
file = projectpath + file;
if (!isFile(file)) {
mkdir(path.dirname(file), '0755');
var buffer = fs.readFileSync(path.normalize(sourceFiles[i]));
fs.writeFileSync(path.normalize(file), buffer);
}
});
}
/**
* 拷贝thinkjs目录
* @param {[type]} projectpath [description]
* @return {[type]} [description]
*/
function copyThinkJS(projectpath){
var thinkjsPath = __dirname + '/node_modules/thinkjs/';
var targetPath = path.normalize(projectpath + '/node_modules/thinkjs/');
if (isDir(targetPath)) {
return;
};
var files = getFiles(thinkjsPath);
files.forEach(function(file){
var sourceFile = thinkjsPath + file;
var targetFile = path.normalize(targetPath + '/' + file);
mkdir(path.dirname(targetFile), '0755');
var buffer = fs.readFileSync(sourceFile);
fs.writeFileSync(targetFile, buffer);
})
}
/**
* 启动服务
* @param {[type]} projectpath [description]
* @return {[type]} [description]
*/
function startService(projectpath){
var nodePath = process.execPath;
child_process.exec('"' + nodePath + '" ' + path.normalize(projectpath + '/www/index.js'));
//打开浏览器
var exec = 'open';
if (os.platform() === 'win32') {
exec = 'start';
}
setTimeout(function(){
child_process.exec(exec + ' http://127.0.0.1:8360');
}, 1000);
}