1 | var http = require('http');
|
2 | var https = require('https');
|
3 | var _ = require('lodash');
|
4 | var fs = require('fs');
|
5 | var path = require('path');
|
6 | var async = require('async');
|
7 | var formatUrl = require('url').format;
|
8 | var api = require('../lib/api');
|
9 | var querystring = require('querystring');
|
10 | var openBrowser = require('open');
|
11 | var basedir = require('../lib/basedir');
|
12 | var log = require('../lib/log');
|
13 | var debug = require('debug')('4front:cli:dev-sandbox');
|
14 | var helper = require('../lib/helper');
|
15 | var sandboxServer = require('../lib/sandbox-server');
|
16 | var spawn = require('../lib/spawn');
|
17 | var basedir = require('../lib/basedir');
|
18 | var express = require('express');
|
19 |
|
20 | module.exports = function(program, done) {
|
21 | _.defaults(program, {
|
22 | port: 3000,
|
23 | liveReload: program.virtualAppManifest.liveReload === true,
|
24 | cwd: process.cwd(),
|
25 | buildType: 'debug'
|
26 | });
|
27 |
|
28 | debug("running dev-sandbox command");
|
29 |
|
30 | if (program.release === true)
|
31 | program.buildType = 'release';
|
32 |
|
33 |
|
34 | if (_.contains(['debug', 'release'], program.buildType) === false) {
|
35 | return done("Invalid build-type option value. Valid values are 'debug' and 'release'.");
|
36 | }
|
37 |
|
38 | var asyncTasks = [];
|
39 |
|
40 |
|
41 | asyncTasks.push(function(cb) {
|
42 | if (program.buildType === 'release' && program.virtualAppManifest.scripts.build)
|
43 | spawn('npm', ['run-script', 'build'], cb);
|
44 | else
|
45 | cb();
|
46 | });
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | asyncTasks.push(function(cb) {
|
54 | basedir(program, function(err, baseDir) {
|
55 | if (err) return cb(err);
|
56 |
|
57 | debug("setting baseDir to %s", baseDir);
|
58 | program.baseDir = baseDir;
|
59 | cb();
|
60 | });
|
61 | });
|
62 |
|
63 | asyncTasks.push(function(cb) {
|
64 |
|
65 | var npmBuildCommand = 'build:' + program.buildType;
|
66 |
|
67 | if (program.virtualAppManifest.scripts[npmBuildCommand]) {
|
68 | log.debug("Found npm watch script");
|
69 | spawn('npm', ['run-script', npmBuildCommand], cb);
|
70 | }
|
71 | else
|
72 | cb();
|
73 | });
|
74 |
|
75 | asyncTasks.push(function(cb) {
|
76 | if (program.virtualAppManifest.scripts.watch) {
|
77 | log.debug("Found npm watch script");
|
78 | spawn('npm', ['run-script', 'watch'], {waitForExit: false}, cb);
|
79 | }
|
80 | else
|
81 | cb();
|
82 | });
|
83 |
|
84 | asyncTasks.push(function(cb) {
|
85 | debug("uploading the app manifest");
|
86 |
|
87 |
|
88 | var requestOptions = {
|
89 | method: 'post',
|
90 | path: '/dev/' + program.virtualApp.appId + '/manifest',
|
91 | body: _.omit(program.virtualAppManifest, 'scripts', 'appId')
|
92 | };
|
93 |
|
94 | api(program, requestOptions, cb);
|
95 | });
|
96 |
|
97 | var server;
|
98 | var sandboxUrl = buildSandboxUrl();
|
99 |
|
100 | asyncTasks.push(function(cb) {
|
101 | var localhost = sandboxServer(program);
|
102 |
|
103 | if (program.virtualApp.requireSsl) {
|
104 |
|
105 | httpsOptions = {
|
106 | key: fs.readFileSync(path.join(__dirname, '../certs', 'private.key')).toString(),
|
107 | cert: fs.readFileSync(path.join(__dirname, '../certs', 'localhost.crt')).toString(),
|
108 | rejectUnauthorized: false
|
109 | };
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | https.createServer(httpsOptions, localhost).listen(program.port, cb);
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | }
|
137 | else
|
138 | localhost.listen(program.port, cb);
|
139 | });
|
140 |
|
141 | async.series(asyncTasks, function(err) {
|
142 | if (err) return done(err);
|
143 |
|
144 |
|
145 | log.messageBox("The dev sandbox was launched in your browser with url:");
|
146 | log.writeln(sandboxUrl);
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | openBrowser(sandboxUrl);
|
158 |
|
159 | done(null, function() {
|
160 | if (localhost)
|
161 | localhost.stop();
|
162 | });
|
163 | });
|
164 |
|
165 | function buildSandboxUrl() {
|
166 | var devOptions = {
|
167 | buildType: program.buildType,
|
168 | token: program.profile.jwt.token,
|
169 | port: program.port
|
170 | };
|
171 |
|
172 | if (program.liveReload === true) {
|
173 | devOptions.liveReload = 1;
|
174 | }
|
175 |
|
176 | return formatUrl({
|
177 | protocol: program.virtualApp.requireSsl === true ? 'https' : 'http',
|
178 | host: program.virtualApp.name + '--dev.' + program.virtualHost,
|
179 | pathname: '/__login',
|
180 | query: devOptions
|
181 | });
|
182 | }
|
183 | };
|