@caleuche/core 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.
package/README.md CHANGED
@@ -1,12 +1,28 @@
1
1
  # Caleuche Library
2
2
 
3
- This package provides core logic for compiling code samples and generating project files for multiple languages. It is designed to be consumed by the CLI or other tools.
3
+ Caleuche is a template-based code generation library that compiles code samples with dynamic inputs into runnable code files across multiple programming languages. It enables you to create reusable code templates using EJS-style syntax (`<%= %>`) and generate complete, executable projects with language-specific project files and dependency manifests.
4
+
5
+ The library acts as the core engine for tools like the Caleuche CLI, providing a programmatic API for template compilation, input validation, and multi-language code generation. Whether you're building code snippet generators, documentation systems with runnable examples, or developer tools that need to produce working code samples, Caleuche simplifies the process of transforming templates into fully functional code.
4
6
 
5
7
  ## Features
6
8
 
7
- - Compile code samples for C#, Go, Java, Python, and JavaScript.
8
- - Generate language-specific project files (e.g., `Sample.csproj`, `go.mod`, `pom.xml`, `requirements.txt`, `package.json`).
9
- - Supports template input validation and dependency injection.
9
+ - **Multi-Language Support**: Compile code templates for C#, Go, Java, Python, and JavaScript
10
+ - **EJS-Style Templating**: Use familiar `<%= variable %>` syntax with support for JavaScript expressions and control flow
11
+ - **Project File Generation**: Automatically generate language-specific project files including dependency manifests:
12
+ - C#: `Sample.csproj`
13
+ - Go: `go.mod`
14
+ - Java: `pom.xml`
15
+ - Python: `requirements.txt`
16
+ - JavaScript: `package.json`
17
+ - **Rich Input Types**: Define template inputs with strong typing support:
18
+ - Primitives: `string`, `number`, `boolean`
19
+ - Complex types: `object`, `array` (with typed items)
20
+ - Required/optional fields with default values
21
+ - **Template Helper Functions**: Built-in language-specific helpers for common code generation tasks:
22
+ - Import/using statement generation
23
+ - Environment variable handling with runtime validation
24
+ - **Metadata Support**: Attach custom tags to samples for categorization and filtering
25
+ - **Input Validation**: Automatic validation of required inputs with clear error messages
10
26
 
11
27
  ## Installation
12
28
 
@@ -24,7 +40,7 @@ Import the main API in your TypeScript/JavaScript project:
24
40
  import { compileSample, Sample, CompileOptions, CompileOutput } from "caleuche";
25
41
  ```
26
42
 
27
- ### Example
43
+ ### Basic Example
28
44
 
29
45
  ```ts
30
46
  const sample: Sample = {
@@ -38,36 +54,298 @@ const options: CompileOptions = { project: true };
38
54
 
39
55
  const output: CompileOutput = compileSample(sample, { name: "World" }, options);
40
56
  // output.items will contain the generated files
57
+ // - sample.js: contains compiled code
58
+ // - package.json: contains project metadata and dependencies
59
+ ```
60
+
61
+ ### Working with Input Types
62
+
63
+ Caleuche supports various input types with validation and defaults:
64
+
65
+ ```ts
66
+ const sample: Sample = {
67
+ template: `
68
+ const config = {
69
+ name: "<%= name %>",
70
+ port: <%= port %>,
71
+ debug: <%= debug %>,
72
+ features: <%= JSON.stringify(features) %>
73
+ };
74
+ `,
75
+ type: "javascript",
76
+ dependencies: [{ name: "express", version: "^4.18.0" }],
77
+ input: [
78
+ { name: "name", type: "string", required: true },
79
+ { name: "port", type: "number", required: false, default: 3000 },
80
+ { name: "debug", type: "boolean", required: false, default: false },
81
+ {
82
+ name: "features",
83
+ type: "array",
84
+ itemsType: "string",
85
+ required: false,
86
+ default: ["logging"],
87
+ },
88
+ ],
89
+ };
90
+
91
+ const output = compileSample(
92
+ sample,
93
+ {
94
+ name: "MyApp",
95
+ port: 8080,
96
+ features: ["auth", "logging", "metrics"],
97
+ },
98
+ { project: true },
99
+ );
100
+ ```
101
+
102
+ ### Using Template Helpers
103
+
104
+ Templates can use language-specific helper functions for common tasks:
105
+
106
+ ```ts
107
+ // Go sample with import management
108
+ const goSample: Sample = {
109
+ template: `
110
+ package main
111
+
112
+ <%- go.includes("fmt", "os", { module: "github.com/joho/godotenv", condition: useEnv }) %>
113
+
114
+ func main() {
115
+ <%- go.valueOrEnvironment(useEnv, "apiKey", "API_KEY", hardcodedKey) %>
116
+ fmt.Println(apiKey)
117
+ }
118
+ `,
119
+ type: "go",
120
+ dependencies: [],
121
+ input: [
122
+ { name: "useEnv", type: "boolean", required: true },
123
+ {
124
+ name: "hardcodedKey",
125
+ type: "string",
126
+ required: false,
127
+ default: "default-key",
128
+ },
129
+ ],
130
+ };
131
+ ```
132
+
133
+ ### Adding Metadata with Tags
134
+
135
+ Attach custom metadata to samples for organization and filtering:
136
+
137
+ ```ts
138
+ const sample: Sample = {
139
+ template: 'print("Hello, <%= name %>!")',
140
+ type: "python",
141
+ dependencies: [],
142
+ input: [{ name: "name", type: "string", required: true }],
143
+ tags: {
144
+ category: "basic",
145
+ difficulty: "beginner",
146
+ version: "1.0.0",
147
+ },
148
+ };
149
+
150
+ const output = compileSample(sample, { name: "World" }, { project: false });
151
+ // Output includes tags.yaml file with metadata
41
152
  ```
42
153
 
43
154
  ### Template Built-ins
44
155
 
45
- For use in authoring templates. All functions described below are ingested by Caleuche when compiling code files.
156
+ Template helper functions are available within templates to generate language-specific boilerplate code. These functions are accessed via the language namespace (e.g., `go.includes()`, `csharp.usings()`).
157
+
158
+ #### Universal Helpers
159
+
160
+ Available for all supported languages:
161
+
162
+ - **`<language>.valueOrEnvironment(useEnvironmentVariable, variableName, environmentVariable, value, indentationLevel?)`**
163
+
164
+ Generates code to assign a variable either from an environment variable at runtime (with validation) or from a provided static value.
165
+
166
+ - `useEnvironmentVariable` (boolean): If true, generates code to read from environment variable
167
+ - `variableName` (string): Name of the variable to create
168
+ - `environmentVariable` (string): Name of the environment variable to read from
169
+ - `value` (string, optional): Static value to use if not reading from environment
170
+ - `indentationLevel` (number, optional): Indentation level for generated code (default varies by language)
171
+
172
+ **Example:**
173
+
174
+ ```ts
175
+ // In a Go template:
176
+ <%- go.valueOrEnvironment(true, "apiKey", "API_KEY") %>
177
+ // Generates:
178
+ // apiKey := os.Getenv("API_KEY")
179
+ // if len(apiKey) == 0 {
180
+ // fmt.Println("Please set the API_KEY environment variable.")
181
+ // os.Exit(1)
182
+ // }
183
+ ```
184
+
185
+ #### Language-Specific Helpers
186
+
187
+ **Go**
188
+
189
+ - **`go.includes(...items)`**
46
190
 
47
- #### Universal
191
+ Generates Go import statements with automatic grouping (standard library vs. external packages).
48
192
 
49
- - `<language>.valueOrEnvironment(useEnvironmentVariable: boolean, variableName: string, environmentVariable: string, value: string)` Generates code to assign a variable from a specified environment variable `environmentVariable` at runtime or from a provided value `value`.
193
+ - `items`: Rest parameter accepting strings or objects with `{ module: string, condition?: boolean }`
50
194
 
51
- #### Language Specific
52
- - `go.includes(...items: string | {module: string; condition?: boolean})` Generates go import statement from list of dependencies. Exclude a dependency by setting condition to false.
195
+ **Example:**
53
196
 
54
- - `csharp.usings(...items: string | {namespace: string; condition?: boolean})` Generates C# using statements from a list of namespaces. Exclude a namespace by setting condition to false.
197
+ ```ts
198
+ <%- go.includes("fmt", "os", { module: "github.com/joho/godotenv", condition: needsDotenv }) %>
199
+ // Generates:
200
+ // import (
201
+ // "fmt"
202
+ // "os"
203
+ //
204
+ // "github.com/joho/godotenv"
205
+ // )
206
+ ```
207
+
208
+ **C#**
209
+
210
+ - **`csharp.usings(...items)`**
211
+
212
+ Generates C# using statements.
213
+
214
+ - `items`: Rest parameter accepting strings or objects with `{ namespace: string, condition?: boolean }`
215
+
216
+ **Example:**
217
+
218
+ ```ts
219
+ <%- csharp.usings("System", "System.IO", { namespace: "System.Net.Http", condition: needsHttp }) %>
220
+ // Generates:
221
+ // using System;
222
+ // using System.IO;
223
+ // using System.Net.Http;
224
+ ```
225
+
226
+ **Java, Python, JavaScript**
227
+
228
+ These languages have the `valueOrEnvironment` helper available but no additional language-specific helpers at this time.
55
229
 
56
230
  ## API
57
231
 
58
232
  ### Types
59
233
 
60
- - `Language`: `"csharp" | "java" | "python" | "go" | "javascript"`
61
- - `Dependency`: `{ name: string; version: string }`
62
- - `TemplateInput`: Input parameter definition for templates.
63
- - `Sample`: Describes a code sample, its language, dependencies, and inputs.
64
- - `CompileOptions`: `{ project: boolean }`
65
- - `CompileOutput`: `{ items: Array<{ fileName: string; content: string }> }`
234
+ **`Language`**
235
+
236
+ ```ts
237
+ type Language = "csharp" | "java" | "python" | "go" | "javascript";
238
+ ```
239
+
240
+ **`Dependency`**
241
+
242
+ ```ts
243
+ interface Dependency {
244
+ name: string;
245
+ version: string;
246
+ }
247
+ ```
248
+
249
+ **`TemplateInput`**
250
+
251
+ Defines an input parameter for a template. Supports multiple types:
252
+
253
+ ```ts
254
+ type TemplateInput =
255
+ | {
256
+ name: string;
257
+ type: "string";
258
+ required: boolean;
259
+ default?: string;
260
+ }
261
+ | {
262
+ name: string;
263
+ type: "number";
264
+ required: boolean;
265
+ default?: number;
266
+ }
267
+ | {
268
+ name: string;
269
+ type: "boolean";
270
+ required: boolean;
271
+ default?: boolean;
272
+ }
273
+ | {
274
+ name: string;
275
+ type: "object";
276
+ required: boolean;
277
+ default?: Record<string, any>;
278
+ }
279
+ | {
280
+ name: string;
281
+ type: "array";
282
+ itemsType: "string" | "number" | "boolean" | "object";
283
+ required: boolean;
284
+ default?: Array<string | number | boolean | Record<string, any>>;
285
+ };
286
+ ```
287
+
288
+ **`Sample`**
289
+
290
+ Describes a code sample with its template, language, dependencies, inputs, and optional metadata:
291
+
292
+ ```ts
293
+ interface Sample {
294
+ template: string; // EJS-style template string
295
+ type: Language; // Target language
296
+ dependencies: Dependency[]; // Package dependencies
297
+ input: TemplateInput[]; // Template input definitions
298
+ tags?: Record<string, any>; // Optional metadata tags
299
+ }
300
+ ```
301
+
302
+ **`CompileOptions`**
303
+
304
+ ```ts
305
+ interface CompileOptions {
306
+ project: boolean; // If true, generates language-specific project file
307
+ }
308
+ ```
309
+
310
+ **`CompileOutput`**
311
+
312
+ ```ts
313
+ interface CompileOutput {
314
+ items: Array<{
315
+ fileName: string; // Generated file name
316
+ content: string; // File content
317
+ }>;
318
+ }
319
+ ```
66
320
 
67
321
  ### Functions
68
322
 
69
- - `compileSample(sample, input, options): CompileOutput`
70
- Compiles a code sample and returns generated files.
323
+ **`compileSample(sample: Sample, input: Record<string, any>, options: CompileOptions): CompileOutput`**
324
+
325
+ Compiles a code sample template with provided input data and returns generated files.
326
+
327
+ - **`sample`**: Sample definition including template, language, dependencies, and input schema
328
+ - **`input`**: Object containing values for template variables (must satisfy required inputs)
329
+ - **`options`**: Compilation options (e.g., whether to generate project files)
330
+ - **Returns**: Object containing array of generated files with names and content
331
+
332
+ **Example:**
333
+
334
+ ```ts
335
+ const output = compileSample(
336
+ {
337
+ template: 'console.log("<%= message %>");',
338
+ type: "javascript",
339
+ dependencies: [],
340
+ input: [{ name: "message", type: "string", required: true }],
341
+ },
342
+ { message: "Hello World" },
343
+ { project: false },
344
+ );
345
+
346
+ // output.items[0].fileName === "sample.js"
347
+ // output.items[0].content === 'console.log("Hello World");'
348
+ ```
71
349
 
72
350
  ## License
73
351
 
package/dist/index.cjs.js CHANGED
@@ -7658,6 +7658,8 @@ function getProjectFileTemplate(sample) {
7658
7658
  targetFileName: `requirements.txt`,
7659
7659
  template: preprocessTemplate(requirementsTxt),
7660
7660
  };
7661
+ case "shell":
7662
+ throw new Error(`Shell language does not support project file generation.`);
7661
7663
  default:
7662
7664
  throw new Error(`Unsupported language: ${language}. Cannot generate project file.`);
7663
7665
  }
@@ -7694,6 +7696,8 @@ function getTargetFileName(sample) {
7694
7696
  return "Sample.java";
7695
7697
  case "python":
7696
7698
  return "sample.py";
7699
+ case "shell":
7700
+ return "sample.sh";
7697
7701
  default:
7698
7702
  throw new Error(`Unsupported language: ${language}. Cannot generate sample file.`);
7699
7703
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caleuche/core",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "main": "dist/index.cjs.js",
5
5
  "exports": {
6
6
  ".": {