@caleuche/cli 0.3.0 → 0.4.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # @caleuche/cli
2
2
 
3
+ ## 0.4.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [1dd1a48]
8
+ - Updated dependencies [6254997]
9
+ - @caleuche/core@0.3.0
10
+
11
+ ## 0.4.0
12
+
13
+ ### Minor Changes
14
+
15
+ - 566dad9: Updated command help strings.
16
+ - 6215af4: Adding option directory to batch command.
17
+
3
18
  ## 0.3.0
4
19
 
5
20
  ### Minor Changes
package/dist/batch.js CHANGED
@@ -34,7 +34,7 @@ function loadVariantDefinitions(variants, workingDirectory) {
34
34
  for (const { name, input } of variants) {
35
35
  const v = loadVariantInputDefinition(input, workingDirectory);
36
36
  if (!v) {
37
- console.error(`Failed to load variant definition for key "${name}": ${input}`);
37
+ console.error(`Failed to load variant definition for key "${name}"`);
38
38
  return null;
39
39
  }
40
40
  definitions[name] = v;
@@ -66,7 +66,7 @@ function resolveVariantDefinition(variant, variantRegistry, workingDirectory) {
66
66
  return null;
67
67
  }
68
68
  }
69
- function batchCompile(batchFile) {
69
+ function batchCompile(batchFile, options) {
70
70
  if (!(0, utils_1.isFile)(batchFile)) {
71
71
  console.error(`Batch file "${batchFile}" does not exist or is not a file.`);
72
72
  process.exit(1);
@@ -97,8 +97,8 @@ function batchCompile(batchFile) {
97
97
  if (!resolvedVariant) {
98
98
  process.exit(1);
99
99
  }
100
- const effectiveOutputPath = path_1.default.join(workingDirectory, variant.output);
101
- if (!(0, common_1.compileAndWriteOutput)(sample, resolvedVariant, effectiveOutputPath, {
100
+ const effectiveOutputPath = path_1.default.join(options?.outputDir || workingDirectory, variant.output);
101
+ if (!(0, common_1.compileAndWriteOutput)(sample, resolvedVariant.properties, effectiveOutputPath, {
102
102
  project: true,
103
103
  })) {
104
104
  console.error(`Sample: ${sampleDefinition.templatePath}, Variant: ${JSON.stringify(variant)}`);
package/dist/index.js CHANGED
@@ -10,8 +10,15 @@ commander_1.program
10
10
  .description("Caleuche CLI for compiling samples")
11
11
  .version(package_json_1.version);
12
12
  commander_1.program
13
- .command("compile <sample-directory> <data-file> <output-directory>")
14
- .option("-p, --project", "Generate project file")
13
+ .command("compile")
14
+ .argument("<sample-path>", "Path to the sample file or the directory that containes it.")
15
+ .argument("<data-file>", "Path to the input to be used to compile the template.")
16
+ .argument("<output-directory>", "Directory where the compiled output will be written.")
17
+ .option("-p, --project", "Flag to indicate whether to generate the appropriate project file along with the compiled template.")
15
18
  .action(compile_1.compile);
16
- commander_1.program.command("batch <batch-file>").action(batch_1.batchCompile);
19
+ commander_1.program
20
+ .command("batch")
21
+ .argument("<batch-file>", "Path to the batch file")
22
+ .option("-d, --output-dir <outputDir>", "Output directory for compiled samples")
23
+ .action(batch_1.batchCompile);
17
24
  commander_1.program.parse();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caleuche/cli",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "main": "dist/index.js",
5
5
  "bin": {
6
6
  "che": "dist/index.js"
@@ -19,7 +19,7 @@
19
19
  "license": "MIT",
20
20
  "description": "Caleuche CLI",
21
21
  "dependencies": {
22
- "@caleuche/core": "^0.2.0",
22
+ "@caleuche/core": "^0.3.0",
23
23
  "commander": "^14.0.0",
24
24
  "yaml": "^2.8.0"
25
25
  },
package/src/batch.ts CHANGED
@@ -42,9 +42,7 @@ function loadVariantDefinitions(
42
42
  for (const { name, input } of variants) {
43
43
  const v = loadVariantInputDefinition(input, workingDirectory);
44
44
  if (!v) {
45
- console.error(
46
- `Failed to load variant definition for key "${name}": ${input}`,
47
- );
45
+ console.error(`Failed to load variant definition for key "${name}"`);
48
46
  return null;
49
47
  }
50
48
  definitions[name] = v;
@@ -82,7 +80,10 @@ function resolveVariantDefinition(
82
80
  }
83
81
  }
84
82
 
85
- export function batchCompile(batchFile: string) {
83
+ export function batchCompile(
84
+ batchFile: string,
85
+ options: { outputDir?: string },
86
+ ) {
86
87
  if (!isFile(batchFile)) {
87
88
  console.error(`Batch file "${batchFile}" does not exist or is not a file.`);
88
89
  process.exit(1);
@@ -127,12 +128,20 @@ export function batchCompile(batchFile: string) {
127
128
  process.exit(1);
128
129
  }
129
130
 
130
- const effectiveOutputPath = path.join(workingDirectory, variant.output);
131
+ const effectiveOutputPath = path.join(
132
+ options?.outputDir || workingDirectory,
133
+ variant.output,
134
+ );
131
135
 
132
136
  if (
133
- !compileAndWriteOutput(sample, resolvedVariant, effectiveOutputPath, {
134
- project: true,
135
- })
137
+ !compileAndWriteOutput(
138
+ sample,
139
+ resolvedVariant.properties,
140
+ effectiveOutputPath,
141
+ {
142
+ project: true,
143
+ },
144
+ )
136
145
  ) {
137
146
  console.error(
138
147
  `Sample: ${sampleDefinition.templatePath}, Variant: ${JSON.stringify(variant)}`,
package/src/index.ts CHANGED
@@ -11,10 +11,32 @@ program
11
11
  .version(version);
12
12
 
13
13
  program
14
- .command("compile <sample-directory> <data-file> <output-directory>")
15
- .option("-p, --project", "Generate project file")
14
+ .command("compile")
15
+ .argument(
16
+ "<sample-path>",
17
+ "Path to the sample file or the directory that containes it.",
18
+ )
19
+ .argument(
20
+ "<data-file>",
21
+ "Path to the input to be used to compile the template.",
22
+ )
23
+ .argument(
24
+ "<output-directory>",
25
+ "Directory where the compiled output will be written.",
26
+ )
27
+ .option(
28
+ "-p, --project",
29
+ "Flag to indicate whether to generate the appropriate project file along with the compiled template.",
30
+ )
16
31
  .action(compile);
17
32
 
18
- program.command("batch <batch-file>").action(batchCompile);
33
+ program
34
+ .command("batch")
35
+ .argument("<batch-file>", "Path to the batch file")
36
+ .option(
37
+ "-d, --output-dir <outputDir>",
38
+ "Output directory for compiled samples",
39
+ )
40
+ .action(batchCompile);
19
41
 
20
42
  program.parse();
package/src/utils.ts CHANGED
@@ -3,6 +3,8 @@ import fs from "fs";
3
3
  import path from "path";
4
4
  import { Sample } from "@caleuche/core";
5
5
 
6
+ export type Optional<T> = T | undefined;
7
+
6
8
  export function parse<T>(filePath: string): T | null {
7
9
  try {
8
10
  const fileContent = fs.readFileSync(filePath, "utf-8");
package/test/bach.test.ts CHANGED
@@ -1,46 +1,23 @@
1
1
  import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
2
- import path from "path";
3
-
4
- vi.mock("fs");
5
2
  import fs from "fs";
6
- const mockFs = vi.mocked(fs);
3
+ import path from "path";
4
+ import os from "os";
7
5
 
8
6
  vi.mock("@caleuche/core");
9
- import { compileSample, Sample } from "@caleuche/core";
7
+ import { compileSample } from "@caleuche/core";
10
8
  const mockCompileSample = vi.mocked(compileSample);
11
9
 
12
- vi.mock("../src/utils");
13
- import {
14
- parse,
15
- resolveSampleFile,
16
- createOutputDirectory,
17
- resolveTemplate,
18
- isVariantInputDefinition,
19
- getAbsoluteDirectoryPath,
20
- isFile,
21
- isVariantInputReference,
22
- getVariantInputReferenceValue,
23
- } from "../src/utils";
24
- const mockParse = vi.mocked(parse);
25
- const mockResolveSampleFile = vi.mocked(resolveSampleFile);
26
- const mockCreateOutputDirectory = vi.mocked(createOutputDirectory);
27
- const mockResolveTemplate = vi.mocked(resolveTemplate);
28
- const mockIsVariantInputReference = vi.mocked(isVariantInputReference);
29
- const mockGetVariantInputReferenceValue = vi.mocked(
30
- getVariantInputReferenceValue,
31
- );
32
- const mockIsVariantInputDefinition = vi.mocked(isVariantInputDefinition);
33
- const mockGetAbsoluteDirectoryPath = vi.mocked(getAbsoluteDirectoryPath);
34
- const mockIsFile = vi.mocked(isFile);
35
-
36
10
  import { batchCompile } from "../src/batch";
11
+ import { Optional } from "../src/utils";
12
+ import { multiline } from "./utils.test";
37
13
 
38
14
  describe("batchCompile", () => {
15
+ let tempDir: Optional<string>;
39
16
  let mockExit: any;
40
17
  let mockConsoleError: any;
41
18
 
42
19
  beforeEach(() => {
43
- vi.clearAllMocks();
20
+ tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "caleuche-cli-test-"));
44
21
  mockExit = vi.spyOn(process, "exit").mockImplementation(() => {
45
22
  throw new Error("process.exit");
46
23
  });
@@ -48,25 +25,30 @@ describe("batchCompile", () => {
48
25
  });
49
26
 
50
27
  afterEach(() => {
28
+ if (tempDir) {
29
+ fs.rmSync(tempDir, { recursive: true });
30
+ }
51
31
  vi.restoreAllMocks();
52
32
  });
53
33
 
34
+ function getPath(relative: string): string {
35
+ return path.join(tempDir!, relative);
36
+ }
37
+
54
38
  it("should exit if batch file does not exist", () => {
55
- mockFs.existsSync.mockReturnValue(false);
56
39
  expect(() => {
57
- batchCompile("batch.yaml");
40
+ batchCompile(getPath("batch.yaml"), {});
58
41
  }).toThrow("process.exit");
59
42
  expect(mockConsoleError).toHaveBeenCalledWith(
60
- 'Batch file "batch.yaml" does not exist or is not a file.',
43
+ `Batch file "${getPath("batch.yaml")}" does not exist or is not a file.`,
61
44
  );
62
45
  expect(mockExit).toHaveBeenCalledWith(1);
63
46
  });
64
47
 
65
48
  it("should exit if batch file is not a file", () => {
66
- mockFs.existsSync.mockReturnValue(true);
67
- mockFs.lstatSync.mockReturnValue({ isFile: () => false } as any);
49
+ fs.mkdirSync(getPath("batch.yaml"));
68
50
  expect(() => {
69
- batchCompile("batch.yaml");
51
+ batchCompile("batch.yaml", {});
70
52
  }).toThrow("process.exit");
71
53
  expect(mockConsoleError).toHaveBeenCalledWith(
72
54
  'Batch file "batch.yaml" does not exist or is not a file.',
@@ -75,134 +57,161 @@ describe("batchCompile", () => {
75
57
  });
76
58
 
77
59
  it("should exit if batch file cannot be parsed", () => {
78
- mockFs.existsSync.mockReturnValue(true);
79
- mockFs.lstatSync.mockReturnValue({ isFile: () => true } as any);
80
- mockParse.mockReturnValueOnce(null);
60
+ const batchFilePath = getPath("batch.yaml");
61
+ const invalidYaml = multiline`
62
+ invalid: yaml: structure:
63
+ broken
64
+ `;
65
+ fs.writeFileSync(batchFilePath, invalidYaml);
66
+
81
67
  expect(() => {
82
- batchCompile("batch.yaml");
68
+ batchCompile(batchFilePath, {});
83
69
  }).toThrow("process.exit");
84
70
  expect(mockConsoleError).toHaveBeenCalledWith(
85
- 'Batch file "batch.yaml" does not exist or is not a file.',
71
+ `Failed to parse batch file: ${batchFilePath}`,
86
72
  );
87
73
  expect(mockExit).toHaveBeenCalledWith(1);
88
74
  });
89
75
 
90
76
  it("should exit if variant definitions cannot be loaded", () => {
91
- mockFs.existsSync.mockReturnValue(true);
92
- mockFs.lstatSync.mockReturnValue({ isFile: () => true } as any);
93
- mockParse
94
- .mockImplementationOnce(() => ({
95
- variants: [
96
- { name: "foo", input: { type: "path", value: "badvariant.yaml" } },
97
- ],
98
- samples: [],
99
- }))
100
- .mockImplementationOnce(() => null);
77
+ const batchFilePath = getPath("batch.yaml");
78
+ const content = multiline`
79
+ variants:
80
+ - name: foo
81
+ input:
82
+ type: path
83
+ value: badvariant.yaml
84
+ `;
85
+ fs.writeFileSync(batchFilePath, content);
86
+
101
87
  expect(() => {
102
- batchCompile("batch.yaml");
88
+ batchCompile(batchFilePath, {});
103
89
  }).toThrow("process.exit");
104
90
  expect(mockConsoleError).toHaveBeenCalledWith(
105
- `Batch file \"batch.yaml\" does not exist or is not a file.`,
91
+ `Failed to load variant definition for key "foo"`,
106
92
  );
107
93
  expect(mockExit).toHaveBeenCalledWith(1);
108
94
  });
109
95
 
110
96
  it("should exit if sample file not found", () => {
111
- mockIsFile.mockReturnValue(true);
112
- mockGetAbsoluteDirectoryPath.mockReturnValue("/working/directory");
113
- mockParse.mockReturnValueOnce({
114
- variants: [],
115
- samples: [{ templatePath: "sample.yaml", variants: [], output: "out" }],
116
- });
117
- mockResolveSampleFile.mockReturnValue("sample.yaml");
118
- mockFs.existsSync.mockReturnValueOnce(false);
97
+ const batchFilePath = getPath("batch.yaml");
98
+ const content = multiline`
99
+ samples:
100
+ - templatePath: sample.yaml
101
+ variants: []
102
+ output: out
103
+ `;
104
+ fs.writeFileSync(batchFilePath, content);
105
+
119
106
  expect(() => {
120
- batchCompile("batch.yaml");
107
+ batchCompile(batchFilePath, {});
121
108
  }).toThrow("process.exit");
122
109
  expect(mockConsoleError).toHaveBeenCalledWith(
123
- "Failed to parse sample file: sample.yaml",
110
+ `Sample file not found: ${getPath("sample.yaml")}`,
124
111
  );
112
+
125
113
  expect(mockExit).toHaveBeenCalledWith(1);
126
114
  });
127
115
 
128
116
  it("should exit if sample file cannot be parsed", () => {
129
- mockFs.existsSync.mockReturnValue(true);
130
- mockFs.lstatSync.mockReturnValue({ isFile: () => true } as any);
131
- mockParse.mockReturnValueOnce({
132
- variants: [],
133
- samples: [
134
- { templatePath: "sample.yaml", variants: ["v1"], output: "out" },
135
- ],
136
- });
137
- mockResolveSampleFile.mockReturnValue("/path/to/sample.yaml");
138
- mockParse.mockReturnValueOnce(null);
117
+ const batchFilePath = getPath("batch.yaml");
118
+ const batchFileContent = multiline`
119
+ variants:
120
+ - name: foo
121
+ input:
122
+ type: object
123
+ properties:
124
+ var: value
125
+ samples:
126
+ - templatePath: sample.yaml
127
+ variants:
128
+ - output: out
129
+ input: foo
130
+ `;
131
+ fs.writeFileSync(batchFilePath, batchFileContent);
132
+ const sampleFilePath = getPath("sample.yaml");
133
+ const invalidSampleContent = multiline`
134
+ some: invalid: yaml
135
+ `;
136
+ fs.writeFileSync(sampleFilePath, invalidSampleContent);
137
+
139
138
  expect(() => {
140
- batchCompile("batch.yaml");
139
+ batchCompile(batchFilePath, {});
141
140
  }).toThrow("process.exit");
142
141
  expect(mockConsoleError).toHaveBeenCalledWith(
143
- 'Batch file "batch.yaml" does not exist or is not a file.',
142
+ `Failed to parse sample file: ${sampleFilePath}`,
144
143
  );
145
144
  expect(mockExit).toHaveBeenCalledWith(1);
146
145
  });
147
146
 
148
147
  it("should exit if variant cannot be resolved", () => {
149
- mockIsFile.mockReturnValue(true);
150
- mockGetAbsoluteDirectoryPath.mockReturnValue("/working/directory");
151
- mockParse.mockReturnValueOnce({
152
- variants: [],
153
- samples: [
154
- {
155
- templatePath: "sample.yaml",
156
- variants: [{ output: "out", input: "v1" }],
157
- },
158
- ],
159
- });
160
- mockResolveSampleFile.mockReturnValue("/path/to/sample.yaml");
161
- mockParse.mockReturnValueOnce({
162
- template: "t",
163
- type: "js",
164
- dependencies: [],
165
- input: [],
166
- });
167
- mockResolveTemplate.mockReturnValue("resolved template");
168
- mockIsVariantInputReference.mockReturnValue(true);
169
- mockGetVariantInputReferenceValue.mockReturnValue("v1");
170
- mockIsVariantInputDefinition.mockReturnValue(false);
148
+ const batchFilePath = getPath("batch.yaml");
149
+ const batchFileContent = multiline`
150
+ variants:
151
+ - name: foo
152
+ input:
153
+ type: object
154
+ properties:
155
+ var: value
156
+ samples:
157
+ - templatePath: sample.yaml
158
+ variants:
159
+ - output: out
160
+ input: bar
161
+ `;
162
+ fs.writeFileSync(batchFilePath, batchFileContent);
163
+ const sampleFilePath = getPath("sample.yaml");
164
+ const sampleContent = multiline`
165
+ template: sample.js.template
166
+ type: javascript
167
+ dependencies:
168
+ input:
169
+ - name: var
170
+ type: string
171
+ required: true
172
+ `;
173
+ fs.writeFileSync(sampleFilePath, sampleContent);
171
174
  expect(() => {
172
- batchCompile("batch.yaml");
175
+ batchCompile(batchFilePath, {});
173
176
  }).toThrow("process.exit");
174
177
  expect(mockConsoleError).toHaveBeenCalledWith(
175
- `Variant "v1" could not be resolved.`,
178
+ `Variant "bar" could not be resolved.`,
176
179
  );
177
180
  expect(mockExit).toHaveBeenCalledWith(1);
178
181
  });
179
182
 
180
183
  it("should exit if compilation throws error", () => {
181
- mockIsFile.mockReturnValue(true);
182
- mockGetAbsoluteDirectoryPath.mockReturnValue("/working/directory");
183
- mockParse.mockReturnValueOnce({
184
- variants: [],
185
- samples: [
186
- {
187
- templatePath: "sample.yaml",
188
- variants: [{ output: "out", input: "v1" }],
189
- },
190
- ],
191
- });
192
- mockResolveSampleFile.mockReturnValue("/path/to/sample.yaml");
193
- mockParse.mockReturnValueOnce({
194
- template: "t",
195
- type: "js",
196
- dependencies: [],
197
- input: [],
198
- });
199
- mockResolveTemplate.mockReturnValue("resolved template");
200
- mockIsVariantInputDefinition.mockReturnValue(true);
201
184
  mockCompileSample.mockImplementation(() => {
202
185
  throw new Error("Compilation error");
203
186
  });
187
+ const batchFilePath = getPath("batch.yaml");
188
+ const batchFileContent = multiline`
189
+ variants:
190
+ - name: foo
191
+ input:
192
+ type: object
193
+ properties:
194
+ var2: value
195
+ samples:
196
+ - templatePath: sample.yaml
197
+ variants:
198
+ - output: out
199
+ input: foo
200
+ `;
201
+ fs.writeFileSync(batchFilePath, batchFileContent);
202
+ const sampleFilePath = getPath("sample.yaml");
203
+ const sampleContent = multiline`
204
+ template: sample.js.template
205
+ type: javascript
206
+ dependencies:
207
+ input:
208
+ - name: var
209
+ type: string
210
+ required: true
211
+ `;
212
+ fs.writeFileSync(sampleFilePath, sampleContent);
204
213
  expect(() => {
205
- batchCompile("batch.yaml");
214
+ batchCompile(batchFilePath, {});
206
215
  }).toThrow("process.exit");
207
216
  expect(mockConsoleError).toHaveBeenNthCalledWith(
208
217
  1,
@@ -210,37 +219,43 @@ describe("batchCompile", () => {
210
219
  );
211
220
  expect(mockConsoleError).toHaveBeenNthCalledWith(
212
221
  2,
213
- 'Sample: sample.yaml, Variant: {"output":"out","input":"v1"}',
222
+ 'Sample: sample.yaml, Variant: {"output":"out","input":"foo"}',
214
223
  );
215
224
  expect(mockExit).toHaveBeenCalledWith(1);
216
225
  });
217
226
 
218
227
  it("should exit if compilation throws unknown error", () => {
219
- mockIsFile.mockReturnValue(true);
220
- mockGetAbsoluteDirectoryPath.mockReturnValue("/working/directory");
221
- mockParse.mockReturnValueOnce({
222
- variants: [],
223
- samples: [
224
- {
225
- templatePath: "sample.yaml",
226
- variants: [{ output: "out", input: "v1" }],
227
- },
228
- ],
229
- });
230
- mockResolveSampleFile.mockReturnValue("/path/to/sample.yaml");
231
- mockParse.mockReturnValueOnce({
232
- template: "t",
233
- type: "js",
234
- dependencies: [],
235
- input: [],
236
- });
237
- mockResolveTemplate.mockReturnValue("resolved template");
238
- mockIsVariantInputDefinition.mockReturnValue(true);
239
228
  mockCompileSample.mockImplementation(() => {
240
229
  throw "Unknown error";
241
230
  });
231
+ const batchFilePath = getPath("batch.yaml");
232
+ const batchFileContent = multiline`
233
+ variants:
234
+ - name: foo
235
+ input:
236
+ type: object
237
+ properties:
238
+ var2: value
239
+ samples:
240
+ - templatePath: sample.yaml
241
+ variants:
242
+ - output: out
243
+ input: foo
244
+ `;
245
+ fs.writeFileSync(batchFilePath, batchFileContent);
246
+ const sampleFilePath = getPath("sample.yaml");
247
+ const sampleContent = multiline`
248
+ template: sample.js.template
249
+ type: javascript
250
+ dependencies:
251
+ input:
252
+ - name: var
253
+ type: string
254
+ required: true
255
+ `;
256
+ fs.writeFileSync(sampleFilePath, sampleContent);
242
257
  expect(() => {
243
- batchCompile("batch.yaml");
258
+ batchCompile(getPath("batch.yaml"), {});
244
259
  }).toThrow("process.exit");
245
260
  expect(mockConsoleError).toHaveBeenCalledWith(
246
261
  "An unknown error occurred during compilation.",
@@ -249,51 +264,91 @@ describe("batchCompile", () => {
249
264
  });
250
265
 
251
266
  it("should compile and write output files for each variant", () => {
252
- mockIsFile.mockReturnValue(true);
253
- mockGetAbsoluteDirectoryPath.mockReturnValue("/working/directory");
254
- mockParse.mockReturnValueOnce({
255
- variants: [],
256
- samples: [
257
- {
258
- templatePath: "sample.yaml",
259
- variants: [
260
- { output: "v1.output", input: {} },
261
- { output: "v2.output", input: {} },
262
- ],
263
- output: "out",
264
- },
265
- ],
266
- });
267
- mockResolveSampleFile.mockReturnValue("/path/to/sample.yaml");
268
- mockParse.mockReturnValueOnce({
269
- template: "t",
270
- type: "js",
271
- dependencies: [],
272
- input: [],
273
- });
274
- mockResolveTemplate.mockReturnValue("resolved template");
275
- mockIsVariantInputDefinition.mockReturnValue(true);
276
267
  mockCompileSample.mockReturnValue({
277
268
  items: [
278
269
  { fileName: "file1.js", content: "console.log('1');" },
279
270
  { fileName: "file2.js", content: "console.log('2');" },
280
271
  ],
281
272
  });
282
- mockCreateOutputDirectory.mockImplementation(() => {});
283
- mockFs.writeFileSync.mockImplementation(() => {});
284
- batchCompile("batch.yaml");
285
- expect(mockCreateOutputDirectory).toHaveBeenCalledWith(
286
- "/working/directory/v1.output",
273
+ const batchFilePath = getPath("batch.yaml");
274
+ const batchFileContent = multiline`
275
+ variants:
276
+ - name: foo
277
+ input:
278
+ type: object
279
+ properties:
280
+ var2: value
281
+ samples:
282
+ - templatePath: sample.yaml
283
+ variants:
284
+ - output: out
285
+ input: foo
286
+ `;
287
+ fs.writeFileSync(batchFilePath, batchFileContent);
288
+ const sampleFilePath = getPath("sample.yaml");
289
+ const sampleContent = multiline`
290
+ template: sample.js.template
291
+ type: javascript
292
+ dependencies:
293
+ input:
294
+ - name: var
295
+ type: string
296
+ required: true
297
+ `;
298
+ fs.writeFileSync(sampleFilePath, sampleContent);
299
+ batchCompile(batchFilePath, {});
300
+
301
+ expect(fs.existsSync(getPath("out/file1.js"))).toBe(true);
302
+ expect(fs.readFileSync(getPath("out/file1.js"), "utf-8")).toBe(
303
+ "console.log('1');",
287
304
  );
288
- expect(mockCreateOutputDirectory).toHaveBeenCalledWith(
289
- "/working/directory/v2.output",
305
+ expect(fs.existsSync(getPath("out/file2.js"))).toBe(true);
306
+ expect(fs.readFileSync(getPath("out/file2.js"), "utf-8")).toBe(
307
+ "console.log('2');",
290
308
  );
291
- expect(mockFs.writeFileSync).toHaveBeenCalledWith(
292
- path.join("/working/directory", "v1.output", "file1.js"),
309
+ });
310
+
311
+ it("should compile and write output files for each variant", () => {
312
+ mockCompileSample.mockReturnValue({
313
+ items: [
314
+ { fileName: "file1.js", content: "console.log('1');" },
315
+ { fileName: "file2.js", content: "console.log('2');" },
316
+ ],
317
+ });
318
+ const batchFilePath = getPath("batch.yaml");
319
+ const batchFileContent = multiline`
320
+ variants:
321
+ - name: foo
322
+ input:
323
+ type: object
324
+ properties:
325
+ var2: value
326
+ samples:
327
+ - templatePath: sample.yaml
328
+ variants:
329
+ - output: out
330
+ input: foo
331
+ `;
332
+ fs.writeFileSync(batchFilePath, batchFileContent);
333
+ const sampleFilePath = getPath("sample.yaml");
334
+ const sampleContent = multiline`
335
+ template: sample.js.template
336
+ type: javascript
337
+ dependencies:
338
+ input:
339
+ - name: var
340
+ type: string
341
+ required: true
342
+ `;
343
+ fs.writeFileSync(sampleFilePath, sampleContent);
344
+ batchCompile(batchFilePath, { outputDir: getPath("other") });
345
+
346
+ expect(fs.existsSync(getPath("other/out/file1.js"))).toBe(true);
347
+ expect(fs.readFileSync(getPath("other/out/file1.js"), "utf-8")).toBe(
293
348
  "console.log('1');",
294
349
  );
295
- expect(mockFs.writeFileSync).toHaveBeenCalledWith(
296
- path.join("/working/directory", "v1.output", "file2.js"),
350
+ expect(fs.existsSync(getPath("other/out/file2.js"))).toBe(true);
351
+ expect(fs.readFileSync(getPath("other/out/file2.js"), "utf-8")).toBe(
297
352
  "console.log('2');",
298
353
  );
299
354
  });
@@ -12,7 +12,7 @@ import {
12
12
  } from "../src/utils";
13
13
  import { Sample } from "@caleuche/core";
14
14
 
15
- function multiline(strings: TemplateStringsArray, ...values: any[]) {
15
+ export function multiline(strings: TemplateStringsArray, ...values: any[]) {
16
16
  let result = strings[0];
17
17
  for (let i = 0; i < values.length; i++) {
18
18
  result += values[i] + strings[i + 1];