-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev.js
76 lines (64 loc) · 1.78 KB
/
dev.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
const express = require("express");
const { exec } = require("child_process");
const chokidar = require("chokidar");
const cors = require("cors");
let port = 3701;
const path = () => `http://localhost:${port}/addon.json`;
// Execute build command
const runBuild = () => {
console.log("Running build...");
exec("node build.js --dev", (error, stdout, stderr) => {
if (error) {
console.log(`Error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log("Build complete.");
// log addon.json file path and copy it to the clipboard
console.log("addon.json file path:");
console.log(path());
});
};
// Run initial build
runBuild();
// Watch for file changes in the src directory
const watcher = chokidar.watch("src", {
ignored: /(^|[\/\\])\../,
persistent: true,
});
watcher.on("change", (path) => {
console.log(`File ${path} has been changed. Re-running build...`);
runBuild();
});
// Create an express application
const app = express();
// Enable all CORS requests
app.use(cors());
// Serve static files from the 'export' directory
app.use(express.static("export"));
// Start the server
function tryListen() {
app.listen(port, () => {
console.log("Server is running at http://localhost:" + port);
// log addon.json file path and copy it to the clipboard
console.log("addon.json file path:");
console.log(path);
// copy to clipboard
exec(`echo ${path} | clip`);
});
}
process.on("uncaughtException", function (err) {
if (err.code === "EADDRINUSE") {
console.log(`Port ${port} is already in use. Trying another port...`);
port++;
tryListen();
} else {
console.log(err);
process.exit(1);
}
});
tryListen();