2021-02-05 15:33:21 +08:00
|
|
|
const chokidar = require("chokidar");
|
|
|
|
const ws = require("./server");
|
|
|
|
const fs = require("fs");
|
|
|
|
const doMerge = require("./command").doMerge;
|
|
|
|
const { exec } = require("child_process");
|
|
|
|
require("colors");
|
|
|
|
require("shelljs/global");
|
2019-12-04 14:16:57 +08:00
|
|
|
|
2021-02-05 15:33:21 +08:00
|
|
|
const tscProcess = exec("node node_modules/.bin/tsc -w -p .");
|
|
|
|
const rollupProcess = exec("node node_modules/.bin/rollup -c -w");
|
|
|
|
|
|
|
|
console.warn("Waiting ...");
|
2019-12-04 14:16:57 +08:00
|
|
|
|
|
|
|
setTimeout(() => {
|
2021-02-05 15:33:21 +08:00
|
|
|
console.warn("Start watching");
|
|
|
|
ws.listen(7777);
|
|
|
|
chokidar
|
|
|
|
.watch(process.cwd() + "/bundle", {
|
|
|
|
ignored: /(^|[\/\\])\../,
|
|
|
|
awaitWriteFinish: true,
|
|
|
|
alwaysStat: true,
|
|
|
|
})
|
|
|
|
.on("change", (path) => {
|
|
|
|
if (ws.debugging) {
|
|
|
|
console.log("debugging, hot reload by pass");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fs.readFile(path, "utf-8", (error, data) => {
|
|
|
|
if (!path.endsWith(".map")) {
|
|
|
|
try {
|
|
|
|
const sourceMap = doMerge(path + ".map");
|
|
|
|
ws.connections.forEach((e) => {
|
|
|
|
e.sendText(
|
|
|
|
JSON.stringify({
|
|
|
|
cmd: "RELOAD",
|
|
|
|
script: data,
|
|
|
|
source: path.match(/[^/\\]*$/)[0],
|
|
|
|
sourceMap,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2019-12-11 15:14:29 +08:00
|
|
|
}
|
2021-02-05 15:33:21 +08:00
|
|
|
});
|
2019-12-04 14:16:57 +08:00
|
|
|
});
|
|
|
|
}, 3000);
|
2021-02-05 15:33:21 +08:00
|
|
|
const os = require("os");
|
2019-12-04 14:16:57 +08:00
|
|
|
|
|
|
|
function getIPAdress() {
|
2021-02-05 15:33:21 +08:00
|
|
|
const ret = [];
|
|
|
|
var interfaces = os.networkInterfaces();
|
|
|
|
for (var devName in interfaces) {
|
|
|
|
var iface = interfaces[devName];
|
|
|
|
for (var i = 0; i < iface.length; i++) {
|
|
|
|
var alias = iface[i];
|
|
|
|
if (
|
|
|
|
alias.family === "IPv4" &&
|
|
|
|
alias.address !== "127.0.0.1" &&
|
|
|
|
!alias.internal
|
|
|
|
) {
|
|
|
|
ret.push(alias.address);
|
|
|
|
}
|
2019-12-04 14:16:57 +08:00
|
|
|
}
|
2021-02-05 15:33:21 +08:00
|
|
|
}
|
|
|
|
return ret;
|
2019-12-04 14:16:57 +08:00
|
|
|
}
|
|
|
|
|
2021-02-05 15:33:21 +08:00
|
|
|
const qrcode = require("qrcode-terminal");
|
2019-12-04 14:16:57 +08:00
|
|
|
|
2021-02-05 15:33:21 +08:00
|
|
|
const ips = getIPAdress();
|
|
|
|
ips.forEach((e) => {
|
|
|
|
console.log(`IP:${e}`);
|
|
|
|
qrcode.generate(e, { small: false });
|
|
|
|
});
|
2019-12-04 14:16:57 +08:00
|
|
|
|
2021-02-05 15:33:21 +08:00
|
|
|
const keypress = require("keypress");
|
2019-12-04 14:16:57 +08:00
|
|
|
|
|
|
|
keypress(process.stdin);
|
2021-02-05 15:33:21 +08:00
|
|
|
process.stdin.on("keypress", function (ch, key) {
|
|
|
|
if (key && key.ctrl && key.name == "r") {
|
|
|
|
ips.forEach((e) => {
|
|
|
|
console.log(`IP:${e}`);
|
|
|
|
qrcode.generate(e, { small: false });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (key && key.ctrl && key.name == "c") {
|
|
|
|
process.stdin.pause();
|
|
|
|
tscProcess.kill("SIGABRT");
|
|
|
|
rollupProcess.kill("SIGABRT");
|
|
|
|
process.exit(0);
|
|
|
|
}
|
2019-12-04 14:16:57 +08:00
|
|
|
});
|
|
|
|
process.stdin.setRawMode(true);
|
2021-02-05 15:33:21 +08:00
|
|
|
process.stdin.resume();
|