create-medan-app 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/templates.js +47 -22
- package/package.json +1 -1
package/dist/templates.js
CHANGED
|
@@ -22,44 +22,64 @@ export const DEV_MJS = `// dev.mjs
|
|
|
22
22
|
import { spawnSync } from "node:child_process";
|
|
23
23
|
import { watch, readFileSync, existsSync } from "node:fs";
|
|
24
24
|
import { createServer } from "node:http";
|
|
25
|
+
import { networkInterfaces } from "node:os";
|
|
25
26
|
|
|
26
27
|
const PORT = ${DEV_PORT};
|
|
27
|
-
|
|
28
|
+
const clients = new Set();
|
|
28
29
|
|
|
29
|
-
function
|
|
30
|
-
|
|
30
|
+
function fmtMs(ms) {
|
|
31
|
+
return ms >= 1000 ? (ms / 1000).toFixed(1) + "s" : Math.round(ms) + "ms";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function localIp() {
|
|
35
|
+
const nets = networkInterfaces();
|
|
36
|
+
for (const name of Object.keys(nets)) {
|
|
37
|
+
for (const net of nets[name] ?? []) {
|
|
38
|
+
if (net.family === "IPv4" && !net.internal) return net.address;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function build(quiet) {
|
|
45
|
+
const start = Date.now();
|
|
46
|
+
if (!quiet) console.log("○ compiling main.medan ...");
|
|
31
47
|
const result = spawnSync("medan", ["main.medan"], { stdio: "inherit" });
|
|
32
48
|
if (result.status !== 0) {
|
|
33
|
-
console.error("
|
|
49
|
+
console.error("\\n✗ gagal compile, benerin dulu terus save lagi.\\n");
|
|
34
50
|
return;
|
|
35
51
|
}
|
|
36
|
-
|
|
52
|
+
console.log(\`✓ Compiled in \${fmtMs(Date.now() - start)}\`);
|
|
53
|
+
for (const res of clients) res.write("data: reload\\n\\n");
|
|
37
54
|
}
|
|
38
55
|
|
|
39
56
|
const RELOAD_SCRIPT = \`
|
|
40
57
|
<script>
|
|
41
58
|
(function () {
|
|
42
|
-
var
|
|
43
|
-
|
|
44
|
-
fetch("/__medan-version")
|
|
45
|
-
.then(function (r) { return r.text(); })
|
|
46
|
-
.then(function (v) {
|
|
47
|
-
if (current === null) current = v;
|
|
48
|
-
else if (v !== current) location.reload();
|
|
49
|
-
})
|
|
50
|
-
.catch(function () {});
|
|
51
|
-
}, 400);
|
|
59
|
+
var es = new EventSource("/__medan-events");
|
|
60
|
+
es.onmessage = function () { location.reload(); };
|
|
52
61
|
})();
|
|
53
62
|
</script>
|
|
54
63
|
\`;
|
|
55
64
|
|
|
56
65
|
const server = createServer((req, res) => {
|
|
57
|
-
if (req.url === "/__medan-
|
|
58
|
-
res.writeHead(200, {
|
|
59
|
-
|
|
66
|
+
if (req.url === "/__medan-events") {
|
|
67
|
+
res.writeHead(200, {
|
|
68
|
+
"content-type": "text/event-stream",
|
|
69
|
+
"cache-control": "no-cache",
|
|
70
|
+
"connection": "keep-alive",
|
|
71
|
+
});
|
|
72
|
+
res.write(\`retry: 500\\n\\n\`);
|
|
73
|
+
clients.add(res);
|
|
74
|
+
req.on("close", () => clients.delete(res));
|
|
60
75
|
return;
|
|
61
76
|
}
|
|
62
77
|
|
|
78
|
+
const start = Date.now();
|
|
79
|
+
res.on("finish", () => {
|
|
80
|
+
console.log(\` \${req.method} \${req.url} \${res.statusCode} in \${fmtMs(Date.now() - start)}\`);
|
|
81
|
+
});
|
|
82
|
+
|
|
63
83
|
if (!existsSync("landing.html")) {
|
|
64
84
|
res.writeHead(200, { "content-type": "text/html" });
|
|
65
85
|
res.end("<p>lagi nge-generate landing.html, tunggu bentar terus refresh...</p>");
|
|
@@ -71,16 +91,21 @@ const server = createServer((req, res) => {
|
|
|
71
91
|
res.end(html);
|
|
72
92
|
});
|
|
73
93
|
|
|
74
|
-
|
|
94
|
+
const bootStart = Date.now();
|
|
95
|
+
build(true);
|
|
96
|
+
|
|
75
97
|
server.listen(PORT, () => {
|
|
76
|
-
|
|
77
|
-
console.log("
|
|
98
|
+
const ip = localIp();
|
|
99
|
+
console.log("\\n▲ Medan-lang dev");
|
|
100
|
+
console.log(\` - Local: http://localhost:\${PORT}\`);
|
|
101
|
+
if (ip) console.log(\` - Network: http://\${ip}:\${PORT}\`);
|
|
102
|
+
console.log(\`\\n✓ Ready in \${fmtMs(Date.now() - bootStart)}\\n\`);
|
|
78
103
|
});
|
|
79
104
|
|
|
80
105
|
let timer;
|
|
81
106
|
watch("main.medan", () => {
|
|
82
107
|
clearTimeout(timer);
|
|
83
|
-
timer = setTimeout(build, 100);
|
|
108
|
+
timer = setTimeout(() => build(false), 100);
|
|
84
109
|
});
|
|
85
110
|
`;
|
|
86
111
|
export function mainMedan(name) {
|