@caleuche/core 0.1.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/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # Caleuche Library
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.
4
+
5
+ ## Features
6
+
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.
10
+
11
+ ## Installation
12
+
13
+ Install via npm:
14
+
15
+ ```sh
16
+ npm install caleuche
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Import the main API in your TypeScript/JavaScript project:
22
+
23
+ ```ts
24
+ import { compileSample, Sample, CompileOptions, CompileOutput } from "caleuche";
25
+ ```
26
+
27
+ ### Example
28
+
29
+ ```ts
30
+ const sample: Sample = {
31
+ template: 'console.log("Hello, <%= name %>!");',
32
+ type: "javascript",
33
+ dependencies: [],
34
+ input: [{ name: "name", type: "string", required: true }],
35
+ };
36
+
37
+ const options: CompileOptions = { project: true };
38
+
39
+ const output: CompileOutput = compileSample(sample, { name: "World" }, options);
40
+ // output.items will contain the generated files
41
+ ```
42
+
43
+ ## API
44
+
45
+ ### Types
46
+
47
+ - `Language`: `"csharp" | "java" | "python" | "go" | "javascript"`
48
+ - `Dependency`: `{ name: string; version: string }`
49
+ - `TemplateInput`: Input parameter definition for templates.
50
+ - `Sample`: Describes a code sample, its language, dependencies, and inputs.
51
+ - `CompileOptions`: `{ project: boolean }`
52
+ - `CompileOutput`: `{ items: Array<{ fileName: string; content: string }> }`
53
+
54
+ ### Functions
55
+
56
+ - `compileSample(sample, input, options): CompileOutput`
57
+ Compiles a code sample and returns generated files.
58
+
59
+ ## License
60
+
61
+ MIT
@@ -0,0 +1,215 @@
1
+ 'use strict';
2
+
3
+ var _ = require('lodash');
4
+
5
+ function usings(...items) {
6
+ return items
7
+ .filter((u) => typeof u === "string" || u.condition)
8
+ .map((u) => `using ${typeof u === "string" ? u : u.namespace};`)
9
+ .join("\n");
10
+ }
11
+ function valueOrEnvironment$2(useEnvironmentVariable, variableName, environmentVariable, value) {
12
+ if (useEnvironmentVariable && environmentVariable) {
13
+ return `var ${variableName} = Environment.GetEnvironmentVariable("${environmentVariable}") ?? throw new InvalidOperationException("${environmentVariable} environment variable is not set.")`;
14
+ }
15
+ else if (value) {
16
+ return `const string ${variableName} = "${value}";`;
17
+ }
18
+ else {
19
+ throw new Error("No value provided for variable or environment variable.");
20
+ }
21
+ }
22
+
23
+ var csharp = /*#__PURE__*/Object.freeze({
24
+ __proto__: null,
25
+ usings: usings,
26
+ valueOrEnvironment: valueOrEnvironment$2
27
+ });
28
+
29
+ function includes(...items) {
30
+ let imports = "import (\n";
31
+ const included = items
32
+ .filter((i) => typeof i === "string" || i.condition)
33
+ .map((i) => (typeof i === "string" ? i : i.module));
34
+ const system = included
35
+ .filter((i) => !i.startsWith("github.com/") && !i.startsWith("golang.org/"))
36
+ .sort()
37
+ .map((i) => `\t"${i}"`)
38
+ .join("\n");
39
+ const nonSystem = included
40
+ .filter((i) => i.startsWith("github.com/") || i.startsWith("golang.org/"))
41
+ .sort()
42
+ .map((i) => `\t"${i}"`)
43
+ .join("\n");
44
+ imports += [system, nonSystem]
45
+ .filter((i) => i !== null && i.length > 0)
46
+ .join("\n\n");
47
+ imports += "\n)";
48
+ return imports;
49
+ }
50
+ function valueOrEnvironment$1(useEnvironmentVariable, variableName, environmentVariable, value, indentationLevel = 1) {
51
+ if (!variableName) {
52
+ throw new Error("Variable name must be provided.");
53
+ }
54
+ const indent = "\t".repeat(indentationLevel);
55
+ if (useEnvironmentVariable && environmentVariable) {
56
+ return (`${variableName} := os.Getenv("${environmentVariable}")\n` +
57
+ `${indent}if len(${variableName}) == 0 {\n` +
58
+ `${indent}\tfmt.Println("Please set the ${environmentVariable} environment variable.")\n` +
59
+ `${indent}\tos.Exit(1)\n` +
60
+ `${indent}}`);
61
+ }
62
+ else if (value) {
63
+ return `const ${variableName} = "${value}"`;
64
+ }
65
+ else {
66
+ throw new Error("No value provided for variable or environment variable.");
67
+ }
68
+ }
69
+
70
+ var go = /*#__PURE__*/Object.freeze({
71
+ __proto__: null,
72
+ includes: includes,
73
+ valueOrEnvironment: valueOrEnvironment$1
74
+ });
75
+
76
+ function valueOrEnvironment(useEnvironmentVariable, variableName, environmentVariable, value, indentationLevel = 0) {
77
+ if (!variableName) {
78
+ throw new Error("Variable name must be provided.");
79
+ }
80
+ const indent = " ".repeat(indentationLevel);
81
+ if (useEnvironmentVariable && environmentVariable) {
82
+ return (`${variableName} = os.environ.get("${environmentVariable}")\n` +
83
+ `${indent}if not ${variableName}:\n` +
84
+ `${indent} raise ValueError("Please set the ${environmentVariable} environment variable.")\n`);
85
+ }
86
+ else if (value) {
87
+ return `${variableName} = "${value}"`;
88
+ }
89
+ else {
90
+ throw new Error("No value provided for variable or environment variable.");
91
+ }
92
+ }
93
+
94
+ var python = /*#__PURE__*/Object.freeze({
95
+ __proto__: null,
96
+ valueOrEnvironment: valueOrEnvironment
97
+ });
98
+
99
+ var csproj = "<Project Sdk=\"Microsoft.NET.Sdk\">\n\n <PropertyGroup>\n <OutputType>Exe</OutputType>\n <TargetFramework>net9.0</TargetFramework>\n <ImplicitUsings>enable</ImplicitUsings>\n <Nullable>enable</Nullable>\n </PropertyGroup>\n\n <ItemGroup>\n <% _.forEach(dependencies, function(dependency) { %>\n <PackageReference Include=\"<%= dependency.name %>\" Version=\"<%= dependency.version %>\" />\n <% }); %>\n </ItemGroup>\n\n</Project>\n";
100
+
101
+ var gomod = "module sample\n\ngo 1.24.3\n\nrequire (\n<% _.forEach(dependencies, function(dependency) { %>\n <%= dependency.name %> <%= dependency.version %>\n<% }) %>\n)\n";
102
+
103
+ var packageJson = "{\n \"name\": \"sample\",\n \"version\": \"1.0.0\",\n \"type\": \"module\",\n \"dependencies\": {\n<% _.forEach(dependencies, function(dependency, i) { %>\n \"<%= dependency.name %>\": \"<%= dependency.version %>\"<% if (i < dependencies.length - 1) { %>,<% } %>\n<% }); %>\n }\n}\n";
104
+
105
+ var pomXml = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n <modelVersion>4.0.0</modelVersion>\n <groupId>sample</groupId>\n <artifactId>sample</artifactId>\n <version>1.0-SNAPSHOT</version>\n <dependencies>\n <% _.forEach(dependencies, function(dependency) { %>\n <dependency>\n <groupId><%= dependency.name.split(\":\")[0] %></groupId>\n <artifactId><%= dependency.name.split(\":\")[1] %></artifactId>\n <version><%= dependency.version %></version>\n </dependency>\n <% }); %>\n </dependencies>\n <build>\n <plugins>\n <plugin>\n <groupId>org.codehaus.mojo</groupId>\n <artifactId>build-helper-maven-plugin</artifactId>\n <version>3.6.0</version>\n <executions>\n <execution>\n <id>add-source</id>\n <phase>generate-sources</phase>\n <goals>\n <goal>add-source</goal>\n </goals>\n <configuration>\n <sources>\n <source>.</source>\n </sources>\n </configuration>\n </execution>\n </executions>\n </plugin>\n </plugins>\n </build>\n</project>\n";
106
+
107
+ var requirementsTxt = "<% _.forEach(dependencies, function(dependency) { %>\n<%= dependency.name %>==<%= dependency.version %>\n<% }) %>";
108
+
109
+ function fillInputObject(sample, inputData) {
110
+ const inputObject = {};
111
+ for (const { name, required, default: defaultValue } of sample.input) {
112
+ if (name in inputData) {
113
+ /* TODO: Check type */
114
+ inputObject[name] = inputData[name];
115
+ }
116
+ else {
117
+ if (!required) {
118
+ inputObject[name] = defaultValue;
119
+ continue;
120
+ }
121
+ throw new Error(`Missing required input: ${name}. Please provide a value for this input.`);
122
+ }
123
+ }
124
+ return inputObject;
125
+ }
126
+ const regex = /[ \t]*(<%(?!=)[^%]+%>)\r?\n/g;
127
+ function preprocessTemplate(template) {
128
+ return template.replace(regex, "$1");
129
+ }
130
+ function getProjectFileTemplate(sample) {
131
+ const language = sample.type;
132
+ switch (language) {
133
+ case "csharp":
134
+ return {
135
+ targetFileName: `Sample.csproj`,
136
+ template: preprocessTemplate(csproj),
137
+ };
138
+ case "go":
139
+ return {
140
+ targetFileName: `go.mod`,
141
+ template: preprocessTemplate(gomod),
142
+ };
143
+ case "javascript":
144
+ return {
145
+ targetFileName: `package.json`,
146
+ template: preprocessTemplate(packageJson),
147
+ };
148
+ case "java":
149
+ return {
150
+ targetFileName: `pom.xml`,
151
+ template: preprocessTemplate(pomXml),
152
+ };
153
+ case "python":
154
+ return {
155
+ targetFileName: `requirements.txt`,
156
+ template: preprocessTemplate(requirementsTxt),
157
+ };
158
+ default:
159
+ throw new Error(`Unsupported language: ${language}. Cannot generate project file.`);
160
+ }
161
+ }
162
+ function generateProjectFile(sample) {
163
+ const { targetFileName, template } = getProjectFileTemplate(sample);
164
+ const compiledTemplate = _.template(template);
165
+ const instantiatedTemplate = compiledTemplate({
166
+ dependencies: sample.dependencies,
167
+ });
168
+ return {
169
+ fileName: targetFileName,
170
+ content: instantiatedTemplate,
171
+ };
172
+ }
173
+ function getTargetFileName(sample) {
174
+ const language = sample.type;
175
+ switch (language) {
176
+ case "csharp":
177
+ return "Sample.cs";
178
+ case "go":
179
+ return "sample.go";
180
+ case "javascript":
181
+ return "sample.js";
182
+ case "java":
183
+ return "Sample.java";
184
+ case "python":
185
+ return "sample.py";
186
+ default:
187
+ throw new Error(`Unsupported language: ${language}. Cannot generate sample file.`);
188
+ }
189
+ }
190
+ function compileSample(sample, input, options) {
191
+ const output = { items: [] };
192
+ if (options.project) {
193
+ const projectFile = generateProjectFile(sample);
194
+ output.items.push(projectFile);
195
+ }
196
+ const inputObject = fillInputObject(sample, input);
197
+ const processedTemplate = preprocessTemplate(sample.template);
198
+ const compiledTemplate = _.template(processedTemplate, {
199
+ imports: {
200
+ csharp: csharp,
201
+ go: go,
202
+ python: python,
203
+ },
204
+ });
205
+ const targetFileName = getTargetFileName(sample);
206
+ const outputFileContent = compiledTemplate(inputObject);
207
+ output.items.push({
208
+ fileName: targetFileName,
209
+ content: outputFileContent,
210
+ });
211
+ return output;
212
+ }
213
+
214
+ exports.compileSample = compileSample;
215
+ //# sourceMappingURL=index.cjs.js.map
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@caleuche/core",
3
+ "version": "0.1.2",
4
+ "main": "dist/index.cjs.js",
5
+ "exports": {
6
+ ".": {
7
+ "types": "./dist/types/index.d.ts",
8
+ "import": "./dist/index.esm.js",
9
+ "require": "./dist/index.cjs.js"
10
+ },
11
+ "./package.json": "./package.json"
12
+ },
13
+ "types": "dist/types/index.d.ts",
14
+ "scripts": {
15
+ "build": "rollup -c",
16
+ "build:watch": "rollup -c -w",
17
+ "build:clean": "rimraf dist && rollup -c",
18
+ "build:clean:watch": "rimraf dist && rollup -c -w",
19
+ "clean": "rimraf dist",
20
+ "format": "prettier --write .",
21
+ "prepare": "npm run build"
22
+ },
23
+ "author": "",
24
+ "license": "MIT",
25
+ "description": "",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "https://github.com/brandor64/caleuche"
29
+ },
30
+ "dependencies": {
31
+ "lodash": "^4.17.21",
32
+ "tslib": "^2.8.1"
33
+ },
34
+ "devDependencies": {
35
+ "@rollup/plugin-commonjs": "^28.0.3",
36
+ "@rollup/plugin-node-resolve": "^16.0.1",
37
+ "@rollup/plugin-typescript": "^12.1.2",
38
+ "@types/lodash": "^4.17.17",
39
+ "prettier": "^3.5.3",
40
+ "rimraf": "^6.0.1",
41
+ "rollup": "^4.42.0",
42
+ "typescript": "^5.8.3"
43
+ },
44
+ "files": [
45
+ "./project-templates/Sample.csproj.template",
46
+ "./project-templates/go.mod.template",
47
+ "./project-templates/package.json.template",
48
+ "./project-templates/pom.xml.template",
49
+ "./project-templates/requirements.txt.template",
50
+ "./project-templates/Sample.csproj.template"
51
+ ],
52
+ "bin": {}
53
+ }
@@ -0,0 +1,16 @@
1
+ <Project Sdk="Microsoft.NET.Sdk">
2
+
3
+ <PropertyGroup>
4
+ <OutputType>Exe</OutputType>
5
+ <TargetFramework>net9.0</TargetFramework>
6
+ <ImplicitUsings>enable</ImplicitUsings>
7
+ <Nullable>enable</Nullable>
8
+ </PropertyGroup>
9
+
10
+ <ItemGroup>
11
+ <% _.forEach(dependencies, function(dependency) { %>
12
+ <PackageReference Include="<%= dependency.name %>" Version="<%= dependency.version %>" />
13
+ <% }); %>
14
+ </ItemGroup>
15
+
16
+ </Project>
@@ -0,0 +1,9 @@
1
+ module sample
2
+
3
+ go 1.24.3
4
+
5
+ require (
6
+ <% _.forEach(dependencies, function(dependency) { %>
7
+ <%= dependency.name %> <%= dependency.version %>
8
+ <% }) %>
9
+ )
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "sample",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "dependencies": {
6
+ <% _.forEach(dependencies, function(dependency, i) { %>
7
+ "<%= dependency.name %>": "<%= dependency.version %>"<% if (i < dependencies.length - 1) { %>,<% } %>
8
+ <% }); %>
9
+ }
10
+ }
@@ -0,0 +1,40 @@
1
+ <project xmlns="http://maven.apache.org/POM/4.0.0"
2
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4
+ <modelVersion>4.0.0</modelVersion>
5
+ <groupId>sample</groupId>
6
+ <artifactId>sample</artifactId>
7
+ <version>1.0-SNAPSHOT</version>
8
+ <dependencies>
9
+ <% _.forEach(dependencies, function(dependency) { %>
10
+ <dependency>
11
+ <groupId><%= dependency.name.split(":")[0] %></groupId>
12
+ <artifactId><%= dependency.name.split(":")[1] %></artifactId>
13
+ <version><%= dependency.version %></version>
14
+ </dependency>
15
+ <% }); %>
16
+ </dependencies>
17
+ <build>
18
+ <plugins>
19
+ <plugin>
20
+ <groupId>org.codehaus.mojo</groupId>
21
+ <artifactId>build-helper-maven-plugin</artifactId>
22
+ <version>3.6.0</version>
23
+ <executions>
24
+ <execution>
25
+ <id>add-source</id>
26
+ <phase>generate-sources</phase>
27
+ <goals>
28
+ <goal>add-source</goal>
29
+ </goals>
30
+ <configuration>
31
+ <sources>
32
+ <source>.</source>
33
+ </sources>
34
+ </configuration>
35
+ </execution>
36
+ </executions>
37
+ </plugin>
38
+ </plugins>
39
+ </build>
40
+ </project>
@@ -0,0 +1,3 @@
1
+ <% _.forEach(dependencies, function(dependency) { %>
2
+ <%= dependency.name %>==<%= dependency.version %>
3
+ <% }) %>