create-medan-app 0.1.2 → 0.1.3

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,23 +13,69 @@ 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
+
26
+ const PORT = ${DEV_PORT};
27
+ let version = 0;
22
28
 
23
29
  function build() {
24
30
  console.log("\\n[medan] generate landing.html ...");
25
31
  const result = spawnSync("medan", ["main.medan"], { stdio: "inherit" });
26
32
  if (result.status !== 0) {
27
33
  console.error("[medan] ada error di atas, benerin dulu terus save lagi.");
34
+ return;
28
35
  }
36
+ version++;
29
37
  }
30
38
 
39
+ const RELOAD_SCRIPT = \`
40
+ <script>
41
+ (function () {
42
+ var current = null;
43
+ setInterval(function () {
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);
52
+ })();
53
+ </script>
54
+ \`;
55
+
56
+ const server = createServer((req, res) => {
57
+ if (req.url === "/__medan-version") {
58
+ res.writeHead(200, { "content-type": "text/plain" });
59
+ res.end(String(version));
60
+ return;
61
+ }
62
+
63
+ if (!existsSync("landing.html")) {
64
+ res.writeHead(200, { "content-type": "text/html" });
65
+ res.end("<p>lagi nge-generate landing.html, tunggu bentar terus refresh...</p>");
66
+ return;
67
+ }
68
+
69
+ const html = readFileSync("landing.html", "utf-8") + RELOAD_SCRIPT;
70
+ res.writeHead(200, { "content-type": "text/html; charset=utf-8" });
71
+ res.end(html);
72
+ });
73
+
31
74
  build();
32
- console.log("[medan] mantau main.medan, ctrl+c buat berhenti...");
75
+ server.listen(PORT, () => {
76
+ console.log(\`\\n[medan] jalan di http://localhost:\${PORT}\`);
77
+ console.log("[medan] mantau main.medan, ctrl+c buat berhenti...");
78
+ });
33
79
 
34
80
  let timer;
35
81
  watch("main.medan", () => {
@@ -165,7 +211,7 @@ siap
165
211
  modal halaman = "<title>${name}</title>" + fontLink + css() + "<div class='page'>" + hero() + stepsSection() + cheatsheetSection() + footerSection() + "</div>"
166
212
 
167
213
  tulis("landing.html", halaman)
168
- bilang "landing.html kelar digenerate. buka file itu di browser lu."
214
+ bilang "landing.html kelar digenerate."
169
215
  `;
170
216
  }
171
217
  export function readme(name) {
@@ -180,11 +226,11 @@ npm install
180
226
  npm run dev
181
227
  \`\`\`
182
228
 
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.
229
+ Buka http://localhost:${DEV_PORT} di browser. \`npm run dev\` mantau
230
+ \`main.medan\` tiap kamu save, halamannya auto-reload sendiri, gak perlu
231
+ refresh manual.
186
232
 
187
- Mau generate sekali doang tanpa watch mode, pakai \`npm start\`.
233
+ Mau generate \`landing.html\` sekali doang tanpa server, pakai \`npm start\`.
188
234
 
189
235
  ## Belajar lebih lanjut
190
236
 
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.3",
4
4
  "description": "Scaffold project Medan-lang baru, kayak 'npx create-next-app'.",
5
5
  "author": "Wilbert Law",
6
6
  "license": "MIT",