-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathsubway.js
More file actions
executable file
·107 lines (88 loc) · 3.71 KB
/
Copy pathsubway.js
File metadata and controls
executable file
·107 lines (88 loc) · 3.71 KB
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
#!/usr/bin/env node
/* The Subway IRC client
* Written By: David Petersen (thedjpetersen)
*
* This is a persistent web based IRC client that is targeted
* to those new to IRC.
*/
// Basic dependencies. We use Express/HTTP to serve our content.
// Modern async/await patterns are used for flow control instead of callbacks.
const express = require("express");
const http = require("http");
const bower = require("bower");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const { promisify } = require("util");
const server_settings = require("./settings/server");
// Get current environment
const env = process.env.IRC_ENV || "dev";
// These are our local lib files. The initialize function in our plugin module
// fetches the different plugins(github gists) we have and downloads them to
// the plugin_cache directory.
//
// The static module includes all of our grunt configuration.
// This takes care of any code preprocessing(grunt/stylus/Jade)
// as well code concatenation/minification.
//
// The connection module handles any interaction between the client and the server
// all incoming IRC commands are handled here. It also handles IRC logging
// and any other info that needs to be sent to the client(plugin info or settings)
const init_plugins = require("./lib/plugins").initialize;
const static = require("./lib/static");
const connection = require("./lib/connection");
const cwd = __dirname;
// Promisify the init_plugins callback-based function
const init_plugins_async = promisify(init_plugins);
// Wrapper to promisify bower.commands.install()
const installBowerDependencies = () => {
return new Promise((resolve, reject) => {
console.log("Downloading dependencies...");
bower.commands.install()
.on("end", (results) => resolve(results))
.on("error", (err) => reject(err));
});
};
// Promisify the static build function
const buildStatic = promisify(static);
// Main startup function using async/await
async function startSubway() {
try {
// Step 1: Fetch different plugins from the github gists
// and save them to the plugin_cache directory
console.log("Fetching Subway plugins...");
// TODO: resolve Fatal error: getaddrinfo ENOTFOUND
// when we don't have active internet connection
await init_plugins_async();
// Step 2: Download all third party dependencies or upgrade any if
// the configuration has changed. These are all client side dependencies
// like jQuery, Backbone or React.
await installBowerDependencies();
// Step 3: Compile any preprocessed code that we need to like React components
// and Stylus stylesheets
console.log("Compiling static resources...");
await buildStatic();
// All static content is placed in the ./tmp directory
// we use this directory as the root of our server
const app = express()
.use(bodyParser.urlencoded({ extended: true }))
.use(cookieParser(server_settings.cookie_secret || "subway_secret"))
.use(express.static(cwd + "/tmp"));
app.set("views", __dirname + "/tmp");
app.engine("ejs", require("ejs").renderFile);
const server = http.Server(app);
const io = require("socket.io")(server);
// Get the port from the server settings based on environment
const port = server_settings[env] ? server_settings[env].port : server_settings.dev.port;
server.listen(port, () => {
console.log(`Subway IRC client listening on port ${port}`);
});
// Pass the socket.io listener to the connection module
// so it can handle incoming events and emit different actions
connection(io, app);
} catch (err) {
console.error("Error starting Subway:", err);
process.exit(1);
}
}
// Start the application
startSubway();