kuza-lang 0.4.0 → 0.5.0

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.
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Единый генератор проектов: используется и `kuza new/init`, и postinstall.
3
+ * Шаблоны лежат в packages/cli/templates и попадают в npm-пакет целиком.
4
+ */
5
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
6
+ import { basename, dirname, join, resolve } from "node:path";
7
+ import { FILE_EXT, VERSION } from "kuza-core";
8
+ const TEMPLATES = new URL("../templates/", import.meta.url);
9
+ function template(name) {
10
+ return readFileSync(new URL(name, TEMPLATES), "utf8");
11
+ }
12
+ function packageName(dir) {
13
+ const raw = basename(dir).toLowerCase().replace(/[^a-z0-9-]+/g, "-").replace(/^-+|-+$/g, "");
14
+ return raw || "kuza-app";
15
+ }
16
+ function patchPackageJson(dir, created, force) {
17
+ const path = join(dir, "package.json");
18
+ const scripts = {
19
+ dev: "kuza dev",
20
+ start: "kuza dev",
21
+ build: "kuza build",
22
+ check: "kuza check",
23
+ };
24
+ let data;
25
+ if (existsSync(path)) {
26
+ try {
27
+ data = JSON.parse(readFileSync(path, "utf8"));
28
+ }
29
+ catch {
30
+ if (!force)
31
+ return;
32
+ data = {};
33
+ }
34
+ }
35
+ else {
36
+ data = {};
37
+ }
38
+ if (!data.name)
39
+ data.name = packageName(dir);
40
+ if (!data.private)
41
+ data.private = true;
42
+ if (!data.type)
43
+ data.type = "module";
44
+ data.scripts = { ...scripts, ...data.scripts };
45
+ const deps = (data.dependencies ?? {});
46
+ if (!deps["kuza-lang"])
47
+ deps["kuza-lang"] = `^${VERSION}`;
48
+ data.dependencies = deps;
49
+ if (!existsSync(path) || force)
50
+ created.push("package.json");
51
+ writeFileSync(path, JSON.stringify(data, null, 2) + "\n", "utf8");
52
+ }
53
+ /** Создать базовый проект. Возвращает список созданных файлов. */
54
+ export function scaffoldProject(target, options = {}) {
55
+ const dir = resolve(target ?? process.cwd());
56
+ const force = options.force ?? false;
57
+ mkdirSync(dir, { recursive: true });
58
+ const created = [];
59
+ const write = (name, content) => {
60
+ const path = join(dir, name);
61
+ if (existsSync(path) && !force)
62
+ return;
63
+ mkdirSync(dirname(path), { recursive: true });
64
+ writeFileSync(path, content, { encoding: "utf8", flag: force ? "w" : "wx" });
65
+ created.push(name);
66
+ };
67
+ if (options.scriptOnly) {
68
+ write(`main${FILE_EXT}`, template("main-script.kuza"));
69
+ }
70
+ else {
71
+ write("index.html", template("index.html"));
72
+ write("public/favicon.svg", template("public/favicon.svg"));
73
+ write(`main${FILE_EXT}`, template("main.kuza"));
74
+ write("START.md", template("START.md"));
75
+ write("README.md", `# ${packageName(dir)}\n\nA website built with Kuza.\n\n` +
76
+ "```bash\nnpm install\nnpm run dev\n```\n\n" +
77
+ "Open `START.md` for commands, AI, plugins, backend and VS Code setup.\n");
78
+ write(".gitignore", "node_modules/\ndist/\n.env\n");
79
+ }
80
+ patchPackageJson(dir, created, force);
81
+ return created;
82
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kuza-lang",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Kuza — консоль языка: запуск, сборка, REPL и ИИ-помощник",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -10,6 +10,8 @@
10
10
  "main": "./dist/index.js",
11
11
  "files": [
12
12
  "dist",
13
+ "templates",
14
+ "scripts/postinstall.mjs",
13
15
  "README.md"
14
16
  ],
15
17
  "engines": {
@@ -24,10 +26,11 @@
24
26
  ],
25
27
  "scripts": {
26
28
  "build": "tsc -p tsconfig.json",
29
+ "postinstall": "node scripts/postinstall.mjs",
27
30
  "start": "node dist/index.js"
28
31
  },
29
32
  "dependencies": {
30
- "kuza-core": "^0.4.0",
33
+ "kuza-core": "^0.5.0",
31
34
  "boxen": "^8.0.1",
32
35
  "chalk": "^5.3.0",
33
36
  "gradient-string": "^3.0.0"
@@ -0,0 +1,39 @@
1
+ /**
2
+ * postinstall для kuza-lang.
3
+ *
4
+ * npm вызывает его из папки самого пакета, поэтому настоящую папку проекта
5
+ * берём из INIT_CWD. Скаффолдим только «свежий» проект: если уже есть другой
6
+ * код или зависимости, молча ничего не трогаем.
7
+ */
8
+
9
+ import { existsSync, readFileSync } from "node:fs";
10
+ import { join, resolve } from "node:path";
11
+
12
+ const cwd = process.env.INIT_CWD ? resolve(process.env.INIT_CWD) : process.cwd();
13
+
14
+ if (process.env.npm_config_global === "true") process.exit(0);
15
+
16
+ const has = (name) => existsSync(join(cwd, name));
17
+ if (has("index.html") || has("src") || has("app") || has("pages") || has("main.kuza")) {
18
+ process.exit(0);
19
+ }
20
+
21
+ try {
22
+ const pkg = JSON.parse(readFileSync(join(cwd, "package.json"), "utf8"));
23
+ const deps = { ...(pkg.dependencies || {}), ...(pkg.devDependencies || {}) };
24
+ const other = Object.keys(deps).filter((name) => name !== "kuza-lang");
25
+ if (other.length > 0 || /kuza-monorepo/.test(String(pkg.name || ""))) process.exit(0);
26
+ } catch {
27
+ // package.json ещё нет — это тоже свежий проект, создадим его.
28
+ }
29
+
30
+ try {
31
+ const { scaffoldProject } = await import(new URL("../dist/scaffold.js", import.meta.url));
32
+ const created = scaffoldProject(cwd);
33
+ if (created.length > 0) {
34
+ console.log("\nKuza: created " + created.join(", "));
35
+ console.log("Kuza: run `npm run dev` → http://localhost:5173\n");
36
+ }
37
+ } catch {
38
+ // postinstall никогда не должен ломать установку.
39
+ }
@@ -0,0 +1,133 @@
1
+ # Kuza — start here
2
+
3
+ You already have a working Kuza project.
4
+
5
+ ```text
6
+ index.html — page shell (Tailwind is already connected)
7
+ main.kuza — the whole application
8
+ public/favicon.svg — the site icon
9
+ START.md — this instruction
10
+ ```
11
+
12
+ > Kuza files use `.kuza`. `index.qza` / `main.qza` are not used — the language is
13
+ > **Kuza**, so the extension is `.kuza`.
14
+
15
+ ## 1. Run it
16
+
17
+ ```bash
18
+ npm run dev # http://localhost:5173
19
+ npm run build # production site in dist/
20
+ npm run check # compile without running
21
+ npm test # if you add tests
22
+ ```
23
+
24
+ `npm start` is an alias for `npm run dev`.
25
+
26
+ ## 2. Change it
27
+
28
+ Open `main.kuza`. The `view` block is the screen:
29
+
30
+ ```kuza
31
+ :count = 0;
32
+
33
+ view App<> >> [
34
+ body<"p-8"> >> [
35
+ heading >> <"Count: {count}", "text-2xl font-bold">;
36
+ button >> <"Add one", increase, "rounded bg-orange-500 px-4 py-2">;
37
+ ];
38
+ render;
39
+ ];
40
+
41
+ func increase<> >> [
42
+ count = count + 1; // the screen updates automatically
43
+ ];
44
+ ```
45
+
46
+ Classes are Tailwind classes, written right in Kuza. Containers also accept
47
+ classes: `card >> <"p-4">` or `card<"p-4"> >> [ ... ]`.
48
+
49
+ ## 3. More pages
50
+
51
+ Create `pages/about.kuza`. It becomes available at `/about` in dev and
52
+ `dist/about/index.html` after build. `main.kuza` stays the home page.
53
+
54
+ ## 4. CLI
55
+
56
+ ```bash
57
+ npx kuza-lang new my-site # create a project in ./my-site
58
+ kuza start # same as kuza dev
59
+ kuza build # build into dist/
60
+ kuza check # check syntax
61
+ kuza ai "explain main.kuza" # AI assistant reads your files
62
+ kuza help # all commands
63
+ ```
64
+
65
+ ## 5. AI assistant
66
+
67
+ The assistant can read `.kuza`, `.html`, `.js`, `.ts`, `.json` and `.md` files
68
+ from the current folder. Set a key in `.env` or in the environment:
69
+
70
+ ```dotenv
71
+ KUZA_AI_PROVIDER=openai
72
+ KUZA_AI_KEY=sk-...
73
+ KUZA_AI_MODEL=gpt-4o-mini
74
+ # or for Anthropic:
75
+ # ANTHROPIC_API_KEY=...
76
+ ```
77
+
78
+ OpenAI-compatible services (OpenRouter, Groq, Ollama, Freebuff) work through
79
+ `KUZA_AI_BASE_URL`. The key is never printed.
80
+
81
+ ## 6. Backend
82
+
83
+ ```kuza
84
+ func home<> >> [
85
+ respond >> <{ ok: true, message: "Hello" }, 200>;
86
+ ];
87
+
88
+ route >> <"GET", "/", home>;
89
+ listen >> 3000;
90
+ ```
91
+
92
+ Run with `kuza serve server.kuza`, then open `http://localhost:3000`.
93
+ `param`, `query`, `requestBody`, `header`, `method` and `serverPath` read the
94
+ current request.
95
+
96
+ ## 7. Plugins
97
+
98
+ Create `kuza.config.js` next to `package.json`:
99
+
100
+ ```js
101
+ export default {
102
+ commands: [
103
+ {
104
+ id: "shout", name: "shout", aliases: [], category: "output",
105
+ args: "single", params: ["text"],
106
+ description: "Uppercase output", example: 'shout >> "hi";',
107
+ codegen: "console.log(String({0}).toUpperCase())",
108
+ },
109
+ ],
110
+ plugins: [
111
+ {
112
+ name: "my-plugin",
113
+ setup(api) {
114
+ api.onCompile(({ source }) => source);
115
+ api.onBuild(({ code }) => code);
116
+ api.onRun(({ result }) => result);
117
+ },
118
+ },
119
+ ],
120
+ };
121
+ ```
122
+
123
+ ## 8. VS Code
124
+
125
+ Search for **Kuza** in the Extensions panel, or install the local
126
+ `packages/vscode/*.vsix` via **Install from VSIX…**. The extension gives
127
+ syntax highlighting, completion, diagnostics and Run/Build/Dev buttons.
128
+
129
+ ## Links
130
+
131
+ - All commands: https://github.com/kuza-lang/kuza/blob/main/docs/commands.md
132
+ - Compiler API: `import { compile, run } from "kuza-core"`
133
+ - Package with CLI: `kuza-lang`
@@ -0,0 +1,19 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
6
+ <meta name="theme-color" content="#101417" />
7
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
8
+ <meta
9
+ name="description"
10
+ content="A Kuza website. Edit main.kuza and the screen updates automatically."
11
+ />
12
+ <title>My Kuza app</title>
13
+ <script src="https://cdn.tailwindcss.com"></script>
14
+ </head>
15
+ <body class="min-h-screen bg-slate-950">
16
+ <!-- Kuza mounts the application here. Do not remove this marker. -->
17
+ {main.kuza}
18
+ </body>
19
+ </html>
@@ -0,0 +1,14 @@
1
+ ## main.kuza — first console program on Kuza
2
+ ## Run: npx kuza-lang run main.kuza
3
+
4
+ :name = "world";
5
+ log >> "Hello, {name}!";
6
+
7
+ :numbers = [1, 2, 3, 4, 5];
8
+ log >> "Sum: {sum >> numbers}";
9
+
10
+ ?len >> numbers > 3 >> [
11
+ log >> "More than three numbers";
12
+ ] || [
13
+ log >> "Too few numbers";
14
+ ]
@@ -0,0 +1,36 @@
1
+ ## main.kuza — your first Kuza website
2
+ ## npm run dev → http://localhost:5173
3
+ ## npm run build → dist/index.html
4
+ ## Edit this file. The view updates automatically when count changes.
5
+
6
+ :count = 0;
7
+
8
+ view App<> >> [
9
+ page >> "Hello, Kuza!";
10
+
11
+ body<"mx-auto max-w-2xl px-6 py-16"> >> [
12
+ card<"rounded-2xl border border-white/10 bg-white/5 p-8 shadow-xl"> >> [
13
+ image >> <"/favicon.svg", "Kuza icon">;
14
+ heading >> <"Hello, Kuza!", "mt-4 text-4xl font-bold tracking-tight text-white">;
15
+ para >> <"Your first website. Change main.kuza — no manual redraw needed.", "mt-3 text-slate-300">;
16
+ para >> <"Count: {count}", "mt-8 text-2xl font-semibold text-orange-400">;
17
+
18
+ row<"mt-5 flex flex-wrap gap-3"> >> [
19
+ button >> <"Add one", increase, "rounded-lg bg-orange-500 px-4 py-2 font-semibold text-slate-950 hover:bg-orange-400">;
20
+ button >> <"Reset", reset, "rounded-lg border border-white/20 px-4 py-2 text-slate-200 hover:bg-white/10">;
21
+ ];
22
+
23
+ link >> <"Read the docs", "https://github.com/kuza-lang/kuza/blob/main/docs/commands.md", "mt-6 inline-block text-orange-400 underline underline-offset-4">;
24
+ ];
25
+ ];
26
+
27
+ render;
28
+ ];
29
+
30
+ func increase<> >> [
31
+ count = count + 1;
32
+ ];
33
+
34
+ func reset<> >> [
35
+ count = 0;
36
+ ];
@@ -0,0 +1,4 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
2
+ <rect width="64" height="64" rx="14" fill="#101417" />
3
+ <path d="M18 14h8v15.5L42 14h10L37.5 32 53 50H42.5L26 34.5V50h-8z" fill="#fb923c" />
4
+ </svg>