-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
executable file
·258 lines (214 loc) · 6.48 KB
/
cli.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env node
import os from "os";
import fs from "fs";
import url from "url";
import express from "express";
import livereload from "livereload";
import connectLivereload from "connect-livereload";
import serveIndex from "serve-index";
import yargs from "yargs/yargs";
import { hideBin } from "yargs/helpers";
import ejs from "ejs";
import PouchDB from "pouchdb";
import ExpressPutchDBFactory from "express-pouchdb";
let config;
// Read the content of package.json
const packageJsonPath = url.fileURLToPath(
import.meta.resolve("./package.json")
);
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf8");
const packageJson = JSON.parse(packageJsonContent);
// Access the version
const cliVersion = packageJson.version;
// to override the default config, create a config.json file
// in the root folder of your project
const default_config = {
port: 2525,
dbpath: "db",
livereload: false,
};
try {
// Read the contents of config.json synchronously
const data = fs.readFileSync("config.json", "utf8");
try {
// Parse the JSON config data
config = { ...default_config, ...JSON.parse(data) };
} catch (jsonError) {
console.error("Error parsing config.json:", jsonError);
process.exit(1);
}
} catch (fileError) {
// use default config when config.json is not found
config = default_config;
}
// CLI arguments
yargs(hideBin(process.argv))
.scriptName("npx @streamerjs/streamerjs")
.usage("$0 <cmd> [args]")
.command(
["$0", "start"],
"start StreamerJS application",
(yargs) => {},
start
)
.command(
"create-scene [file-name]",
"create a new scene in /scenes/ folder",
{
fileName: {
alias: "f",
default: "index.html",
},
},
(argv) => {
createScene(argv.fileName);
process.exit(0);
}
)
.command(
"create-control-panel [file-name]",
"create a new control panel in /control/ folder",
{
fileName: {
alias: "f",
default: "index.html",
},
},
(argv) => {
createControlPanel(argv.fileName);
process.exit(0);
}
)
.help()
.wrap(null)
.version(cliVersion).argv;
function createScene(fileName) {
if (!fs.existsSync("scenes")) {
console.log("Creating /scenes/ folder");
fs.mkdirSync("scenes");
}
console.log(`Creating a new scene file: scenes/${fileName}`);
if (fs.existsSync(`scenes/${fileName}`)) {
console.error(`Error: Scene ${fileName} already exists in /scenes/ folder`);
process.exit(1);
}
fs.copyFileSync(
url.fileURLToPath(import.meta.resolve("./boilerplate/scene/index.html")),
`scenes/${fileName}`
);
}
function createControlPanel(fileName) {
if (!fs.existsSync("control")) {
console.log("Creating /control/ folder");
fs.mkdirSync("control");
}
console.log(`Creating a new control panel file: control/${fileName}`);
if (fs.existsSync(`control/${fileName}`)) {
console.error(
`Error: Control panel ${fileName} already exists in /control/ folder`
);
process.exit(1);
}
fs.copyFileSync(
url.fileURLToPath(import.meta.resolve("./boilerplate/control/index.html")),
`control/${fileName}`
);
}
function start() {
const insecurePort = config.port || process.env.PORT;
const app = express();
// Scenes in user's project
app.use(
"/scenes/",
express.static("scenes"),
serveIndex("scenes", { icons: true })
);
app.use("/assets/", express.static("assets"));
if (config.livereload) {
// Setup livereload
const liveReloadServer = livereload.createServer();
// Use connect-livereload middleware
app.use(connectLivereload());
liveReloadServer.watch("./scenes/");
// Assets in user's project
liveReloadServer.watch("./assets/");
console.log("Livereload enabled for /scenes/ and /assets/ folders");
}
// streamer resources
const templates = url.fileURLToPath(import.meta.resolve("./templates/"));
app.set("view engine", "ejs").set("views", templates);
let enableControlPanel = false;
// check if control folder exists to indicate that the user wants to create the control panel(s)
if (fs.existsSync("control")) {
enableControlPanel = true;
// Check if the db folder exists, create it if it doesn't
if (!fs.existsSync(config.dbpath)) {
try {
fs.mkdirSync(config.dbpath, { recursive: true });
console.log(`PouchDB database folder created at: ${config.dbpath}`);
} catch (error) {
console.error(
`Error creating PouchDB database folder: ${error.message}`
);
}
}
const StreamerPouchDB = PouchDB.defaults({ prefix: config.dbpath + "/" });
new StreamerPouchDB("streamer");
// Control panel resources in user's project
app.use("/control/", express.static("control"));
if (config.livereload) {
liveReloadServer.watch("./control/");
console.log("Livereload enabled for /control/ folder");
}
/**
* Server paths
*/
const pouchDBLibPath = url.fileURLToPath(
import.meta.resolve("Pouchdb/dist/")
);
// PouchDB client library
app.use("/_resources/pouchdb/", express.static(pouchDBLibPath));
const pouchApp = ExpressPutchDBFactory(StreamerPouchDB, {
logPath: config.dbpath + "/log.txt",
configPath: config.dbpath + "/config.json",
});
// PouchDB server
app.use("/_db", pouchApp);
}
// Get network interfaces
const networkInterfaces = os.networkInterfaces();
const ips = [];
// Iterate over each network interface
Object.keys(networkInterfaces).forEach((interfaceName) => {
const interfaces = networkInterfaces[interfaceName];
// Iterate over each interface
interfaces.forEach((interfaceInfo) => {
// Check if the address is an IPv4
if (interfaceInfo.family === "IPv4") {
ips.push(interfaceInfo.address);
}
});
});
// Server index linking to other parts of the server
app.get("/", (req, res) => {
app.engine("ejs", ejs.renderFile);
res.render("index", { control: enableControlPanel });
});
// Assets in user's project
app.use(
"/_resources/",
express.static(url.fileURLToPath(import.meta.resolve("./resources/")))
);
// HTTP server
app.listen(insecurePort);
console.log(`StreamerJS (v${cliVersion}) server listening on:`);
ips.forEach((ip) => {
console.log(`http://${ip}:${insecurePort}`);
});
if (enableControlPanel) {
console.log("\nControl your stream by opening:");
ips.forEach((ip) => {
console.log(`http://${ip}:${insecurePort}/control/`);
});
}
}