forked from crystian/WEBAPP-BUILDER
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.js
More file actions
312 lines (270 loc) · 10.4 KB
/
create.js
File metadata and controls
312 lines (270 loc) · 10.4 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
* Created by Crystian on 2/18/2015.
*
* it is a wizard for create a new app based on a template
*
* Remember, you need permission to execute this file (in *nix ambient)
* > chmod u+x create.js
*/
var fs = require('fs-extra'),
inquirer = require('inquirer'),
exec = require('child_process').exec,
replaceGulp = require('gulp-replace'),
gulp = require('gulp'),
debug = require('gulp-debug'),
cfg = require('./project-config.json'),
chalk = require('chalk'),
del = require('del');
require('./tasks/shared/utils');
var questions = [];
console.logGreen(' ');
console.logGreen(' ╔═══════════════════════════════════════════════════════════════════════════╗ ');
console.logGreen(' ║ * Welcome to "create wizard" of WEAPP-BUILDER! * ║ ');
console.logGreen(' ║ please consider the readme.md before create one. ║ ');
console.logGreen(' ╚═══════════════════════════════════════════════════════════════════════════╝ ');
console.logGreen(' ');
console.log('');
/*
questions tutorial: http://enzolutions.com/articles/2014/09/08/how-to-create-an-interactive-command-in-node-js/
*/
var create = [{
type: 'confirm',
name: 'create',
message: 'Create it? (this action OVERWRITE your files)',
default: false
}];
questions = questions.concat(create);
var projectCode = [{
when: function(r){return r.create;},
type: 'input',
name: 'projectCode',
message: 'Project name (without spaces and simbols):',
default: 'APP'
}];
questions = questions.concat(projectCode);
var copyTemplate = [{
when: function(r){return r.projectCode;},
type: 'list',
name: 'copyTemplate',
message: 'Which template do you want to use?',
choices: [
{value: 'templates/empty', name: 'Empty'},
{value: 'templates/angular-empty', name: 'Angular empty'},
{value: 'templates/angular-full', name: 'Angular full'},
{value: 'templates/angular-material', name: 'Angular material empty'}
]
}];
questions = questions.concat(copyTemplate);
var installDep = [{
when: function(r){return r.copyTemplate;},
type: 'confirm',
name: 'installDep',
message: 'Install dependencies? (npm & bower)',
default: true
}];
questions = questions.concat(installDep);
var cordova = [{
when: function(r){return r.projectCode;},
type: 'confirm',
name: 'cordova',
message: 'Do you want to make a cordova app?',
default: false
}, {
when: function(r){return r.cordova;},
type: 'input',
name: 'main',
message: 'Name:',
default: 'HelloWorld'
}, {
when: function(r){return r.main;},
type: 'input',
name: 'domain',
message: 'Package:',
default: 'com.example.hello'
}];
questions = questions.concat(cordova);
var cordovaPlatform = [{
when: function(r){return r.cordova;},
type: "checkbox",
message: "Platform to install:",
name: "platforms",
choices: [
{value: "android", name: "Android", checked: true},
{value: "browser", name: "Browser"},
{value: "windows", name: "Windows"},
{value: "ios", name: "iOS"},
{value: "wp8", name: "Windows Phone 8"}
]
}];
questions = questions.concat(cordovaPlatform);
var cordovaPlugins = [{
when: function(r){return r.cordova;},
type: "checkbox",
message: "Plugins to install:",
name: "plugins",
choices: [
{name: "device", value: "cordova-plugin-device", checked: true},
{name: "network-information", value: "cordova-plugin-network-information", checked: true},
{name: "globalization", value: "cordova-plugin-globalization", checked: true},
{name: "splashscreen", value: "cordova-plugin-splashscreen", checked: true},
{name: "whitelist", value: "cordova-plugin-whitelist", checked: true},
{name: "dialogs", value: "cordova-plugin-dialogs"},
{name: "battery-status", value: "cordova-plugin-battery-status"},
{name: "device-motion", value: "cordova-plugin-device-motion"},
{name: "device-orientation", value: "cordova-plugin-device-orientation"},
{name: "geolocation", value: "cordova-plugin-geolocation"},
{name: "camera", value: "cordova-plugin-camera"},
{name: "media-capture", value: "cordova-plugin-media-capture"},
{name: "media", value: "cordova-plugin-media"},
{name: "file", value: " cordova-plugin-file"},
{name: "file-transfer", value: "cordova-plugin-file-transfer"},
{name: "vibration", value: "cordova-plugin-vibration"},
{name: "contacts", value: "cordova-plugin-contacts"},
{name: "statusbar", value: "cordova-plugin-statusbar"},
{name: "inappbrowser", value: "cordova-plugin-inappbrowser"},
{name: "console", value: "cordova-plugin-console"}
]
}];
questions = questions.concat(cordovaPlugins);
inquirer.prompt(questions, function(answers){
var projectConfigFile = 'project-config.json',
projectConfigLocalFile = 'project-config-local.json';
if(answers.create){
if(answers.copyTemplate){
del([
answers.copyTemplate + '/build',
answers.copyTemplate + '/dist',
answers.copyTemplate + '/node_modules',
answers.copyTemplate + '/www/vendors/bower_components'
]);
fs.copySync(answers.copyTemplate, '../' + answers.projectCode);
switch (answers.copyTemplate){
case 'templates/angular-empty':
case 'templates/angular-material':
break;
case 'templates/angular-full':
replace([
'../' + answers.projectCode + '/www/app/app.json',
'../' + answers.projectCode + '/www/app/landing.scss',
'../' + answers.projectCode + '/www/vendors/theme/style.css',
'../' + answers.projectCode + '/www/app2/app.json',
'../' + answers.projectCode + '/www/app2/app.js',
'../' + answers.projectCode + '/www/app2/app.scss',
'../' + answers.projectCode + '/www/app2/other.scss',
'../' + answers.projectCode + '/www/app2/msgBold/msgBoldDirective.js',
'../' + answers.projectCode + '/www/app2/data/dataServices.js'
], '../' + answers.copyTemplate + '/www/', '../');
break;
case 'templates/empty':
replace([
'../' + answers.projectCode + '/www/app/app.json',
'../' + answers.projectCode + '/www/app/app.scss'
], '../' + answers.copyTemplate + '/www/', '../');
break;
default:
console.logRed('WHATT?? there isn\'t this template');
process.exit(1);
}
//replaces paths for all templates
replace([
'../' + answers.projectCode + '/gulpfile.js',
'../' + answers.projectCode + '/tasks/tasks.js'
], '../../t', '../WEBAPP-BUILDER/t');
replace([
'../' + answers.projectCode + '/project-config.json'
], '"template": "' + answers.copyTemplate + '"', '');
fs.outputJSONSync('../' + answers.projectCode + '/' + projectConfigLocalFile, {}, {encoding: 'utf8'});
if(answers.installDep){
console.logGreen('Installing dependencies, wait please...');
npmInstall(answers, function(){
npmBower(answers);
});
}
if(answers.cordova){
console.log(chalk.black.bgYellow('Cordova project is generating...'));
exec('cordova create ' + cfg.cordova.folder + ' ' + answers.domain + ' ' + answers.main, {cwd: '../' + answers.projectCode},
function(error, stdout, stderr){
if(error !== null){
console.logRed(stdout);
console.logRed('stderr: ' + stderr);
console.logRed('exec error: ' + error);
console.logRed('Is Cordova installed?');
process.exit(1);
} else {
console.logGreen(stdout);
var packageJson = '../' + answers.projectCode + '/package.json';
var pkg = require(packageJson);
pkg.domain = answers.domain;
pkg.mainClass = answers.main;
fs.outputJSONSync(packageJson, pkg);
var configFile = '../' + answers.projectCode + '/' + projectConfigFile;
var cfgFile = require(configFile);
cfgFile.cordova = {active: true};
fs.outputJSONSync(configFile, cfgFile);
installCordovaPl('platform', answers.platforms, '../' + answers.projectCode, function(){
if(answers.platforms.length > 0){
installCordovaPl('plugin', answers.plugins, '../' + answers.projectCode, finalCordova);
}
});
}
});
}
}
}
});
function replace(files, key, value){
gulp.src(files, {base: './'})
//.pipe(debug({verbose: true}))
.pipe(replaceGulp(key, value))
.pipe(gulp.dest('.'));
}
function npmInstall(answers, cb){
exec('npm install', {cwd: '../' + answers.projectCode},
function(error, stdout, stderr){
if(error !== null){
console.logRed(stdout);
console.logRed('stderr: ' + stderr);
console.logRed('exec error: ' + error);
process.exit(1);
} else {
console.log(stdout);
cb();
}
});
}
function npmBower(answers){
exec('bower install', {cwd: '../' + answers.projectCode},
function(error, stdout, stderr){
if(error !== null){
console.logRed(stdout);
console.logRed('stderr: ' + stderr);
console.logRed('exec error: ' + error);
process.exit(1);
} else {
console.log(stdout);
}
});
}
/* plugins and platforms */
function installCordovaPl(text, pl, projectCode, cb){
if(pl.length > 0){
exec('cordova ' + text + ' add ' + pl.join(' '), {cwd: projectCode + '/' + cfg.cordova.folder},
function(error, stdout, stderr){
if(error !== null){
console.logRed(stdout);
console.logRed('stderr: ' + stderr);
console.logRed('exec error: ' + error);
process.exit(1);
} else {
console.log(stdout);
}
cb(projectCode);
});
} else {
cb();
}
}
function finalCordova(projectCode){
var www = projectCode + '/' + cfg.cordova.folder + '/www';
fs.emptyDirSync(www);
}