1 | #! /usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | 'use strict';
|
19 |
|
20 | var argv = require('minimist')(process.argv.slice(2));
|
21 | var prompt = require('readline-sync');
|
22 | var color = require('colors');
|
23 | var datef = require('dateformat');
|
24 | var mkdirp = require('mkdirp');
|
25 | var path = require('path');
|
26 | var uuid = require('uuid/v4');
|
27 |
|
28 | global.envPaths = {
|
29 |
|
30 | 'base': __dirname,
|
31 | 'libraries': path.join(__dirname, 'lib')
|
32 |
|
33 | }
|
34 |
|
35 | var variables = require(path.join(envPaths.libraries, 'variables.js'));
|
36 | var tools = require(path.join(envPaths.libraries, 'tools.js'));
|
37 | var events = require(path.join(envPaths.libraries, 'pubsub.js'));
|
38 | var m_docker = require(path.join(envPaths.libraries, 'docker.js'));
|
39 | var host = require(path.join(envPaths.libraries, 'host.js'));
|
40 | var cross = require(path.join(envPaths.libraries, 'crossExec.js'));
|
41 | var promises = require(path.join(envPaths.libraries, 'promises.js'));
|
42 | var commit = require(path.join(envPaths.libraries, 'commit.js'));
|
43 | var plugins = require(path.join(envPaths.libraries, 'plugins.js'));
|
44 | var credentials = require(path.join(envPaths.libraries, 'login.js'));
|
45 |
|
46 | var gorillaPath = variables.gorillaPath;
|
47 | var gorillaFolder = variables.gorillaFolder;
|
48 | var gorillaTemplateFolder = variables.gorillaTemplateFolder;
|
49 | var gorillaFile = variables.gorillaFile;
|
50 | var messagesFile = variables.messagesFile;
|
51 | var projectPath = variables.projectPath;
|
52 | var homeUserPathBash = variables.homeUserPathBash;
|
53 | var homeUserPathNodeJS = variables.homeUserPathNodeJS;
|
54 | var hostsFile = variables.hostsFile;
|
55 | var commonPath = variables.commonPath;
|
56 | var workingPath = variables.workingPath;
|
57 | var composeFile = variables.composeFile;
|
58 | var proxyName = variables.proxyName;
|
59 | var proxyHost = variables.proxyHost;
|
60 | var env = variables.env;
|
61 | var verbose = variables.verbose;
|
62 | var templateOptions = variables.templateOptions;
|
63 | var templateRepos = variables.templateRepos;
|
64 |
|
65 | global.envPaths.plugins = path.join(homeUserPathNodeJS, proxyName, 'plugins');
|
66 |
|
67 | events.subscribe('ERROR', showError);
|
68 | events.subscribe('VERBOSE', showVerbose);
|
69 | events.subscribe('WARNING', tools.showWarning);
|
70 | events.subscribe('STEP', tools.showStep);
|
71 | events.subscribe('MESSAGE', tools.showMessage);
|
72 | events.subscribe('ANSWER', tools.answer);
|
73 |
|
74 | checkUserInput();
|
75 |
|
76 | function 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 |
|
216 | function 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 |
|
240 | function 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 |
|
298 |
|
299 |
|
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 |
|
355 | [host.check, ['http://{{domain}}']],
|
356 | [host.open, '{{protocol}}://{{domain}}'],
|
357 | [events.publish, ['MESSAGE', ['Server ready!!!']], true]
|
358 |
|
359 | ], [
|
360 |
|
361 |
|
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 |
|
385 | function exit(text){
|
386 |
|
387 | console.log(text);
|
388 | process.exit();
|
389 |
|
390 | }
|
391 |
|
392 | function showVerbose(systemMessage, force){
|
393 | if(verbose || force){
|
394 | tools.showVerbose(systemMessage);
|
395 | }
|
396 | }
|
397 |
|
398 | function 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 | }
|