UNPKG

15.5 kBJavaScriptView Raw
1#! /usr/bin/env node
2
3/******
4
5 MIT License
6
7 Copyright (c) 2016 GorillaJS
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14
15******/
16
17
18'use strict';
19
20var argv = require('minimist')(process.argv.slice(2));
21var prompt = require('readline-sync');
22var color = require('colors');
23var datef = require('dateformat');
24var mkdirp = require('mkdirp');
25var path = require('path');
26var uuid = require('uuid/v4');
27
28global.envPaths = {
29
30 'base': __dirname,
31 'libraries': path.join(__dirname, 'lib')
32
33}
34
35var variables = require(path.join(envPaths.libraries, 'variables.js'));
36var tools = require(path.join(envPaths.libraries, 'tools.js'));
37var events = require(path.join(envPaths.libraries, 'pubsub.js'));
38var m_docker = require(path.join(envPaths.libraries, 'docker.js'));
39var host = require(path.join(envPaths.libraries, 'host.js'));
40var cross = require(path.join(envPaths.libraries, 'crossExec.js'));
41var promises = require(path.join(envPaths.libraries, 'promises.js'));
42var commit = require(path.join(envPaths.libraries, 'commit.js'));
43var plugins = require(path.join(envPaths.libraries, 'plugins.js'));
44var credentials = require(path.join(envPaths.libraries, 'login.js'));
45
46var gorillaPath = variables.gorillaPath;
47var gorillaFolder = variables.gorillaFolder;
48var gorillaTemplateFolder = variables.gorillaTemplateFolder;
49var gorillaFile = variables.gorillaFile;
50var messagesFile = variables.messagesFile;
51var projectPath = variables.projectPath;
52var homeUserPathBash = variables.homeUserPathBash;
53var homeUserPathNodeJS = variables.homeUserPathNodeJS;
54var hostsFile = variables.hostsFile;
55var commonPath = variables.commonPath;
56var workingPath = variables.workingPath;
57var composeFile = variables.composeFile;
58var proxyName = variables.proxyName;
59var proxyHost = variables.proxyHost;
60var env = variables.env;
61var verbose = variables.verbose;
62var templateOptions = variables.templateOptions;
63var templateRepos = variables.templateRepos;
64
65global.envPaths.plugins = path.join(homeUserPathNodeJS, proxyName, 'plugins');
66
67events.subscribe('ERROR', showError);
68events.subscribe('VERBOSE', showVerbose);
69events.subscribe('WARNING', tools.showWarning);
70events.subscribe('STEP', tools.showStep);
71events.subscribe('MESSAGE', tools.showMessage);
72events.subscribe('ANSWER', tools.answer);
73
74checkUserInput();
75
76function checkUserInput(){
77
78 var promisesPack = [];
79
80 if(projectPath === gorillaPath){
81
82 events.publish('ERROR', ['000']);
83
84 }else{
85
86 if((argv.hasOwnProperty('v') || argv.hasOwnProperty('version')) && argv._.length === 0){
87
88 console.log(tools.printVersion());
89
90 }else if((argv.hasOwnProperty('h') || argv.hasOwnProperty('help')) && argv._.length === 0){
91
92 console.log('Please, visit https://gorillajs.com');
93
94 }else{
95
96 if(argv._[0] === 'build' || argv._[0] === 'run'){
97
98 promisesPack.push([tools.printLogo]);
99
100 }
101
102 promisesPack.push(
103
104 [m_docker.getTemplateSource, [path.join(homeUserPathBash, proxyName, 'templates'), templateRepos.proxy, 'gorillajs-proxy']],
105 [tools.retrieveConfigData, [path.join(homeUserPathNodeJS, proxyName), 'gorillajs-proxy']],
106 [tools.retrieveConfigData, [path.join(homeUserPathNodeJS, proxyName), 'overwrite']],
107 [tools.track, ['Platform', process.platform, '', 3]],
108 [plugins.include]
109
110 );
111
112 if(argv._[0] === 'build'){
113
114 if(argv._[0] && argv._[1]){
115
116 projectPath = path.resolve(argv._[1]);
117 mkdirp.sync(projectPath);
118 variables.projectPath = projectPath;
119 commonPath = path.join(projectPath, gorillaFolder, 'common');
120 workingPath = projectPath;
121 variables.workingPath = workingPath;
122
123 }
124
125
126 if(argv.hasOwnProperty('f')){
127
128 promisesPack.push(
129 [m_docker.stop, [path.join(projectPath, gorillaFolder, gorillaFile)]],
130 [tools.force, [path.join(projectPath, gorillaFolder, gorillaFile)]]
131 );
132
133 }
134
135 if(argv.hasOwnProperty('p')){
136
137 proxyPort = argv.p;
138
139 }
140
141 promisesPack.push(
142
143 [tools.config, env],
144 [tools.isNewProject, path.join(workingPath, gorillaFolder, gorillaFile), 'new-project'],
145
146 [m_docker.check],
147 [m_docker.config],
148
149 [tools.createGorillaFile, [path.join(projectPath, gorillaFolder, gorillaFile), gorillaFolder], 'id'],
150 [promises.cond, '{{id}}!:', [
151
152 [tools.paramForced, ['project', 'id', uuid()]]
153
154 ], [
155
156 [tools.paramForced, ['project', 'id', '{{id}}']]
157
158 ]],
159 [build]
160
161 );
162
163 }else if(argv._[0] === 'run'){
164
165 if(argv._[0] && argv._[1]){
166
167 projectPath = path.resolve(argv._[1]);
168 variables.projectPath = projectPath;
169 workingPath = projectPath;
170 variables.workingPath = workingPath;
171
172 }
173
174 promisesPack.push(
175
176 [tools.config, env],
177 [tools.createGorillaFile, [path.join(projectPath, gorillaFolder, gorillaFile), gorillaFolder], 'id'],
178 [run]
179
180 );
181
182 }else if(argv._[0] === 'login'){
183
184 promisesPack.push(
185
186 [credentials.login, [argv._[1], argv._[2]]]
187
188 );
189
190 }else if(argv._[0] === 'logout'){
191
192 promisesPack.push(
193
194 credentials.logout
195
196 );
197
198 }
199
200 promisesPack.push(
201
202 [events.publish, ['INIT_PLUGINS', path.join(projectPath, gorillaFolder, gorillaFile)], true]
203
204 );
205
206
207 }
208
209 promises.add(promisesPack);
210 promises.start();
211
212 }
213
214}
215
216function run(){
217
218 var promisesPack = [];
219
220 promisesPack = [
221
222 [tools.param, ['project', 'domain'], 'domain'],
223 [tools.param, ['docker', 'template_type'], 'template_type'],
224 [tools.sanitize, ['{{domain}}', ''], 'slug'],
225
226 [commit.replace],
227 [m_docker.startSimple, [path.join(workingPath, gorillaFolder, gorillaTemplateFolder, composeFile), '{{slug}}']],
228 [m_docker.startSimple, [path.join(homeUserPathBash, proxyName, 'proxy', 'template', composeFile), proxyName]],
229 [tools.track, ['Installer', '{{template_type}}', '', 1]],
230
231 [events.publish, ['STEP', ['Your project is ready!']]]
232
233 ];
234
235 promises.add(promisesPack);
236 promises.start();
237
238}
239
240function build(){
241
242 var promisesPack = [];
243
244 promisesPack = [
245
246 [events.publish, ['STEP', ['starting']]],
247 [tools.paramForced, ['docker', 'gorillafolder', gorillaFolder]],
248 [tools.param, ['docker', 'template_type', templateOptions], 'template_type'],
249 [tools.track, ['Installer', '{{template_type}}', '', 1]],
250 [events.publish, ['TEMPLATE_SELECTED', '{{template_type}}'], true],
251
252 [events.publish, ['STEP', ['check_repo']]],
253 [promises.cond, '{{template_type}}::Local folder', [
254
255 [tools.param, ['docker', 'template_folder'], 'template']
256
257 ], [
258
259 [promises.cond, '{{template_type}}::External repository', [
260
261 [tools.param, ['docker', 'template_repository'], 'template']
262
263 ], [
264
265 [tools.sanitize, ['{{template_type}}', ''], 'template_type'],
266 [tools.selectObjectValue, [templateRepos, '{{template_type}}'], 'template']
267
268 ]],
269
270 ]],
271
272 [tools.basename, ['{{template}}'], 'template_basename'],
273 [tools.sanitize, ['{{template_basename}}', '-'], 'template_slug'],
274 [tools.paramForced, ['docker', 'data_path', path.join(homeUserPathBash, proxyName, 'data')], 'data_path'],
275 [tools.paramForced, ['docker', 'template_path', path.join(homeUserPathBash, proxyName, 'templates', '{{template_slug}}')], 'template_path'],
276 [tools.paramForced, ['docker', 'template_slug', '{{template_slug}}']],
277 [tools.paramForced, ['docker', 'template', '{{template}}']],
278
279 [promises.cond, '{{template_type}}::Local folder', [
280
281 [cross.moveFiles, ['{{template_path}}', false, ['.DS_Store', '.git'], '{{template}}']]
282
283 ], [
284
285 [m_docker.getTemplateSource, [path.join(homeUserPathBash, proxyName, 'templates'), '{{template}}', '{{template_slug}}']]
286
287 ]],
288
289 [promises.cond, '{{new-project}}::yes', [
290
291 [cross.moveFiles, [projectPath, false, ['.DS_Store', '.git'], path.join('{{template_path}}', 'project')]]
292
293 ]],
294
295 [tools.paramForced, ['docker', 'port', Math.floor(Math.random() * (4999 - 4700)) + 4700], 'dockerport'],
296 [cross.moveFiles, [path.join(projectPath, gorillaFolder, gorillaTemplateFolder), false, ['.DS_Store', 'project', '.git'], path.join(homeUserPathNodeJS, proxyName, 'templates', '{{template_slug}}')]],
297 // [cross.moveFiles, [path.join(projectPath, gorillaFolder, gorillaTemplateFolder), false, ['.DS_Store', 'project', '.git'], path.join(envPaths.base, 'templates', 'Django')]],
298 // [cross.moveFiles, [path.join(projectPath, gorillaFolder, gorillaTemplateFolder), false, ['.DS_Store', 'project', '.git'], path.join(envPaths.base, 'templates', 'Wordpress')]],
299 // [cross.moveFiles, [path.join(projectPath, gorillaFolder, gorillaTemplateFolder), false, ['.DS_Store', 'project', '.git'], path.join(envPaths.base, 'templates', 'nodejs')]],
300 [tools.retrieveConfigData, [path.join(homeUserPathNodeJS, proxyName), '{{template_slug}}']],
301
302 [events.publish, ['STEP', ['check_domain']]],
303 [tools.param, ['project', 'domain'], 'domain'],
304 [tools.sanitize, ['{{domain}}', ''], 'slug'],
305 [tools.isLocalProject, '{{domain}}', 'islocal'],
306 [tools.track, ['IsLocal', '{{islocal}}', '', 2]],
307 [tools.paramForced, ['project', 'protocol', 'http'], 'protocol'],
308 [tools.paramForced, ['project', 'islocal', '{{islocal}}']],
309 [tools.paramForced, ['project', 'slug', '{{slug}}']],
310
311 [events.publish, ['DOMAIN_SELECTED', '{{domain}}'], true],
312
313 [tools.param, ['proxy', 'port'], 'proxyport'],
314 [tools.param, ['proxy', 'host'], 'proxyhost'],
315 [tools.paramForced, ['system', 'hostsfile', hostsFile], 'hosts-file'],
316 [tools.paramForced, ['proxy', 'userpath', path.join(homeUserPathBash, proxyName, 'proxy')]],
317
318 [events.publish, ['STEP', ['move_files']]],
319
320 [cross.moveFiles, [path.join(homeUserPathNodeJS, proxyName, 'proxy', 'template'), false, ['.DS_Store', '.git'], path.join(homeUserPathNodeJS, proxyName, 'templates', 'gorillajs-proxy')]],
321
322 [events.publish, ['STEP', ['config_plugins']]],
323
324 [events.publish, ['BEFORE_SET_PROXY_VARIABLES', [path.join(projectPath, gorillaFolder, gorillaFile), path.join(homeUserPathNodeJS, proxyName, 'proxy', 'template')]], true],
325 [tools.setEnvVariables, path.join(homeUserPathNodeJS, proxyName, 'proxy', 'template', '*')],
326 [events.publish, ['AFTER_SET_PROXY_VARIABLES', [path.join(projectPath, gorillaFolder, gorillaFile), path.join(homeUserPathNodeJS, proxyName, 'proxy', 'template')]], true],
327
328 [events.publish, ['BEFORE_SET_TEMPLATE_VARIABLES', [path.join(projectPath, gorillaFolder, gorillaFile), path.join(projectPath, gorillaFolder, gorillaTemplateFolder)]], true],
329 [tools.setEnvVariables, [path.join(projectPath, gorillaFolder, gorillaTemplateFolder, '*'), ['image']]],
330 [events.publish, ['AFTER_SET_TEMPLATE_VARIABLES', [path.join(projectPath, gorillaFolder, gorillaFile), path.join(projectPath, gorillaFolder, gorillaTemplateFolder)]], true],
331
332 [events.publish, ['STEP', ['docker_start']]],
333 [m_docker.network],
334 [commit.replace],
335 [m_docker.stop, [path.join(projectPath, gorillaFolder, gorillaFile)]],
336 [m_docker.start, ['{{machine-name}}', path.join(workingPath, gorillaFolder, gorillaTemplateFolder, composeFile), '{{slug}}', '{{ssh-enabled}}']],
337 [m_docker.stop, [null, 'gorillajsproxy']],
338 [m_docker.base, [path.join(homeUserPathBash, proxyName, 'proxy', 'template', composeFile), proxyName, '{{proxyport}}']],
339 [tools.fusion, [path.join(projectPath, gorillaFolder, gorillaFile)]],
340
341 [promises.cond, '{{islocal}}::yes', [
342
343 [host.checkBeforeAdd, ['{{hosts-file}}', '{{domain}}'], 'add-host'],
344
345 [promises.cond, '{{add-host}}::yes', [
346
347 [tools.param, ['system', 'password', null, null, false], 'system-password'],
348 [host.add, ['{{hosts-file}}', '{{domain}}', '{{system-password}}']]
349
350 ]],
351
352 [promises.cond, '{{proxyport}}::80', [
353
354 // [host.check, ['{{protocol}}://{{domain}}']],
355 [host.check, ['http://{{domain}}']],
356 [host.open, '{{protocol}}://{{domain}}'],
357 [events.publish, ['MESSAGE', ['Server ready!!!']], true]
358
359 ], [
360
361 // [host.check, ['{{protocol}}://{{domain}}']],
362 [host.check, ['http://{{domain}}']],
363 [host.open, '{{protocol}}://{{domain}}:{{proxyport}}'],
364 [events.publish, ['MESSAGE', ['Server ready!!!']], true]
365
366 ]]
367
368 ]],
369
370 [events.publish, ['DOCKER_STARTED'], true],
371 [events.publish, ['STEP', ['build_project']]],
372 [tools.track, ['ProjectStatus', 'Completed', '', 4]],
373 [host.check, ['{{protocol}}://{{domain}}', true]],
374 [events.publish, ['STEP', ['open_browser']]],
375 [events.publish, ['PROJECT_COMPLETED'], true],
376 [exit, '']
377
378 ];
379
380 promises.add(promisesPack);
381 promises.start();
382
383}
384
385function exit(text){
386
387 console.log(text);
388 process.exit();
389
390}
391
392function showVerbose(systemMessage, force){
393 if(verbose || force){
394 tools.showVerbose(systemMessage);
395 }
396}
397
398function showError(number){
399
400 tools.showError(number);
401 tools.showStep('cleaner');
402
403 events.unsubscribe('VERBOSE', showVerbose);
404 events.unsubscribe('WARNING', tools.showWarning);
405 events.unsubscribe('STEP', tools.showStep);
406 events.unsubscribe('MESSAGE', tools.showMessage);
407
408 process.exit();
409
410}