-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhot-reload.js
174 lines (166 loc) · 6.45 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
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
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
}
}
});
// }
// if (stats.isDirectory()) {
// // If the file is a directory, recursively watch it unless its src/scripts as it contains ts files and will trigger changes on both src/scripts and src/dist
// if (fullPath !== "src/scripts") {
// console.log(
// `${GREEN} Watching Folder and its Files: ${YELLOW}${fullPath}`
// );
// watchDirectory(fullPath);
// if (fullPath !== "src/dist/scripts") {
// fs.watch(fullPath, { persistent: true }, (eventType,fname) => {
// if (eventType === "rename") {
// // Reload the extension in the browser
// console.log(`${YELLOW}\n------------------------------------------------------------`);
// console.log(`${GREEN}Folder Content Changed: ${fullPath}/${fname}`);
// console.log(`${RED}Reloading extension with id: ${extensionId}`);
// console.log(
// `${YELLOW}------------------------------------------------------------\n${RESET}`
// );
// exec(`google-chrome-stable --reload-extension=${extensionId}`);
// }
// });
// }
// }
// } else {
// let debounceTimeout = null;
// // If the file is a file, watch it for changes
// fs.watch(fullPath, { persistent: false }, (e, f) => {
// // Reload the extension in the browser
// if (e === "change") {
// if (fullPath !== "src/manifest.json") {
// clearTimeout(debounceTimeout);
// debounceTimeout = setTimeout(() => {
// console.log(`${YELLOW}\n------------------------------------------------------------`);
// console.log(`${GREEN}File Event occurred: ${YELLOW}${fullPath}`);
// console.log(`${RED}Reloading extension with id: ${YELLOW}${extensionId}`);
// console.log(
// `${YELLOW}------------------------------------------------------------\n${RESET}`
// );
// exec(`google-chrome-stable --reload-extension=${extensionId}`);
// }, 100);
// } else {
// clearTimeout(debounceTimeout);
// debounceTimeout = setTimeout(() => {
// console.log(`${YELLOW}\n--------------------------------`);
// console.log(`${GREEN}File Event Occured on Manifest.json:${YELLOW}${fullPath}`);
// console.log(
// `${RED} YOU NEED TO MANUALLY RELOAD IT: ${YELLOW}chrome://extensions/`
// );
// console.log(
// `${YELLOW}--------------------------------\n${RESET}`
// );
// }, 100);
// }
// }
// });
// }
});
}
watchDirectory("src");