Skip to content

Commit 3465719

Browse files
feat(cli): add --template svelte for Svelte project scaffolding
1 parent 2cb1063 commit 3465719

1 file changed

Lines changed: 238 additions & 80 deletions

File tree

cli/cmd_init.c

Lines changed: 238 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -36,85 +36,243 @@ int cmd_init(int argc, char **argv) {
3636
mkdirp(name);
3737
char path[512];
3838
snprintf(path, sizeof(path), "%s/src", name); mkdirp(path);
39-
snprintf(path, sizeof(path), "%s/assets", name); mkdirp(path);
40-
41-
/* lightshell.json */
42-
snprintf(path, sizeof(path), "%s/lightshell.json", name);
43-
char config[1024];
44-
snprintf(config, sizeof(config),
45-
"{\n"
46-
" \"name\": \"%s\",\n"
47-
" \"version\": \"0.1.0\",\n"
48-
" \"entry\": \"src/index.html\",\n"
49-
" \"window\": {\n"
50-
" \"title\": \"%s\",\n"
51-
" \"width\": 1024,\n"
52-
" \"height\": 768,\n"
53-
" \"minWidth\": 400,\n"
54-
" \"minHeight\": 300,\n"
55-
" \"resizable\": true\n"
56-
" },\n"
57-
" \"permissions\": [\"fs\", \"dialog\", \"clipboard\", \"shell\", \"menu\"]\n"
58-
"}\n", name, name);
59-
write_file(path, config);
60-
61-
/* src/index.html */
62-
snprintf(path, sizeof(path), "%s/src/index.html", name);
63-
char html[2048];
64-
snprintf(html, sizeof(html),
65-
"<!DOCTYPE html>\n"
66-
"<html>\n"
67-
"<head>\n"
68-
" <meta charset=\"utf-8\">\n"
69-
" <title>%s</title>\n"
70-
" <link rel=\"stylesheet\" href=\"style.css\">\n"
71-
"</head>\n"
72-
"<body>\n"
73-
" <div id=\"app\">\n"
74-
" <h1>Welcome to %s</h1>\n"
75-
" <p>Built with LightShell</p>\n"
76-
" <button id=\"btn\">Click me</button>\n"
77-
" <p id=\"output\"></p>\n"
78-
" </div>\n"
79-
" <script src=\"app.js\"></script>\n"
80-
"</body>\n"
81-
"</html>\n", name, name);
82-
write_file(path, html);
83-
84-
/* src/app.js */
85-
snprintf(path, sizeof(path), "%s/src/app.js", name);
86-
write_file(path,
87-
"// Your LightShell app\n"
88-
"var count = 0;\n"
89-
"var btn = document.querySelector('#btn');\n"
90-
"var output = document.querySelector('#output');\n"
91-
"\n"
92-
"btn.addEventListener('click', function() {\n"
93-
" count++;\n"
94-
" output.textContent = 'Clicked ' + count + ' times';\n"
95-
"});\n"
96-
"\n"
97-
"// Access native APIs\n"
98-
"var platform = lightshell.system.platform;\n"
99-
"console.log('Running on: ' + platform);\n");
100-
101-
/* src/style.css */
102-
snprintf(path, sizeof(path), "%s/src/style.css", name);
103-
write_file(path,
104-
"* { margin: 0; padding: 0; box-sizing: border-box; }\n"
105-
"body { font-family: -apple-system, sans-serif; padding: 24px; }\n"
106-
"h1 { font-size: 28px; margin-bottom: 8px; }\n"
107-
"p { color: #666; margin-bottom: 16px; }\n"
108-
"button {\n"
109-
" background: #3366FF; color: white; border: none;\n"
110-
" padding: 8px 16px; border-radius: 6px; cursor: pointer;\n"
111-
" font-size: 14px;\n"
112-
"}\n"
113-
"button:hover { background: #5588FF; }\n"
114-
"#output { margin-top: 16px; font-weight: bold; color: #333; }\n");
115-
116-
printf("\nProject created! Next steps:\n");
117-
printf(" cd %s\n", name);
118-
printf(" lightshell dev\n");
39+
40+
if (strcmp(tmpl, "svelte") == 0) {
41+
/* lightshell.json */
42+
snprintf(path, sizeof(path), "%s/lightshell.json", name);
43+
char config[1024];
44+
snprintf(config, sizeof(config),
45+
"{\n"
46+
" \"name\": \"%s\",\n"
47+
" \"version\": \"0.1.0\",\n"
48+
" \"entry\": \"dist/index.html\",\n"
49+
" \"window\": {\n"
50+
" \"title\": \"%s\",\n"
51+
" \"width\": 1024,\n"
52+
" \"height\": 768,\n"
53+
" \"minWidth\": 400,\n"
54+
" \"minHeight\": 300,\n"
55+
" \"resizable\": true\n"
56+
" },\n"
57+
" \"permissions\": [\"fs\", \"dialog\", \"clipboard\", \"shell\", \"menu\"],\n"
58+
" \"devCommand\": \"npm run dev\",\n"
59+
" \"buildCommand\": \"npm run build\"\n"
60+
"}\n", name, name);
61+
write_file(path, config);
62+
63+
/* package.json */
64+
snprintf(path, sizeof(path), "%s/package.json", name);
65+
char pkg[1024];
66+
snprintf(pkg, sizeof(pkg),
67+
"{\n"
68+
" \"name\": \"%s\",\n"
69+
" \"private\": true,\n"
70+
" \"version\": \"0.0.0\",\n"
71+
" \"type\": \"module\",\n"
72+
" \"scripts\": {\n"
73+
" \"dev\": \"vite build --watch\",\n"
74+
" \"build\": \"vite build\"\n"
75+
" },\n"
76+
" \"devDependencies\": {\n"
77+
" \"@sveltejs/vite-plugin-svelte\": \"^5.0.0\",\n"
78+
" \"svelte\": \"^5.0.0\",\n"
79+
" \"vite\": \"^6.0.0\"\n"
80+
" }\n"
81+
"}\n", name);
82+
write_file(path, pkg);
83+
84+
/* vite.config.js */
85+
snprintf(path, sizeof(path), "%s/vite.config.js", name);
86+
write_file(path,
87+
"import { defineConfig } from 'vite'\n"
88+
"import { svelte } from '@sveltejs/vite-plugin-svelte'\n"
89+
"\n"
90+
"export default defineConfig({\n"
91+
" plugins: [svelte()],\n"
92+
" build: {\n"
93+
" outDir: 'dist',\n"
94+
" emptyOutDir: true,\n"
95+
" }\n"
96+
"})\n");
97+
98+
/* src/App.svelte */
99+
snprintf(path, sizeof(path), "%s/src/App.svelte", name);
100+
write_file(path,
101+
"<script>\n"
102+
" let count = $state(0)\n"
103+
" let platform = 'detecting...'\n"
104+
"\n"
105+
" $effect(() => {\n"
106+
" if (typeof lightshell !== 'undefined') {\n"
107+
" platform = lightshell.system.platform || 'unknown'\n"
108+
" }\n"
109+
" })\n"
110+
"\n"
111+
" function increment() {\n"
112+
" count++\n"
113+
" }\n"
114+
"</script>\n"
115+
"\n"
116+
"<main>\n"
117+
" <h1>Welcome to {count > 0 ? 'LightShell!' : 'your app'}</h1>\n"
118+
" <p>Running on: {platform}</p>\n"
119+
" <button onclick={increment}>\n"
120+
" Clicked {count} {count === 1 ? 'time' : 'times'}\n"
121+
" </button>\n"
122+
"</main>\n"
123+
"\n"
124+
"<style>\n"
125+
" main {\n"
126+
" font-family: -apple-system, sans-serif;\n"
127+
" padding: 24px;\n"
128+
" max-width: 600px;\n"
129+
" }\n"
130+
" h1 {\n"
131+
" font-size: 28px;\n"
132+
" color: #1a1a1a;\n"
133+
" margin-bottom: 8px;\n"
134+
" }\n"
135+
" p {\n"
136+
" color: #666;\n"
137+
" margin-bottom: 16px;\n"
138+
" }\n"
139+
" button {\n"
140+
" background: #6366f1;\n"
141+
" color: white;\n"
142+
" border: none;\n"
143+
" padding: 10px 20px;\n"
144+
" border-radius: 8px;\n"
145+
" font-size: 14px;\n"
146+
" cursor: pointer;\n"
147+
" }\n"
148+
" button:hover {\n"
149+
" background: #5558e6;\n"
150+
" }\n"
151+
"</style>\n");
152+
153+
/* src/main.js */
154+
snprintf(path, sizeof(path), "%s/src/main.js", name);
155+
write_file(path,
156+
"import App from './App.svelte'\n"
157+
"import { mount } from 'svelte'\n"
158+
"\n"
159+
"const app = mount(App, {\n"
160+
" target: document.getElementById('app'),\n"
161+
"})\n"
162+
"\n"
163+
"export default app\n");
164+
165+
/* index.html */
166+
snprintf(path, sizeof(path), "%s/index.html", name);
167+
write_file(path,
168+
"<!DOCTYPE html>\n"
169+
"<html lang=\"en\">\n"
170+
"<head>\n"
171+
" <meta charset=\"UTF-8\">\n"
172+
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n"
173+
" <title>LightShell App</title>\n"
174+
"</head>\n"
175+
"<body>\n"
176+
" <div id=\"app\"></div>\n"
177+
" <script type=\"module\" src=\"/src/main.js\"></script>\n"
178+
"</body>\n"
179+
"</html>\n");
180+
181+
/* .gitignore */
182+
snprintf(path, sizeof(path), "%s/.gitignore", name);
183+
write_file(path,
184+
"node_modules/\n"
185+
"dist/\n"
186+
"build/\n"
187+
".DS_Store\n");
188+
189+
printf("\nProject created! Next steps:\n");
190+
printf(" cd %s\n", name);
191+
printf(" npm install\n");
192+
printf(" lightshell dev\n");
193+
} else {
194+
/* Default template */
195+
snprintf(path, sizeof(path), "%s/assets", name); mkdirp(path);
196+
197+
/* lightshell.json */
198+
snprintf(path, sizeof(path), "%s/lightshell.json", name);
199+
char config[1024];
200+
snprintf(config, sizeof(config),
201+
"{\n"
202+
" \"name\": \"%s\",\n"
203+
" \"version\": \"0.1.0\",\n"
204+
" \"entry\": \"src/index.html\",\n"
205+
" \"window\": {\n"
206+
" \"title\": \"%s\",\n"
207+
" \"width\": 1024,\n"
208+
" \"height\": 768,\n"
209+
" \"minWidth\": 400,\n"
210+
" \"minHeight\": 300,\n"
211+
" \"resizable\": true\n"
212+
" },\n"
213+
" \"permissions\": [\"fs\", \"dialog\", \"clipboard\", \"shell\", \"menu\"]\n"
214+
"}\n", name, name);
215+
write_file(path, config);
216+
217+
/* src/index.html */
218+
snprintf(path, sizeof(path), "%s/src/index.html", name);
219+
char html[2048];
220+
snprintf(html, sizeof(html),
221+
"<!DOCTYPE html>\n"
222+
"<html>\n"
223+
"<head>\n"
224+
" <meta charset=\"utf-8\">\n"
225+
" <title>%s</title>\n"
226+
" <link rel=\"stylesheet\" href=\"style.css\">\n"
227+
"</head>\n"
228+
"<body>\n"
229+
" <div id=\"app\">\n"
230+
" <h1>Welcome to %s</h1>\n"
231+
" <p>Built with LightShell</p>\n"
232+
" <button id=\"btn\">Click me</button>\n"
233+
" <p id=\"output\"></p>\n"
234+
" </div>\n"
235+
" <script src=\"app.js\"></script>\n"
236+
"</body>\n"
237+
"</html>\n", name, name);
238+
write_file(path, html);
239+
240+
/* src/app.js */
241+
snprintf(path, sizeof(path), "%s/src/app.js", name);
242+
write_file(path,
243+
"// Your LightShell app\n"
244+
"var count = 0;\n"
245+
"var btn = document.querySelector('#btn');\n"
246+
"var output = document.querySelector('#output');\n"
247+
"\n"
248+
"btn.addEventListener('click', function() {\n"
249+
" count++;\n"
250+
" output.textContent = 'Clicked ' + count + ' times';\n"
251+
"});\n"
252+
"\n"
253+
"// Access native APIs\n"
254+
"var platform = lightshell.system.platform;\n"
255+
"console.log('Running on: ' + platform);\n");
256+
257+
/* src/style.css */
258+
snprintf(path, sizeof(path), "%s/src/style.css", name);
259+
write_file(path,
260+
"* { margin: 0; padding: 0; box-sizing: border-box; }\n"
261+
"body { font-family: -apple-system, sans-serif; padding: 24px; }\n"
262+
"h1 { font-size: 28px; margin-bottom: 8px; }\n"
263+
"p { color: #666; margin-bottom: 16px; }\n"
264+
"button {\n"
265+
" background: #3366FF; color: white; border: none;\n"
266+
" padding: 8px 16px; border-radius: 6px; cursor: pointer;\n"
267+
" font-size: 14px;\n"
268+
"}\n"
269+
"button:hover { background: #5588FF; }\n"
270+
"#output { margin-top: 16px; font-weight: bold; color: #333; }\n");
271+
272+
printf("\nProject created! Next steps:\n");
273+
printf(" cd %s\n", name);
274+
printf(" lightshell dev\n");
275+
}
276+
119277
return 0;
120278
}

0 commit comments

Comments
 (0)