-
Notifications
You must be signed in to change notification settings - Fork 0
/
hot-reload.js
117 lines (110 loc) · 3.75 KB
/
hot-reload.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
const fs = require("fs");
const { exec } = require("child_process");
const path = require("path");
require("dotenv").config();
const WebSocket = require("ws");
const RESET = "\x1b[0m";
const RED = "\x1b[31m";
const GREEN = "\x1b[32m";
const YELLOW = "\x1b[33m";
const wss = new WebSocket.Server({ port: 3000 });
let ws;
let connectedClient = 0;
wss.on("connection", (_ws) => {
ws = _ws;
console.log(
`\n${GREEN}Connection: ${YELLOW}hot-reload.js to ${YELLOW}background.script.ts : ${GREEN}Established`
);
ws.on("message", (message) => {
connectedClient+=1
console.log(
`${GREEN}Client: ${YELLOW}${message} ${GREEN}IS CONNECTED`
);
console.log(
`${GREEN}Total Connected Clients: ${YELLOW}${connectedClient}`
);
});
console.log(`${GREEN}Connection is running on ${YELLOW}ws://localhost:8080`);
console.log(`${GREEN}HOT RELOADING IS WORKING\n\n`);
ws.on("close", (e) => {
console.log(e)
console.log(
`\n\n${RED}Extension Reloading Occured, Restarting Connection`
);
connectedClient-=1
console.log(
`${GREEN}Total Connected Clients: ${YELLOW}${connectedClient}`
);
});
});
let counter = 0; // for some reason w.send is being executed 3 times, so this is my hack, if you knew feel free to fix it
// Recursively traverse the src/ directory and watch for changes to all files
function watchDirectory(dir) {
let debounceTimeout = null;
fs.readdirSync(dir).forEach((file) => {
const fullPath = `${dir}/${file}`;
const stats = fs.statSync(fullPath);
// if (fullPath !== "src/scripts") {
console.log(`${GREEN} Watching Folder and its Files: ${YELLOW}${fullPath}`);
fs.watch(fullPath, { recursive: true, persistent: true }, (e, f) => {
// Reload the extension in the browser
const _path = path.resolve(f);
if (
fullPath !== "src/manifest.json"
) {
counter+=1
clearTimeout(debounceTimeout);
debounceTimeout = setTimeout(() => {
if (ws) {
console.log(
`${YELLOW}\n------------------------------------------------------------`
);
console.log(`${GREEN}File Event occurred: ${YELLOW}${_path}`);
console.log(`${RED}Reloading ${YELLOW}extension`);
console.log(
`${YELLOW}------------------------------------------------------------\n${RESET}`
);
} else {
console.log(`${YELLOW}\nConnection is not established yet`);
console.log(
`${YELLOW}HOT RELOAD NOT WORKING, TRY TO MANUALLY RELOAD THE EXTENSION\n`
);
}
// exec(`google-chrome-stable --update-extension=${extensionId}`);
}, 100);
if (ws && counter >=3) {
ws.send("reload");
counter=0
}
} else {
clearTimeout(debounceTimeout);
debounceTimeout = setTimeout(() => {
if (ws) {
console.log(
`${YELLOW}\n-------------------------------------------------------`
);
console.log(
`${GREEN}File Event Occured on Manifest.json: ${YELLOW}${_path}`
);
console.log(
`${RED} YOU NEED TO MANUALLY RELOAD IT: ${YELLOW}chrome://extensions/`
);
console.log(
`${YELLOW}-------------------------------------------------------\n${RESET}`
);
} else {
console.log(`${YELLOW}\nConnection is not established yet`);
console.log(
`${YELLOW}HOT RELOAD NOT WORKING, TRY TO MANUALLY RELOAD THE EXTENSION\n`
);
}
}, 100);
if (ws && counter >=3) {
ws.send("reload");
counter=0
}
}
});
});
}
watchDirectory("src");