-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
190 lines (172 loc) · 5.63 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
/*jshint node:true */
// the basic setup of the build tools is to first parse the configuration
// the configuration should contain specific settings for either dev or deploy / serve or build
//var vm = require('vm');
var util = require('util');
//var http = require('http');
//var repl = require('repl');
var dirname = __dirname; // dirname of this file
//var events = require('events');
var pathlib = require('path');
var env = require('sproutnode');
var files = [
'lib/core.js',
'lib/logger.js',
'lib/node_wrap.js',
'lib/proxy.js',
'lib/bt_socketio.js',
'lib/project.js',
'lib/file.js',
'lib/filetypes.js',
'lib/appbuilder.js',
'lib/framework.js',
'lib/theme.js',
'lib/installer.js'
];
// we need to install some new stuff in the running context
files.forEach(function (f) {
env.loadFile(pathlib.join(dirname, f));
});
module.exports.__env = env;
var loadScConfigs = function (projectpath, opts) {
var p = opts.configFile || 'sc_config';
if (p[0] !== '/') p = pathlib.join(projectpath, p);
env.loadFile(p);
};
module.exports.startDevServer = function (projectpath, opts) {
env.setPath('BT.runMode', "debug");
try {
env.setPath('BT.projectPath', projectpath);
env.setPath('BT.curPath', projectpath);
env.setPath('BT.btPath', dirname);
env.setPath('BT.startTime', Date.now());
if (opts.disableSocket) {
env.setPath('BT.noSocket', true);
}
if (opts.logFile || opts.daemonize) {
if (typeof opts.logFile !== 'string') opts.logFile = 'serve.log';
env.runCode("BT.Logger.logFile = '"+opts.logFile+"'");
}
if (opts.includeTests) {
// tests are off by default, change the appbuilder prototype to add tests to all loaded apps
env.runCode("BT.AppBuilder.prototype.includeTests = true");
}
// this needs to be done _before_ the configs are loaded, otherwise we never get the traces
// of the inits of the appBuilders or frameworks
if (opts.logLevel) {
env.runCode("BT.Logger.logOutputLevel = '"+opts.logLevel+"'");
}
loadScConfigs(projectpath, opts);
// this should actually load the config
//env.runCode("SC.Benchmark.verbose = true;");
if (opts.outputFiles) {
env.setPath("BT.outputFiles", opts.outputFiles);
}
if (opts.runBenchmarks) {
env.setPath("BT.runBenchmarks", true);
env.runCode("SC.Benchmark.start('BT_startup')");
env.runCode("SC.Benchmark.start('sc_config_load')");
}
if (opts.localOnly !== undefined) {
var lo;
// prevent injection of code from the command line
if (opts.localOnly === "true") {
lo = "true";
}
if (opts.localOnly === "false") {
lo = "false";
}
env.runCode("BT.serverConfig.localOnly = " + lo);
}
if (opts.port) {
var p = parseInt(opts.port, 10); // make sure this is a number
env.runCode("BT.serverConfig.port = " + p);
}
if (opts.daemonize) {
require('daemon')();
}
env.runCode("SC.run(function() { BT.projectManager.startServer(); })");
if (opts.hasREPL && !opts.daemonize) {
env.repl();
}
//env.runCode("console.log(__dirname);");
}
catch (e) {
util.log('error caught: ' + util.inspect(e, true, 10));
if (e.code === 'ENOENT') {
util.log("You did not create a valid project config file");
throw e;
}
else if (e.message.indexOf("EMFILE") > -1 && e.message.indexOf("Too many opened files") > -1) {
util.log("It seems your OS only allows a very limited number of open files. On OSX and Linux please run ulimit -n 4096");
process.exit(1);
//throw e;
}
else {
throw e;
}
}
};
module.exports.startInstall = function (projectpath, opts) {
env.setPath('BT.runMode', "install");
env.setPath('BT.projectPath', projectpath);
env.setPath('BT.curPath', projectpath);
env.setPath('BT.btPath', dirname);
var code = "SC.run(function () { BT.startInstall('";
code += opts.gitUrl + "', { ";
if (opts.isGlobal) code += "isGlobal: true, ";
if (opts.logLevel) {
code += "logLevel: '" + opts.logLevel + "', ";
}
if (opts.branch) {
code += "branch: '" + opts.branch + "'";
}
code += "}); });";
//util.log("about to run code: " + code);
try {
env.runCode(code);
}
catch (e) {
throw e;
}
};
module.exports.startBuild = function (projectpath, opts) {
var util = require('util');
env.setPath('BT.runMode', "build");
env.setPath('BT.projectPath', projectpath);
env.setPath('BT.curPath', projectpath);
env.setPath('BT.btPath', dirname);
env.setPath('BT.startTime', Date.now());
try {
if (opts.runBenchmarks) {
env.setPath("BT.runBenchmarks", true);
}
if (opts.logLevel) {
env.runCode("BT.Logger.logOutputLevel = '"+opts.logLevel+"'");
}
if (opts.apps.length > 0) {
env.runCode("BT.BUILDTARGETS = " + JSON.stringify(opts.apps));
}
loadScConfigs(projectpath, opts);
var code = "SC.run(function() { BT.projectManager.startBuild(" + JSON.stringify(opts) + "); });";
var r = env.runCode(code);
//util.log('return value of r: ' + util.inspect(r));
if (opts.hasREPL) env.repl();
if (r === "done" && !opts.hasREPL) process.exit(0);
}
catch (err) {
util.log('error caught: ' + util.inspect(err, true, 10));
if (err.code === 'ENOENT') {
util.log("You did not create a valid project config file");
throw err;
}
else if (err.message.indexOf("EMFILE") > -1 && err.message.indexOf("Too many opened files") > -1) {
util.log("It seems your OS only allows a very limited number of open files. On OSX and Linux please run ulimit -n 4096");
process.exit(1);
//throw e;
}
else {
throw err;
}
}
};