create-medan-app 0.1.1 → 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 +4 -3
- package/dist/templates.js +80 -7
- package/package.json +1 -1
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 { 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
|
}
|
|
@@ -23,13 +23,14 @@ function main() {
|
|
|
23
23
|
mkdirSync(targetDir, { recursive: true });
|
|
24
24
|
writeFileSync(resolve(targetDir, "package.json"), packageJson(rawName));
|
|
25
25
|
writeFileSync(resolve(targetDir, "main.medan"), mainMedan(rawName));
|
|
26
|
+
writeFileSync(resolve(targetDir, "dev.mjs"), DEV_MJS);
|
|
26
27
|
writeFileSync(resolve(targetDir, "README.md"), readme(rawName));
|
|
27
28
|
writeFileSync(resolve(targetDir, ".gitignore"), GITIGNORE);
|
|
28
29
|
console.log(`mantap kali! project '${rawName}' udah jadi.\n`);
|
|
29
30
|
console.log("langkah selanjutnya:\n");
|
|
30
31
|
console.log(` cd ${rawName}`);
|
|
31
32
|
console.log(" npm install");
|
|
32
|
-
console.log(
|
|
33
|
-
console.log(" #
|
|
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");
|
|
34
35
|
}
|
|
35
36
|
main();
|
package/dist/templates.js
CHANGED
|
@@ -4,14 +4,85 @@ export function packageJson(name) {
|
|
|
4
4
|
"version": "0.1.0",
|
|
5
5
|
"private": true,
|
|
6
6
|
"scripts": {
|
|
7
|
-
"start": "medan main.medan"
|
|
7
|
+
"start": "medan main.medan",
|
|
8
|
+
"dev": "node dev.mjs"
|
|
8
9
|
},
|
|
9
10
|
"dependencies": {
|
|
10
|
-
"medan-language": "^0.1.
|
|
11
|
+
"medan-language": "^0.1.3"
|
|
11
12
|
}
|
|
12
13
|
}
|
|
13
14
|
`;
|
|
14
15
|
}
|
|
16
|
+
export const DEV_PORT = 7777;
|
|
17
|
+
export const DEV_MJS = `// dev.mjs
|
|
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.
|
|
21
|
+
|
|
22
|
+
import { spawnSync } from "node:child_process";
|
|
23
|
+
import { watch, readFileSync, existsSync } from "node:fs";
|
|
24
|
+
import { createServer } from "node:http";
|
|
25
|
+
|
|
26
|
+
const PORT = ${DEV_PORT};
|
|
27
|
+
let version = 0;
|
|
28
|
+
|
|
29
|
+
function build() {
|
|
30
|
+
console.log("\\n[medan] generate landing.html ...");
|
|
31
|
+
const result = spawnSync("medan", ["main.medan"], { stdio: "inherit" });
|
|
32
|
+
if (result.status !== 0) {
|
|
33
|
+
console.error("[medan] ada error di atas, benerin dulu terus save lagi.");
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
version++;
|
|
37
|
+
}
|
|
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
|
+
|
|
74
|
+
build();
|
|
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
|
+
});
|
|
79
|
+
|
|
80
|
+
let timer;
|
|
81
|
+
watch("main.medan", () => {
|
|
82
|
+
clearTimeout(timer);
|
|
83
|
+
timer = setTimeout(build, 100);
|
|
84
|
+
});
|
|
85
|
+
`;
|
|
15
86
|
export function mainMedan(name) {
|
|
16
87
|
return `// main.medan
|
|
17
88
|
// Ini titik masuk project "${name}" kamu.
|
|
@@ -140,7 +211,7 @@ siap
|
|
|
140
211
|
modal halaman = "<title>${name}</title>" + fontLink + css() + "<div class='page'>" + hero() + stepsSection() + cheatsheetSection() + footerSection() + "</div>"
|
|
141
212
|
|
|
142
213
|
tulis("landing.html", halaman)
|
|
143
|
-
bilang "landing.html kelar digenerate.
|
|
214
|
+
bilang "landing.html kelar digenerate."
|
|
144
215
|
`;
|
|
145
216
|
}
|
|
146
217
|
export function readme(name) {
|
|
@@ -152,12 +223,14 @@ Project Medan-lang, di-scaffold pakai \`create-medan-app\`.
|
|
|
152
223
|
|
|
153
224
|
\`\`\`bash
|
|
154
225
|
npm install
|
|
155
|
-
npm
|
|
226
|
+
npm run dev
|
|
156
227
|
\`\`\`
|
|
157
228
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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.
|
|
232
|
+
|
|
233
|
+
Mau generate \`landing.html\` sekali doang tanpa server, pakai \`npm start\`.
|
|
161
234
|
|
|
162
235
|
## Belajar lebih lanjut
|
|
163
236
|
|