1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
11 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
12 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
13 | function verb(n) { return function (v) { return step([n, v]); }; }
|
14 | function step(op) {
|
15 | if (f) throw new TypeError("Generator is already executing.");
|
16 | while (_) try {
|
17 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
18 | if (y = 0, t) op = [op[0] & 2, t.value];
|
19 | switch (op[0]) {
|
20 | case 0: case 1: t = op; break;
|
21 | case 4: _.label++; return { value: op[1], done: false };
|
22 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
23 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
24 | default:
|
25 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
26 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
27 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
28 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
29 | if (t[2]) _.ops.pop();
|
30 | _.trys.pop(); continue;
|
31 | }
|
32 | op = body.call(thisArg, _);
|
33 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
34 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
35 | }
|
36 | };
|
37 | var _this = this;
|
38 | Object.defineProperty(exports, "__esModule", { value: true });
|
39 |
|
40 | var dependency_injection_1 = require("@tractor/dependency-injection");
|
41 | var error_handler_1 = require("@tractor/error-handler");
|
42 | var logger_1 = require("@tractor/logger");
|
43 | var bodyParser = require("body-parser");
|
44 | var cors = require("cors");
|
45 | var express = require("express");
|
46 | var fs = require("fs");
|
47 | var http_1 = require("http");
|
48 | var template = require("lodash.template");
|
49 | var path = require("path");
|
50 | var io = require("socket.io");
|
51 | var terminal_link_1 = require("terminal-link");
|
52 |
|
53 | var get_config_1 = require("./api/get-config");
|
54 | var get_plugins_1 = require("./api/get-plugins");
|
55 | var run_protractor_1 = require("./api/run-protractor");
|
56 | var search_1 = require("./api/search");
|
57 |
|
58 | var PARAMETER_LIMIT = 50000;
|
59 | var UPLOAD_SIZE_LIMIT = '50mb';
|
60 | exports.start = dependency_injection_1.inject(function (tractor, server) { return __awaiter(_this, void 0, void 0, function () {
|
61 | var e_1;
|
62 | return __generator(this, function (_a) {
|
63 | switch (_a.label) {
|
64 | case 0:
|
65 | _a.trys.push([0, 2, , 3]);
|
66 |
|
67 |
|
68 |
|
69 | return [4 , Promise.all(tractor.plugins.map(function (plugin) { return tractor.call(plugin.run); }))];
|
70 | case 1:
|
71 |
|
72 |
|
73 |
|
74 | _a.sent();
|
75 | return [3 , 3];
|
76 | case 2:
|
77 | e_1 = _a.sent();
|
78 |
|
79 | console.error(e_1);
|
80 | throw new error_handler_1.TractorError('Could not run plugin.');
|
81 | case 3:
|
82 | server.listen(tractor.config.port, function () {
|
83 | var link = terminal_link_1.default('tractor', "http://localhost:" + server.address().port, {
|
84 | fallback: function (_, url) { return url; }
|
85 | });
|
86 | logger_1.info("tractor is running at " + link);
|
87 | });
|
88 | return [2 ];
|
89 | }
|
90 | });
|
91 | }); }, 'tractor', 'server');
|
92 | exports.init = dependency_injection_1.inject(function (tractor) { return __awaiter(_this, void 0, void 0, function () {
|
93 | var application, server, sockets, templatePath, dir, renderIndex, e_2;
|
94 | return __generator(this, function (_a) {
|
95 | switch (_a.label) {
|
96 | case 0:
|
97 | application = express();
|
98 | server = new http_1.Server(application);
|
99 | sockets = io(server);
|
100 | tractor.constant({ application: application, server: server, sockets: sockets });
|
101 |
|
102 | application.use(bodyParser.json({
|
103 | limit: UPLOAD_SIZE_LIMIT
|
104 | }));
|
105 | application.use(bodyParser.urlencoded({
|
106 | extended: true,
|
107 | limit: UPLOAD_SIZE_LIMIT,
|
108 | parameterLimit: PARAMETER_LIMIT
|
109 | }));
|
110 | application.use(cors());
|
111 | try {
|
112 | templatePath = require.resolve('@tractor/ui');
|
113 | dir = path.dirname(templatePath);
|
114 | }
|
115 | catch (e) {
|
116 |
|
117 | console.error(e);
|
118 | throw new error_handler_1.TractorError('"@tractor/ui" is not installed.');
|
119 | }
|
120 | renderIndex = injectPlugins(tractor, application, templatePath);
|
121 | application.get('/', renderIndex);
|
122 | application.use(express.static(dir));
|
123 | application.get('/config', get_config_1.getConfigHandler(tractor));
|
124 | application.get('/plugins', get_plugins_1.getPluginsHandler(tractor));
|
125 | application.get('/search', search_1.searchHandler());
|
126 | sockets.of('/run-protractor')
|
127 | .on('connection', run_protractor_1.runProtractorHandler(tractor));
|
128 | sockets.of('/server-status');
|
129 | _a.label = 1;
|
130 | case 1:
|
131 | _a.trys.push([1, 3, , 4]);
|
132 |
|
133 |
|
134 |
|
135 | return [4 , Promise.all(tractor.plugins.map(function (plugin) { return tractor.call(plugin.serve); }))];
|
136 | case 2:
|
137 |
|
138 |
|
139 |
|
140 | _a.sent();
|
141 | return [3 , 4];
|
142 | case 3:
|
143 | e_2 = _a.sent();
|
144 |
|
145 | console.error(e_2);
|
146 | throw new error_handler_1.TractorError('Could not serve plugin.');
|
147 | case 4:
|
148 |
|
149 | application.get('*', renderIndex);
|
150 | return [2 ];
|
151 | }
|
152 | });
|
153 | }); }, 'tractor');
|
154 | function injectPlugins(tractor, application, templatePath) {
|
155 | var pluginsWithUI = tractor.plugins.filter(function (plugin) { return plugin.description.hasUI; });
|
156 | var UP_TO_NODE_MODULE = '../../../../';
|
157 | pluginsWithUI.forEach(function (plugin) {
|
158 | application.use(express.static(path.resolve(plugin.script, UP_TO_NODE_MODULE)));
|
159 | });
|
160 | return function (_, response) {
|
161 | var createTemplate = template(fs.readFileSync(templatePath, 'utf8'));
|
162 | var scripts = pluginsWithUI.map(function (plugin) {
|
163 | var nodeModuleDir = path.resolve(plugin.script, UP_TO_NODE_MODULE);
|
164 | return plugin.script.replace(nodeModuleDir, '');
|
165 | });
|
166 | var rendered = createTemplate({ scripts: scripts });
|
167 | response.header('Content-Type', 'text/html');
|
168 | response.send(rendered);
|
169 | };
|
170 | }
|
171 |
|
\ | No newline at end of file |