create-medan-app 0.1.2 → 0.1.4

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/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { existsSync, mkdirSync, readdirSync, writeFileSync } from "node:fs";
3
3
  import { resolve } from "node:path";
4
- import { DEV_MJS, GITIGNORE, mainMedan, packageJson, readme } from "./templates.js";
4
+ import { DEV_MJS, DEV_PORT, GITIGNORE, mainMedan, packageJson, readme } from "./templates.js";
5
5
  function isValidPackageName(name) {
6
6
  return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(name);
7
7
  }
@@ -30,8 +30,7 @@ function main() {
30
30
  console.log("langkah selanjutnya:\n");
31
31
  console.log(` cd ${rawName}`);
32
32
  console.log(" npm install");
33
- console.log(" npm run dev # auto-regenerate tiap main.medan disave");
34
- console.log(" # atau: npm start # generate sekali doang");
35
- console.log(" # terus buka landing.html di browser");
33
+ console.log(` npm run dev # jalan di http://localhost:${DEV_PORT}, auto-reload`);
34
+ console.log(" # atau: npm start # generate landing.html sekali doang");
36
35
  }
37
36
  main();
package/dist/templates.js CHANGED
@@ -13,28 +13,101 @@ export function packageJson(name) {
13
13
  }
14
14
  `;
15
15
  }
16
+ export const DEV_PORT = 7777;
16
17
  export const DEV_MJS = `// dev.mjs
17
- // "npm run dev" jalanin file ini: generate landing.html sekali, terus
18
- // nungguin main.medan berubah dan regenerate otomatis tiap disave.
18
+ // "npm run dev" nge-generate landing.html, jalanin server di
19
+ // http://localhost:${DEV_PORT}, terus mantau main.medan dan auto-reload
20
+ // browser tiap kamu save.
19
21
 
20
22
  import { spawnSync } from "node:child_process";
21
- import { watch } from "node:fs";
23
+ import { watch, readFileSync, existsSync } from "node:fs";
24
+ import { createServer } from "node:http";
25
+ import { networkInterfaces } from "node:os";
22
26
 
23
- function build() {
24
- console.log("\\n[medan] generate landing.html ...");
27
+ const PORT = ${DEV_PORT};
28
+ let version = 0;
29
+
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 ...");
25
47
  const result = spawnSync("medan", ["main.medan"], { stdio: "inherit" });
26
48
  if (result.status !== 0) {
27
- console.error("[medan] ada error di atas, benerin dulu terus save lagi.");
49
+ console.error("\\n✗ gagal compile, benerin dulu terus save lagi.\\n");
50
+ return;
28
51
  }
52
+ version++;
53
+ console.log(\`✓ Compiled in \${fmtMs(Date.now() - start)}\`);
29
54
  }
30
55
 
31
- build();
32
- console.log("[medan] mantau main.medan, ctrl+c buat berhenti...");
56
+ const RELOAD_SCRIPT = \`
57
+ <script>
58
+ (function () {
59
+ var current = null;
60
+ setInterval(function () {
61
+ fetch("/__medan-version")
62
+ .then(function (r) { return r.text(); })
63
+ .then(function (v) {
64
+ if (current === null) current = v;
65
+ else if (v !== current) location.reload();
66
+ })
67
+ .catch(function () {});
68
+ }, 400);
69
+ })();
70
+ </script>
71
+ \`;
72
+
73
+ const server = createServer((req, res) => {
74
+ const start = Date.now();
75
+ res.on("finish", () => {
76
+ console.log(\` \${req.method} \${req.url} \${res.statusCode} in \${fmtMs(Date.now() - start)}\`);
77
+ });
78
+
79
+ if (req.url === "/__medan-version") {
80
+ res.writeHead(200, { "content-type": "text/plain" });
81
+ res.end(String(version));
82
+ return;
83
+ }
84
+
85
+ if (!existsSync("landing.html")) {
86
+ res.writeHead(200, { "content-type": "text/html" });
87
+ res.end("<p>lagi nge-generate landing.html, tunggu bentar terus refresh...</p>");
88
+ return;
89
+ }
90
+
91
+ const html = readFileSync("landing.html", "utf-8") + RELOAD_SCRIPT;
92
+ res.writeHead(200, { "content-type": "text/html; charset=utf-8" });
93
+ res.end(html);
94
+ });
95
+
96
+ const bootStart = Date.now();
97
+ build(true);
98
+
99
+ server.listen(PORT, () => {
100
+ const ip = localIp();
101
+ console.log("\\n▲ Medan-lang dev");
102
+ console.log(\` - Local: http://localhost:\${PORT}\`);
103
+ if (ip) console.log(\` - Network: http://\${ip}:\${PORT}\`);
104
+ console.log(\`\\n✓ Ready in \${fmtMs(Date.now() - bootStart)}\\n\`);
105
+ });
33
106
 
34
107
  let timer;
35
108
  watch("main.medan", () => {
36
109
  clearTimeout(timer);
37
- timer = setTimeout(build, 100);
110
+ timer = setTimeout(() => build(false), 100);
38
111
  });
39
112
  `;
40
113
  export function mainMedan(name) {
@@ -165,7 +238,7 @@ siap
165
238
  modal halaman = "<title>${name}</title>" + fontLink + css() + "<div class='page'>" + hero() + stepsSection() + cheatsheetSection() + footerSection() + "</div>"
166
239
 
167
240
  tulis("landing.html", halaman)
168
- bilang "landing.html kelar digenerate. buka file itu di browser lu."
241
+ bilang "landing.html kelar digenerate."
169
242
  `;
170
243
  }
171
244
  export function readme(name) {
@@ -180,11 +253,11 @@ npm install
180
253
  npm run dev
181
254
  \`\`\`
182
255
 
183
- \`npm run dev\` generate \`landing.html\` terus mantau \`main.medan\` — tiap kamu
184
- save, otomatis di-generate ulang. Buka \`landing.html\` di browser, refresh
185
- manual tiap abis edit & save.
256
+ Buka http://localhost:${DEV_PORT} di browser. \`npm run dev\` mantau
257
+ \`main.medan\` tiap kamu save, halamannya auto-reload sendiri, gak perlu
258
+ refresh manual.
186
259
 
187
- Mau generate sekali doang tanpa watch mode, pakai \`npm start\`.
260
+ Mau generate \`landing.html\` sekali doang tanpa server, pakai \`npm start\`.
188
261
 
189
262
  ## Belajar lebih lanjut
190
263
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-medan-app",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Scaffold project Medan-lang baru, kayak 'npx create-next-app'.",
5
5
  "author": "Wilbert Law",
6
6
  "license": "MIT",