@yannosay/sinth 1.8.0 → 1.8.2
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/LICENSE +660 -660
- package/README.md +548 -548
- package/package.json +45 -45
- package/src/cli.ts +338 -338
- package/src/core/builtins.ts +116 -116
- package/src/core/compiler.ts +1706 -1706
- package/src/core/expr.ts +88 -88
- package/src/core/head-builder.ts +86 -86
- package/src/core/lexer.ts +275 -275
- package/src/core/parser.ts +1523 -1523
- package/src/core/runtime/custom-element.ts +134 -134
- package/src/core/runtime/functions.ts +46 -46
- package/src/core/runtime/helpers.ts +332 -332
- package/src/core/runtime/render.ts +136 -136
- package/src/core/style-processor.ts +299 -299
- package/src/core/types.ts +192 -192
- package/src/resolver.ts +149 -149
- package/src/server.ts +128 -128
- package/src/utils.ts +76 -76
- package/tsconfig.json +26 -26
package/src/cli.ts
CHANGED
|
@@ -1,339 +1,339 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import * as fs from "fs";
|
|
3
|
-
import * as path from "path";
|
|
4
|
-
import { SinthWarning } from "./core/types.ts";
|
|
5
|
-
import { compileFile, CompileOptions, findSinthPages, copyDir } from "./core/compiler.ts";
|
|
6
|
-
import { startDevServer } from "./server.ts";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
function loadConfig(root: string): Record<string, unknown> {
|
|
11
|
-
const cfgPath = path.join(root, "sinth.config.json");
|
|
12
|
-
if (fs.existsSync(cfgPath)) {
|
|
13
|
-
try { return JSON.parse(fs.readFileSync(cfgPath, "utf-8")) as Record<string, unknown>; }
|
|
14
|
-
catch { SinthWarning.emit("Could not parse sinth.config.json"); }
|
|
15
|
-
}
|
|
16
|
-
return {};
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async function main(): Promise<void> {
|
|
20
|
-
const [,, command, ...args] = process.argv;
|
|
21
|
-
const cwd = process.cwd();
|
|
22
|
-
const cfg = loadConfig(cwd);
|
|
23
|
-
|
|
24
|
-
const outDirIdx = args.indexOf("--out");
|
|
25
|
-
const outDir = outDirIdx !== -1 ? args[outDirIdx + 1] : (cfg.outDir as string | undefined) ?? path.join(cwd, "dist");
|
|
26
|
-
const portIdx = args.indexOf("--port");
|
|
27
|
-
const port = portIdx !== -1 ? parseInt(args[portIdx + 1], 10) : (cfg.port as number | undefined) ?? 3000;
|
|
28
|
-
const minify = args.includes("--prod") || Boolean(cfg.minify);
|
|
29
|
-
const sharedRuntime = args.includes("--shared-runtime") || Boolean(cfg.sharedRuntime);
|
|
30
|
-
const libraryPaths = (cfg.libraryPaths as string[] | undefined) ?? [path.join(cwd, "libraries")];
|
|
31
|
-
|
|
32
|
-
const flagValues = new Set<string>();
|
|
33
|
-
if (outDirIdx !== -1) flagValues.add(args[outDirIdx + 1]);
|
|
34
|
-
if (portIdx !== -1) flagValues.add(args[portIdx + 1]);
|
|
35
|
-
const cleanArgs = args.filter(a => !a.startsWith("--") && !flagValues.has(a));
|
|
36
|
-
|
|
37
|
-
const opts: CompileOptions = { projectRoot: cwd, outDir, libraryPaths, minify, checkOnly: false, sharedRuntime };
|
|
38
|
-
|
|
39
|
-
switch (command) {
|
|
40
|
-
case "build": {
|
|
41
|
-
const nonSinth = cleanArgs.filter(a => !a.endsWith(".sinth"));
|
|
42
|
-
if (nonSinth.length > 0) process.stdout.write(`\x1b[33mSkipping non-.sinth files:\x1b[0m ${nonSinth.join(", ")}\n`);
|
|
43
|
-
|
|
44
|
-
const fileArgs = cleanArgs.filter(a => a.endsWith(".sinth"));
|
|
45
|
-
const pages = fileArgs.length > 0
|
|
46
|
-
? fileArgs.map(f => path.resolve(cwd, f)).filter(f => fs.existsSync(f))
|
|
47
|
-
: findSinthPages(cwd, outDir);
|
|
48
|
-
|
|
49
|
-
if (pages.length === 0) { process.stdout.write("No .sinth files found.\n"); process.exit(0); }
|
|
50
|
-
|
|
51
|
-
let hadError = false, built = 0;
|
|
52
|
-
|
|
53
|
-
const sharedRuntimes: string[] = [];
|
|
54
|
-
for (const p of pages) {
|
|
55
|
-
try {
|
|
56
|
-
const result = compileFile(p, opts);
|
|
57
|
-
if (!result) continue;
|
|
58
|
-
const html = result.html;
|
|
59
|
-
if (result.shared) {
|
|
60
|
-
sharedRuntimes.push(result.shared);
|
|
61
|
-
}
|
|
62
|
-
const rel = path.relative(cwd, p).replace(/\.sinth$/, ".html");
|
|
63
|
-
const out = path.join(outDir, rel);
|
|
64
|
-
fs.mkdirSync(path.dirname(out), { recursive: true });
|
|
65
|
-
fs.writeFileSync(out, html);
|
|
66
|
-
process.stdout.write(` \x1b[32m✓\x1b[0m ${rel}\n`);
|
|
67
|
-
built++;
|
|
68
|
-
} catch (e: unknown) {
|
|
69
|
-
process.stderr.write(` \x1b[31m✗\x1b[0m ${path.relative(cwd, p)}\n${(e as Error).message}\n`);
|
|
70
|
-
hadError = true;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (sharedRuntimes.length > 0) {
|
|
75
|
-
const combined = sharedRuntimes.join("\n");
|
|
76
|
-
fs.writeFileSync(path.join(outDir, "sinth-runtime.js"), combined);
|
|
77
|
-
process.stdout.write(` \x1b[32m✓\x1b[0m sinth-runtime.js (shared)\n`);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// copies to output
|
|
81
|
-
const assetsIn = path.join(cwd, "assets"), assetsOut = path.join(outDir, "assets");
|
|
82
|
-
if (fs.existsSync(assetsIn)) {
|
|
83
|
-
copyDir(assetsIn, assetsOut);
|
|
84
|
-
process.stdout.write(` \x1b[32m✓\x1b[0m assets/ → ${path.relative(cwd, assetsOut)}/\n`);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const libIn = path.join(cwd, "libraries"), libOut = path.join(outDir, "libraries");
|
|
88
|
-
if (fs.existsSync(libIn)) {
|
|
89
|
-
copyDir(libIn, libOut);
|
|
90
|
-
const libFiles = fs.readdirSync(libOut, { recursive: true }) as string[];
|
|
91
|
-
for (const f of libFiles) {
|
|
92
|
-
if (f.endsWith(".sinth") || f.endsWith(".html")) {
|
|
93
|
-
try { fs.unlinkSync(path.join(libOut, f)); } catch {}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
process.stdout.write(` \x1b[32m✓\x1b[0m libraries/ → ${path.relative(cwd, libOut)}/\n`);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
process.stdout.write(`\n\x1b[1mBuilt ${built} page(s)\x1b[0m${hadError ? " with errors" : ""}\n`);
|
|
100
|
-
process.exit(hadError ? 1 : 0);
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
case "dev": {
|
|
105
|
-
const fileArgs = cleanArgs.filter(a => a.endsWith(".sinth"));
|
|
106
|
-
const files = fileArgs.length > 0
|
|
107
|
-
? fileArgs.map(f => path.resolve(cwd, f)).filter(f => fs.existsSync(f))
|
|
108
|
-
: undefined;
|
|
109
|
-
await startDevServer({ ...opts, port, files });
|
|
110
|
-
break;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
case "check": {
|
|
114
|
-
opts.checkOnly = true;
|
|
115
|
-
const pages = findSinthPages(cwd, outDir);
|
|
116
|
-
let hadError = false;
|
|
117
|
-
for (const p of pages) {
|
|
118
|
-
try {
|
|
119
|
-
compileFile(p, opts);
|
|
120
|
-
process.stdout.write(` \x1b[32m✓\x1b[0m ${path.relative(cwd, p)}\n`);
|
|
121
|
-
} catch (e: unknown) {
|
|
122
|
-
process.stderr.write(` \x1b[31m✗\x1b[0m ${path.relative(cwd, p)}\n${(e as Error).message}\n`);
|
|
123
|
-
hadError = true;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
process.exit(hadError ? 1 : 0);
|
|
127
|
-
break;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
case "version":
|
|
131
|
-
case "--version":
|
|
132
|
-
case "-v": {
|
|
133
|
-
const pkgPath = path.join(__dirname, "..", "package.json");
|
|
134
|
-
if (fs.existsSync(pkgPath)) {
|
|
135
|
-
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
|
|
136
|
-
process.stdout.write(`Sinth Compiler v${pkg.version}\n`);
|
|
137
|
-
} else {
|
|
138
|
-
process.stdout.write("Sinth Compiler v1.0.0\n");
|
|
139
|
-
}
|
|
140
|
-
break;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
case "init": {
|
|
144
|
-
const projectName = cleanArgs[0] || "my-sinth-project";
|
|
145
|
-
scaffoldProject(path.resolve(cwd, projectName));
|
|
146
|
-
break;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
default: {
|
|
150
|
-
let version = "1.0.0";
|
|
151
|
-
const paths = [
|
|
152
|
-
path.resolve(__dirname, "..", "package.json"),
|
|
153
|
-
path.resolve(__dirname, "package.json"),
|
|
154
|
-
path.resolve(process.cwd(), "package.json"),
|
|
155
|
-
path.resolve(process.cwd(), "..", "package.json"),
|
|
156
|
-
];
|
|
157
|
-
for (const p of paths) {
|
|
158
|
-
if (fs.existsSync(p)) {
|
|
159
|
-
try {
|
|
160
|
-
const pkg = JSON.parse(fs.readFileSync(p, "utf-8"));
|
|
161
|
-
version = pkg.version;
|
|
162
|
-
break;
|
|
163
|
-
} catch {}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
process.stdout.write(`
|
|
167
|
-
\x1b[1mSinth Compiler v${version}\x1b[0m
|
|
168
|
-
|
|
169
|
-
\x1b[1mCommands:\x1b[0m
|
|
170
|
-
sinth build [files] [--out ./dist] [--prod] [--shared-runtime] Compile .sinth pages
|
|
171
|
-
sinth dev [files] [--port 3000] Live-reload dev server
|
|
172
|
-
sinth check Lint without emitting
|
|
173
|
-
sinth init [name] Scaffold a new project
|
|
174
|
-
sinth version Print version
|
|
175
|
-
`);
|
|
176
|
-
break;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// project scaffholding
|
|
182
|
-
|
|
183
|
-
function scaffoldProject(root: string): void {
|
|
184
|
-
for (const d of ["pages", "components", "styles", "libraries", "assets"]) {
|
|
185
|
-
fs.mkdirSync(path.join(root, d), { recursive: true });
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
fs.writeFileSync(path.join(root, "pages", "index.sinth"), `-- My Sinth Site
|
|
189
|
-
page
|
|
190
|
-
|
|
191
|
-
import "../components/Navbar.sinth"
|
|
192
|
-
import css "../styles/reset.css"
|
|
193
|
-
|
|
194
|
-
title = "My Site"
|
|
195
|
-
fav = "assets/favicon.ico"
|
|
196
|
-
descr = "Built with Sinth v1.0.0."
|
|
197
|
-
|
|
198
|
-
var int score = 0
|
|
199
|
-
var str message = "Click to begin"
|
|
200
|
-
|
|
201
|
-
Navbar
|
|
202
|
-
|
|
203
|
-
Hero {
|
|
204
|
-
Heading(level: 1) { "Welcome to Sinth" }
|
|
205
|
-
Paragraph { "A declarative, component-based web UI language." }
|
|
206
|
-
Button(onClick: "handleClick") { "Get Started" }
|
|
207
|
-
Paragraph(id: "score-display") { message }
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
Main {
|
|
211
|
-
Section {
|
|
212
|
-
Heading(level: 2) { "Features" }
|
|
213
|
-
CardGrid {
|
|
214
|
-
-- Add Card components here
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
style {
|
|
220
|
-
section.hero {
|
|
221
|
-
padding: "4rem 2rem"
|
|
222
|
-
textAlign: "center"
|
|
223
|
-
backgroundColor: "#f0f4ff"
|
|
224
|
-
}
|
|
225
|
-
main {
|
|
226
|
-
maxWidth: "1100px"
|
|
227
|
-
margin: "0 auto"
|
|
228
|
-
padding: "2rem"
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
script {
|
|
233
|
-
function handleClick() {
|
|
234
|
-
score += 1
|
|
235
|
-
message = "Score: " + score
|
|
236
|
-
sinthRender()
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
`);
|
|
240
|
-
|
|
241
|
-
fs.writeFileSync(path.join(root, "components", "Navbar.sinth"), `-- Navbar component
|
|
242
|
-
|
|
243
|
-
component Navbar {
|
|
244
|
-
Header {
|
|
245
|
-
Nav {
|
|
246
|
-
Link(href: "/", class: "logo") { "MySite" }
|
|
247
|
-
Div(class: "nav-links") {
|
|
248
|
-
NavLink(href: "/") { "Home" }
|
|
249
|
-
NavLink(href: "/about") { "About" }
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
style {
|
|
255
|
-
header {
|
|
256
|
-
display: "flex"
|
|
257
|
-
alignItems: "center"
|
|
258
|
-
padding: "1rem 2rem"
|
|
259
|
-
backgroundColor: "#1a1a2e"
|
|
260
|
-
color: "white"
|
|
261
|
-
}
|
|
262
|
-
.logo {
|
|
263
|
-
fontSize: "1.5rem"
|
|
264
|
-
fontWeight: "700"
|
|
265
|
-
color: "white"
|
|
266
|
-
textDecoration: "none"
|
|
267
|
-
}
|
|
268
|
-
.nav-links {
|
|
269
|
-
marginLeft: "auto"
|
|
270
|
-
display: "flex"
|
|
271
|
-
gap: "1.5rem"
|
|
272
|
-
}
|
|
273
|
-
.nav-links a {
|
|
274
|
-
color: "rgba(255,255,255,0.8)"
|
|
275
|
-
textDecoration: "none"
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
`);
|
|
280
|
-
|
|
281
|
-
fs.writeFileSync(path.join(root, "components", "Card.sinth"), `-- Card component
|
|
282
|
-
|
|
283
|
-
component Card(title, color = "blue") {
|
|
284
|
-
Div(class: "card") {
|
|
285
|
-
Heading(level: 3) { "$title" }
|
|
286
|
-
Div(class: "card-body") { "$slot" }
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
style {
|
|
290
|
-
.card {
|
|
291
|
-
backgroundColor: "#f7f7f7"
|
|
292
|
-
borderRadius: "1rem"
|
|
293
|
-
padding: "1.5rem"
|
|
294
|
-
marginBottom: "1rem"
|
|
295
|
-
}
|
|
296
|
-
.card:hover {
|
|
297
|
-
boxShadow: "0 4px 16px rgba(0,0,0,0.1)"
|
|
298
|
-
}
|
|
299
|
-
.card-body {
|
|
300
|
-
marginTop: "0.75rem"
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
`);
|
|
305
|
-
|
|
306
|
-
fs.writeFileSync(path.join(root, "styles", "reset.css"),
|
|
307
|
-
`*, *::before, *::after { box-sizing: border-box; }\nbody { margin: 0; font-family: system-ui, sans-serif; line-height: 1.6; }\nimg { max-width: 100%; display: block; }\n`
|
|
308
|
-
);
|
|
309
|
-
|
|
310
|
-
fs.writeFileSync(path.join(root, "sinth.config.json"),
|
|
311
|
-
JSON.stringify({ outDir: "./dist", libraryPaths: ["./libraries"], minify: false }, null, 2)
|
|
312
|
-
);
|
|
313
|
-
|
|
314
|
-
fs.writeFileSync(path.join(root, ".gitignore"), "dist/\nnode_modules/\n");
|
|
315
|
-
|
|
316
|
-
fs.writeFileSync(path.join(root, "package.json"), JSON.stringify({
|
|
317
|
-
name: path.basename(root).toLowerCase().replace(/\s+/g, "-"),
|
|
318
|
-
version: "1.0.0",
|
|
319
|
-
scripts: { build: "sinth build", dev: "sinth dev" },
|
|
320
|
-
dependencies: { "ts-node": "^10.0.0", "sass": "^1.70.0" },
|
|
321
|
-
}, null, 2));
|
|
322
|
-
|
|
323
|
-
process.stdout.write(`
|
|
324
|
-
\x1b[32m✓ Sinth project scaffolded at ${path.basename(root)}/\x1b[0m
|
|
325
|
-
|
|
326
|
-
\x1b[1mNext steps:\x1b[0m
|
|
327
|
-
cd ${path.basename(root)}
|
|
328
|
-
sinth dev
|
|
329
|
-
sinth build
|
|
330
|
-
`);
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
main().catch(e => {
|
|
337
|
-
process.stderr.write(`\x1b[31m${(e as Error).message}\x1b[0m\n`);
|
|
338
|
-
process.exit(1);
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import { SinthWarning } from "./core/types.ts";
|
|
5
|
+
import { compileFile, CompileOptions, findSinthPages, copyDir } from "./core/compiler.ts";
|
|
6
|
+
import { startDevServer } from "./server.ts";
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
function loadConfig(root: string): Record<string, unknown> {
|
|
11
|
+
const cfgPath = path.join(root, "sinth.config.json");
|
|
12
|
+
if (fs.existsSync(cfgPath)) {
|
|
13
|
+
try { return JSON.parse(fs.readFileSync(cfgPath, "utf-8")) as Record<string, unknown>; }
|
|
14
|
+
catch { SinthWarning.emit("Could not parse sinth.config.json"); }
|
|
15
|
+
}
|
|
16
|
+
return {};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function main(): Promise<void> {
|
|
20
|
+
const [,, command, ...args] = process.argv;
|
|
21
|
+
const cwd = process.cwd();
|
|
22
|
+
const cfg = loadConfig(cwd);
|
|
23
|
+
|
|
24
|
+
const outDirIdx = args.indexOf("--out");
|
|
25
|
+
const outDir = outDirIdx !== -1 ? args[outDirIdx + 1] : (cfg.outDir as string | undefined) ?? path.join(cwd, "dist");
|
|
26
|
+
const portIdx = args.indexOf("--port");
|
|
27
|
+
const port = portIdx !== -1 ? parseInt(args[portIdx + 1], 10) : (cfg.port as number | undefined) ?? 3000;
|
|
28
|
+
const minify = args.includes("--prod") || Boolean(cfg.minify);
|
|
29
|
+
const sharedRuntime = args.includes("--shared-runtime") || Boolean(cfg.sharedRuntime);
|
|
30
|
+
const libraryPaths = (cfg.libraryPaths as string[] | undefined) ?? [path.join(cwd, "libraries")];
|
|
31
|
+
|
|
32
|
+
const flagValues = new Set<string>();
|
|
33
|
+
if (outDirIdx !== -1) flagValues.add(args[outDirIdx + 1]);
|
|
34
|
+
if (portIdx !== -1) flagValues.add(args[portIdx + 1]);
|
|
35
|
+
const cleanArgs = args.filter(a => !a.startsWith("--") && !flagValues.has(a));
|
|
36
|
+
|
|
37
|
+
const opts: CompileOptions = { projectRoot: cwd, outDir, libraryPaths, minify, checkOnly: false, sharedRuntime };
|
|
38
|
+
|
|
39
|
+
switch (command) {
|
|
40
|
+
case "build": {
|
|
41
|
+
const nonSinth = cleanArgs.filter(a => !a.endsWith(".sinth"));
|
|
42
|
+
if (nonSinth.length > 0) process.stdout.write(`\x1b[33mSkipping non-.sinth files:\x1b[0m ${nonSinth.join(", ")}\n`);
|
|
43
|
+
|
|
44
|
+
const fileArgs = cleanArgs.filter(a => a.endsWith(".sinth"));
|
|
45
|
+
const pages = fileArgs.length > 0
|
|
46
|
+
? fileArgs.map(f => path.resolve(cwd, f)).filter(f => fs.existsSync(f))
|
|
47
|
+
: findSinthPages(cwd, outDir);
|
|
48
|
+
|
|
49
|
+
if (pages.length === 0) { process.stdout.write("No .sinth files found.\n"); process.exit(0); }
|
|
50
|
+
|
|
51
|
+
let hadError = false, built = 0;
|
|
52
|
+
|
|
53
|
+
const sharedRuntimes: string[] = [];
|
|
54
|
+
for (const p of pages) {
|
|
55
|
+
try {
|
|
56
|
+
const result = compileFile(p, opts);
|
|
57
|
+
if (!result) continue;
|
|
58
|
+
const html = result.html;
|
|
59
|
+
if (result.shared) {
|
|
60
|
+
sharedRuntimes.push(result.shared);
|
|
61
|
+
}
|
|
62
|
+
const rel = path.relative(cwd, p).replace(/\.sinth$/, ".html");
|
|
63
|
+
const out = path.join(outDir, rel);
|
|
64
|
+
fs.mkdirSync(path.dirname(out), { recursive: true });
|
|
65
|
+
fs.writeFileSync(out, html);
|
|
66
|
+
process.stdout.write(` \x1b[32m✓\x1b[0m ${rel}\n`);
|
|
67
|
+
built++;
|
|
68
|
+
} catch (e: unknown) {
|
|
69
|
+
process.stderr.write(` \x1b[31m✗\x1b[0m ${path.relative(cwd, p)}\n${(e as Error).message}\n`);
|
|
70
|
+
hadError = true;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (sharedRuntimes.length > 0) {
|
|
75
|
+
const combined = sharedRuntimes.join("\n");
|
|
76
|
+
fs.writeFileSync(path.join(outDir, "sinth-runtime.js"), combined);
|
|
77
|
+
process.stdout.write(` \x1b[32m✓\x1b[0m sinth-runtime.js (shared)\n`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// copies to output
|
|
81
|
+
const assetsIn = path.join(cwd, "assets"), assetsOut = path.join(outDir, "assets");
|
|
82
|
+
if (fs.existsSync(assetsIn)) {
|
|
83
|
+
copyDir(assetsIn, assetsOut);
|
|
84
|
+
process.stdout.write(` \x1b[32m✓\x1b[0m assets/ → ${path.relative(cwd, assetsOut)}/\n`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const libIn = path.join(cwd, "libraries"), libOut = path.join(outDir, "libraries");
|
|
88
|
+
if (fs.existsSync(libIn)) {
|
|
89
|
+
copyDir(libIn, libOut);
|
|
90
|
+
const libFiles = fs.readdirSync(libOut, { recursive: true }) as string[];
|
|
91
|
+
for (const f of libFiles) {
|
|
92
|
+
if (f.endsWith(".sinth") || f.endsWith(".html")) {
|
|
93
|
+
try { fs.unlinkSync(path.join(libOut, f)); } catch {}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
process.stdout.write(` \x1b[32m✓\x1b[0m libraries/ → ${path.relative(cwd, libOut)}/\n`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
process.stdout.write(`\n\x1b[1mBuilt ${built} page(s)\x1b[0m${hadError ? " with errors" : ""}\n`);
|
|
100
|
+
process.exit(hadError ? 1 : 0);
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
case "dev": {
|
|
105
|
+
const fileArgs = cleanArgs.filter(a => a.endsWith(".sinth"));
|
|
106
|
+
const files = fileArgs.length > 0
|
|
107
|
+
? fileArgs.map(f => path.resolve(cwd, f)).filter(f => fs.existsSync(f))
|
|
108
|
+
: undefined;
|
|
109
|
+
await startDevServer({ ...opts, port, files });
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
case "check": {
|
|
114
|
+
opts.checkOnly = true;
|
|
115
|
+
const pages = findSinthPages(cwd, outDir);
|
|
116
|
+
let hadError = false;
|
|
117
|
+
for (const p of pages) {
|
|
118
|
+
try {
|
|
119
|
+
compileFile(p, opts);
|
|
120
|
+
process.stdout.write(` \x1b[32m✓\x1b[0m ${path.relative(cwd, p)}\n`);
|
|
121
|
+
} catch (e: unknown) {
|
|
122
|
+
process.stderr.write(` \x1b[31m✗\x1b[0m ${path.relative(cwd, p)}\n${(e as Error).message}\n`);
|
|
123
|
+
hadError = true;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
process.exit(hadError ? 1 : 0);
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
case "version":
|
|
131
|
+
case "--version":
|
|
132
|
+
case "-v": {
|
|
133
|
+
const pkgPath = path.join(__dirname, "..", "package.json");
|
|
134
|
+
if (fs.existsSync(pkgPath)) {
|
|
135
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
|
|
136
|
+
process.stdout.write(`Sinth Compiler v${pkg.version}\n`);
|
|
137
|
+
} else {
|
|
138
|
+
process.stdout.write("Sinth Compiler v1.0.0\n");
|
|
139
|
+
}
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
case "init": {
|
|
144
|
+
const projectName = cleanArgs[0] || "my-sinth-project";
|
|
145
|
+
scaffoldProject(path.resolve(cwd, projectName));
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
default: {
|
|
150
|
+
let version = "1.0.0";
|
|
151
|
+
const paths = [
|
|
152
|
+
path.resolve(__dirname, "..", "package.json"),
|
|
153
|
+
path.resolve(__dirname, "package.json"),
|
|
154
|
+
path.resolve(process.cwd(), "package.json"),
|
|
155
|
+
path.resolve(process.cwd(), "..", "package.json"),
|
|
156
|
+
];
|
|
157
|
+
for (const p of paths) {
|
|
158
|
+
if (fs.existsSync(p)) {
|
|
159
|
+
try {
|
|
160
|
+
const pkg = JSON.parse(fs.readFileSync(p, "utf-8"));
|
|
161
|
+
version = pkg.version;
|
|
162
|
+
break;
|
|
163
|
+
} catch {}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
process.stdout.write(`
|
|
167
|
+
\x1b[1mSinth Compiler v${version}\x1b[0m
|
|
168
|
+
|
|
169
|
+
\x1b[1mCommands:\x1b[0m
|
|
170
|
+
sinth build [files] [--out ./dist] [--prod] [--shared-runtime] Compile .sinth pages
|
|
171
|
+
sinth dev [files] [--port 3000] Live-reload dev server
|
|
172
|
+
sinth check Lint without emitting
|
|
173
|
+
sinth init [name] Scaffold a new project
|
|
174
|
+
sinth version Print version
|
|
175
|
+
`);
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// project scaffholding
|
|
182
|
+
|
|
183
|
+
function scaffoldProject(root: string): void {
|
|
184
|
+
for (const d of ["pages", "components", "styles", "libraries", "assets"]) {
|
|
185
|
+
fs.mkdirSync(path.join(root, d), { recursive: true });
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
fs.writeFileSync(path.join(root, "pages", "index.sinth"), `-- My Sinth Site
|
|
189
|
+
page
|
|
190
|
+
|
|
191
|
+
import "../components/Navbar.sinth"
|
|
192
|
+
import css "../styles/reset.css"
|
|
193
|
+
|
|
194
|
+
title = "My Site"
|
|
195
|
+
fav = "assets/favicon.ico"
|
|
196
|
+
descr = "Built with Sinth v1.0.0."
|
|
197
|
+
|
|
198
|
+
var int score = 0
|
|
199
|
+
var str message = "Click to begin"
|
|
200
|
+
|
|
201
|
+
Navbar
|
|
202
|
+
|
|
203
|
+
Hero {
|
|
204
|
+
Heading(level: 1) { "Welcome to Sinth" }
|
|
205
|
+
Paragraph { "A declarative, component-based web UI language." }
|
|
206
|
+
Button(onClick: "handleClick") { "Get Started" }
|
|
207
|
+
Paragraph(id: "score-display") { message }
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
Main {
|
|
211
|
+
Section {
|
|
212
|
+
Heading(level: 2) { "Features" }
|
|
213
|
+
CardGrid {
|
|
214
|
+
-- Add Card components here
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
style {
|
|
220
|
+
section.hero {
|
|
221
|
+
padding: "4rem 2rem"
|
|
222
|
+
textAlign: "center"
|
|
223
|
+
backgroundColor: "#f0f4ff"
|
|
224
|
+
}
|
|
225
|
+
main {
|
|
226
|
+
maxWidth: "1100px"
|
|
227
|
+
margin: "0 auto"
|
|
228
|
+
padding: "2rem"
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
script {
|
|
233
|
+
function handleClick() {
|
|
234
|
+
score += 1
|
|
235
|
+
message = "Score: " + score
|
|
236
|
+
sinthRender()
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
`);
|
|
240
|
+
|
|
241
|
+
fs.writeFileSync(path.join(root, "components", "Navbar.sinth"), `-- Navbar component
|
|
242
|
+
|
|
243
|
+
component Navbar {
|
|
244
|
+
Header {
|
|
245
|
+
Nav {
|
|
246
|
+
Link(href: "/", class: "logo") { "MySite" }
|
|
247
|
+
Div(class: "nav-links") {
|
|
248
|
+
NavLink(href: "/") { "Home" }
|
|
249
|
+
NavLink(href: "/about") { "About" }
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
style {
|
|
255
|
+
header {
|
|
256
|
+
display: "flex"
|
|
257
|
+
alignItems: "center"
|
|
258
|
+
padding: "1rem 2rem"
|
|
259
|
+
backgroundColor: "#1a1a2e"
|
|
260
|
+
color: "white"
|
|
261
|
+
}
|
|
262
|
+
.logo {
|
|
263
|
+
fontSize: "1.5rem"
|
|
264
|
+
fontWeight: "700"
|
|
265
|
+
color: "white"
|
|
266
|
+
textDecoration: "none"
|
|
267
|
+
}
|
|
268
|
+
.nav-links {
|
|
269
|
+
marginLeft: "auto"
|
|
270
|
+
display: "flex"
|
|
271
|
+
gap: "1.5rem"
|
|
272
|
+
}
|
|
273
|
+
.nav-links a {
|
|
274
|
+
color: "rgba(255,255,255,0.8)"
|
|
275
|
+
textDecoration: "none"
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
`);
|
|
280
|
+
|
|
281
|
+
fs.writeFileSync(path.join(root, "components", "Card.sinth"), `-- Card component
|
|
282
|
+
|
|
283
|
+
component Card(title, color = "blue") {
|
|
284
|
+
Div(class: "card") {
|
|
285
|
+
Heading(level: 3) { "$title" }
|
|
286
|
+
Div(class: "card-body") { "$slot" }
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
style {
|
|
290
|
+
.card {
|
|
291
|
+
backgroundColor: "#f7f7f7"
|
|
292
|
+
borderRadius: "1rem"
|
|
293
|
+
padding: "1.5rem"
|
|
294
|
+
marginBottom: "1rem"
|
|
295
|
+
}
|
|
296
|
+
.card:hover {
|
|
297
|
+
boxShadow: "0 4px 16px rgba(0,0,0,0.1)"
|
|
298
|
+
}
|
|
299
|
+
.card-body {
|
|
300
|
+
marginTop: "0.75rem"
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
`);
|
|
305
|
+
|
|
306
|
+
fs.writeFileSync(path.join(root, "styles", "reset.css"),
|
|
307
|
+
`*, *::before, *::after { box-sizing: border-box; }\nbody { margin: 0; font-family: system-ui, sans-serif; line-height: 1.6; }\nimg { max-width: 100%; display: block; }\n`
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
fs.writeFileSync(path.join(root, "sinth.config.json"),
|
|
311
|
+
JSON.stringify({ outDir: "./dist", libraryPaths: ["./libraries"], minify: false }, null, 2)
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
fs.writeFileSync(path.join(root, ".gitignore"), "dist/\nnode_modules/\n");
|
|
315
|
+
|
|
316
|
+
fs.writeFileSync(path.join(root, "package.json"), JSON.stringify({
|
|
317
|
+
name: path.basename(root).toLowerCase().replace(/\s+/g, "-"),
|
|
318
|
+
version: "1.0.0",
|
|
319
|
+
scripts: { build: "sinth build", dev: "sinth dev" },
|
|
320
|
+
dependencies: { "ts-node": "^10.0.0", "sass": "^1.70.0" },
|
|
321
|
+
}, null, 2));
|
|
322
|
+
|
|
323
|
+
process.stdout.write(`
|
|
324
|
+
\x1b[32m✓ Sinth project scaffolded at ${path.basename(root)}/\x1b[0m
|
|
325
|
+
|
|
326
|
+
\x1b[1mNext steps:\x1b[0m
|
|
327
|
+
cd ${path.basename(root)}
|
|
328
|
+
sinth dev
|
|
329
|
+
sinth build
|
|
330
|
+
`);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
main().catch(e => {
|
|
337
|
+
process.stderr.write(`\x1b[31m${(e as Error).message}\x1b[0m\n`);
|
|
338
|
+
process.exit(1);
|
|
339
339
|
});
|