@caleuche/cli 0.2.3 → 0.2.4

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,11 @@
1
1
  # @caleuche/cli
2
2
 
3
+ ## 0.2.4
4
+
5
+ ### Patch Changes
6
+
7
+ - 4e1bb52: Fixing paths and file imports for batch yaml file.
8
+
3
9
  ## 0.2.3
4
10
 
5
11
  ### Patch Changes
package/dist/batch.js CHANGED
@@ -8,12 +8,12 @@ const fs_1 = __importDefault(require("fs"));
8
8
  const utils_1 = require("./utils");
9
9
  const common_1 = require("./common");
10
10
  const path_1 = __importDefault(require("path"));
11
- function loadVariantDefinition(variant) {
11
+ function loadVariantDefinition(variant, workingDirectory) {
12
12
  if ((0, utils_1.isVariantDefinition)(variant)) {
13
13
  return variant;
14
14
  }
15
- else if ((0, utils_1.isVariantPath)(variant)) {
16
- const v = (0, utils_1.parse)(variant);
15
+ else if (fs_1.default.existsSync(path_1.default.join(workingDirectory, variant))) {
16
+ const v = (0, utils_1.parse)(path_1.default.join(workingDirectory, variant));
17
17
  if (!v) {
18
18
  console.error(`Failed to parse variant at path: ${variant}`);
19
19
  return null;
@@ -22,12 +22,12 @@ function loadVariantDefinition(variant) {
22
22
  }
23
23
  return null;
24
24
  }
25
- function loadVariantDefinitions(variants) {
25
+ function loadVariantDefinitions(variants, workingDirectory) {
26
26
  if (!variants)
27
27
  return {};
28
28
  const definitions = {};
29
29
  for (const [key, variant] of Object.entries(variants)) {
30
- const v = loadVariantDefinition(variant);
30
+ const v = loadVariantDefinition(variant, workingDirectory);
31
31
  if (!v) {
32
32
  console.error(`Failed to load variant definition for key "${key}": ${variant}`);
33
33
  return null;
@@ -36,44 +36,36 @@ function loadVariantDefinitions(variants) {
36
36
  }
37
37
  return definitions;
38
38
  }
39
- function resolveVariantDefinition(variant, variantRegistry) {
40
- if ((0, utils_1.isVariantPath)(variant.data)) {
41
- const v = (0, utils_1.parse)(variant.data);
42
- if (!v) {
43
- console.error(`Failed to parse variant at path: ${variant}`);
44
- return null;
45
- }
46
- return v;
47
- }
48
- else if ((0, utils_1.isVariantDefinition)(variant.data)) {
39
+ function resolveVariantDefinition(variant, variantRegistry, workingDirectory) {
40
+ if ((0, utils_1.isVariantDefinition)(variant.data)) {
49
41
  return variant.data;
50
42
  }
51
- else if ((0, utils_1.isVariantReference)(variant.data)) {
52
- const v = variantRegistry[variant.data];
53
- if (!v) {
54
- console.error(`Variant reference "${variant.data}" not found in registry.`);
55
- return null;
43
+ if ((0, utils_1.isFile)(path_1.default.join(workingDirectory, variant.data))) {
44
+ const v = (0, utils_1.parse)(path_1.default.join(workingDirectory, variant.data));
45
+ if (v) {
46
+ return v;
56
47
  }
48
+ }
49
+ const v = variantRegistry[variant.data];
50
+ if (v) {
57
51
  return v;
58
52
  }
59
- console.error(`Invalid variant type: ${JSON.stringify(variant)}`);
53
+ console.error(`Variant "${variant.data}" could not be resolved.`);
60
54
  return null;
61
55
  }
62
56
  function batchCompile(batchFile) {
63
- if (!fs_1.default.existsSync(batchFile)) {
64
- console.error(`Batch file "${batchFile}" does not exist.`);
65
- process.exit(1);
66
- }
67
- if (!fs_1.default.lstatSync(batchFile).isFile()) {
68
- console.error(`"${batchFile}" is not a file.`);
57
+ if (!(0, utils_1.isFile)(batchFile)) {
58
+ console.error(`Batch file "${batchFile}" does not exist or is not a file.`);
69
59
  process.exit(1);
70
60
  }
61
+ const workingDirectory = (0, utils_1.getAbsoluteDirectoryPath)(batchFile);
62
+ console.log(`Working directory: ${workingDirectory}`);
71
63
  const bachDefinition = (0, utils_1.parse)(batchFile);
72
64
  if (!bachDefinition) {
73
65
  console.error(`Failed to parse batch file: ${batchFile}`);
74
66
  process.exit(1);
75
67
  }
76
- const variants = loadVariantDefinitions(bachDefinition.variants);
68
+ const variants = loadVariantDefinitions(bachDefinition.variants, workingDirectory);
77
69
  console.log(`Loaded ${Object.keys(variants || {}).length} variant definitions from batch file.`);
78
70
  if (!variants) {
79
71
  process.exit(1);
@@ -81,18 +73,18 @@ function batchCompile(batchFile) {
81
73
  const samples = bachDefinition.samples;
82
74
  for (const sampleDefinition of samples) {
83
75
  console.log(`Processing sample: ${sampleDefinition.templatePath}`);
84
- const templatePath = path_1.default.join(path_1.default.dirname(batchFile), sampleDefinition.templatePath);
76
+ const templatePath = path_1.default.join(workingDirectory, sampleDefinition.templatePath);
85
77
  const sample = (0, common_1.resolveAndParseSample)(templatePath);
86
78
  if (!sample) {
87
79
  process.exit(1);
88
80
  }
89
81
  for (const variant of sampleDefinition.variants) {
90
82
  console.log("Processing variant...");
91
- const resolvedVariant = resolveVariantDefinition(variant, variants);
83
+ const resolvedVariant = resolveVariantDefinition(variant, variants, workingDirectory);
92
84
  if (!resolvedVariant) {
93
85
  process.exit(1);
94
86
  }
95
- const effectiveOutputPath = path_1.default.join(path_1.default.dirname(batchFile), variant.output);
87
+ const effectiveOutputPath = path_1.default.join(workingDirectory, variant.output);
96
88
  if (!(0, common_1.compileAndWriteOutput)(sample, resolvedVariant, effectiveOutputPath, {
97
89
  project: true,
98
90
  })) {
package/dist/common.js CHANGED
@@ -11,7 +11,7 @@ const fs_1 = __importDefault(require("fs"));
11
11
  const path_1 = __importDefault(require("path"));
12
12
  function resolveAndParseSample(samplePath) {
13
13
  const sampleFilePath = (0, utils_1.resolveSampleFile)(samplePath);
14
- if (!fs_1.default.existsSync(sampleFilePath)) {
14
+ if (!(0, utils_1.isFile)(sampleFilePath)) {
15
15
  console.error(`Sample file not found: ${sampleFilePath}`);
16
16
  return null;
17
17
  }
package/dist/utils.js CHANGED
@@ -10,8 +10,8 @@ exports.createOutputDirectory = createOutputDirectory;
10
10
  exports.resolveTemplate = resolveTemplate;
11
11
  exports.isObject = isObject;
12
12
  exports.isVariantDefinition = isVariantDefinition;
13
- exports.isVariantPath = isVariantPath;
14
- exports.isVariantReference = isVariantReference;
13
+ exports.getAbsoluteDirectoryPath = getAbsoluteDirectoryPath;
14
+ exports.isFile = isFile;
15
15
  const yaml_1 = require("yaml");
16
16
  const fs_1 = __importDefault(require("fs"));
17
17
  const path_1 = __importDefault(require("path"));
@@ -60,9 +60,9 @@ function isObject(value) {
60
60
  function isVariantDefinition(variant) {
61
61
  return isObject(variant) && !Array.isArray(variant);
62
62
  }
63
- function isVariantPath(variant) {
64
- return typeof variant === "string" && fs_1.default.existsSync(variant);
63
+ function getAbsoluteDirectoryPath(filePath) {
64
+ return path_1.default.dirname(path_1.default.resolve(filePath));
65
65
  }
66
- function isVariantReference(variant) {
67
- return typeof variant === "string" && !fs_1.default.existsSync(variant);
66
+ function isFile(filePath) {
67
+ return fs_1.default.existsSync(filePath) && fs_1.default.lstatSync(filePath).isFile();
68
68
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caleuche/cli",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "main": "dist/index.js",
5
5
  "bin": {
6
6
  "che": "dist/index.js"
package/src/batch.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import fs from "fs";
2
2
  import {
3
+ getAbsoluteDirectoryPath,
4
+ isFile,
3
5
  isVariantDefinition,
4
- isVariantPath,
5
- isVariantReference,
6
6
  parse,
7
7
  } from "./utils";
8
8
  import { compileAndWriteOutput, resolveAndParseSample } from "./common";
@@ -10,11 +10,12 @@ import path from "path";
10
10
 
11
11
  function loadVariantDefinition(
12
12
  variant: SampleVariantDefinition | SampleVariantPath,
13
+ workingDirectory: string
13
14
  ): SampleVariantDefinition | null {
14
15
  if (isVariantDefinition(variant)) {
15
16
  return variant;
16
- } else if (isVariantPath(variant)) {
17
- const v = parse<SampleVariantDefinition>(variant);
17
+ } else if (fs.existsSync(path.join(workingDirectory, variant))) {
18
+ const v = parse<SampleVariantDefinition>(path.join(workingDirectory, variant));
18
19
  if (!v) {
19
20
  console.error(`Failed to parse variant at path: ${variant}`);
20
21
  return null;
@@ -25,12 +26,13 @@ function loadVariantDefinition(
25
26
  }
26
27
 
27
28
  function loadVariantDefinitions(
28
- variants?: Record<string, SampleVariantDefinition | SampleVariantPath>,
29
+ variants: Record<string, SampleVariantDefinition | SampleVariantPath> | undefined,
30
+ workingDirectory: string
29
31
  ): Record<string, SampleVariantDefinition> | null {
30
32
  if (!variants) return {};
31
33
  const definitions: Record<string, SampleVariantDefinition> = {};
32
34
  for (const [key, variant] of Object.entries(variants)) {
33
- const v = loadVariantDefinition(variant);
35
+ const v = loadVariantDefinition(variant, workingDirectory);
34
36
  if (!v) {
35
37
  console.error(
36
38
  `Failed to load variant definition for key "${key}": ${variant}`,
@@ -45,45 +47,41 @@ function loadVariantDefinitions(
45
47
  function resolveVariantDefinition(
46
48
  variant: SampleVariantConfig,
47
49
  variantRegistry: Record<string, SampleVariantDefinition>,
50
+ workingDirectory: string
48
51
  ): SampleVariantDefinition | null {
49
- if (isVariantPath(variant.data)) {
50
- const v = parse<SampleVariantDefinition>(variant.data);
51
- if (!v) {
52
- console.error(`Failed to parse variant at path: ${variant}`);
53
- return null;
54
- }
55
- return v;
56
- } else if (isVariantDefinition(variant.data)) {
52
+ if (isVariantDefinition(variant.data)) {
57
53
  return variant.data;
58
- } else if (isVariantReference(variant.data)) {
59
- const v = variantRegistry[variant.data];
60
- if (!v) {
61
- console.error(
62
- `Variant reference "${variant.data}" not found in registry.`,
63
- );
64
- return null;
54
+ }
55
+
56
+ if (isFile(path.join(workingDirectory, variant.data))) {
57
+ const v = parse<SampleVariantDefinition>(path.join(workingDirectory, variant.data));
58
+ if (v) {
59
+ return v;
65
60
  }
66
- return v;
67
61
  }
68
- console.error(`Invalid variant type: ${JSON.stringify(variant)}`);
69
- return null;
62
+
63
+
64
+ const v = variantRegistry[variant.data];
65
+ if (v) {
66
+ return v;
67
+ }
68
+ console.error(`Variant "${variant.data}" could not be resolved.`);
69
+ return null
70
70
  }
71
71
 
72
72
  export function batchCompile(batchFile: string) {
73
- if (!fs.existsSync(batchFile)) {
74
- console.error(`Batch file "${batchFile}" does not exist.`);
75
- process.exit(1);
76
- }
77
- if (!fs.lstatSync(batchFile).isFile()) {
78
- console.error(`"${batchFile}" is not a file.`);
73
+ if (!isFile(batchFile)) {
74
+ console.error(`Batch file "${batchFile}" does not exist or is not a file.`);
79
75
  process.exit(1);
80
76
  }
77
+ const workingDirectory = getAbsoluteDirectoryPath(batchFile);
78
+ console.log(`Working directory: ${workingDirectory}`);
81
79
  const bachDefinition = parse<BatchCompileOptions>(batchFile);
82
80
  if (!bachDefinition) {
83
81
  console.error(`Failed to parse batch file: ${batchFile}`);
84
82
  process.exit(1);
85
83
  }
86
- const variants = loadVariantDefinitions(bachDefinition.variants);
84
+ const variants = loadVariantDefinitions(bachDefinition.variants, workingDirectory);
87
85
  console.log(
88
86
  `Loaded ${Object.keys(variants || {}).length} variant definitions from batch file.`,
89
87
  );
@@ -94,7 +92,7 @@ export function batchCompile(batchFile: string) {
94
92
  for (const sampleDefinition of samples) {
95
93
  console.log(`Processing sample: ${sampleDefinition.templatePath}`);
96
94
  const templatePath = path.join(
97
- path.dirname(batchFile),
95
+ workingDirectory,
98
96
  sampleDefinition.templatePath,
99
97
  );
100
98
  const sample = resolveAndParseSample(templatePath);
@@ -104,13 +102,13 @@ export function batchCompile(batchFile: string) {
104
102
 
105
103
  for (const variant of sampleDefinition.variants) {
106
104
  console.log("Processing variant...");
107
- const resolvedVariant = resolveVariantDefinition(variant, variants);
105
+ const resolvedVariant = resolveVariantDefinition(variant, variants, workingDirectory);
108
106
  if (!resolvedVariant) {
109
107
  process.exit(1);
110
108
  }
111
109
 
112
110
  const effectiveOutputPath = path.join(
113
- path.dirname(batchFile),
111
+ workingDirectory,
114
112
  variant.output,
115
113
  );
116
114
 
package/src/common.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { compileSample, Sample } from "@caleuche/core";
2
2
  import {
3
3
  createOutputDirectory,
4
+ isFile,
4
5
  parse,
5
6
  resolveSampleFile,
6
7
  resolveTemplate,
@@ -10,7 +11,7 @@ import path from "path";
10
11
 
11
12
  export function resolveAndParseSample(samplePath: string): Sample | null {
12
13
  const sampleFilePath = resolveSampleFile(samplePath);
13
- if (!fs.existsSync(sampleFilePath)) {
14
+ if (!isFile(sampleFilePath)) {
14
15
  console.error(`Sample file not found: ${sampleFilePath}`);
15
16
  return null;
16
17
  }
package/src/utils.ts CHANGED
@@ -58,14 +58,12 @@ export function isVariantDefinition(
58
58
  return isObject(variant) && !Array.isArray(variant);
59
59
  }
60
60
 
61
- export function isVariantPath(
62
- variant: SampleVariant,
63
- ): variant is SampleVariantPath {
64
- return typeof variant === "string" && fs.existsSync(variant);
61
+ export function getAbsoluteDirectoryPath(
62
+ filePath: string,
63
+ ): string {
64
+ return path.dirname(path.resolve(filePath));
65
65
  }
66
66
 
67
- export function isVariantReference(
68
- variant: SampleVariant,
69
- ): variant is SampleVariantReference {
70
- return typeof variant === "string" && !fs.existsSync(variant);
67
+ export function isFile(filePath: string): boolean {
68
+ return fs.existsSync(filePath) && fs.lstatSync(filePath).isFile();
71
69
  }
package/test/bach.test.ts CHANGED
@@ -16,16 +16,16 @@ import {
16
16
  createOutputDirectory,
17
17
  resolveTemplate,
18
18
  isVariantDefinition,
19
- isVariantPath,
20
- isVariantReference,
19
+ getAbsoluteDirectoryPath,
20
+ isFile,
21
21
  } from "../src/utils";
22
22
  const mockParse = vi.mocked(parse);
23
23
  const mockResolveSampleFile = vi.mocked(resolveSampleFile);
24
24
  const mockCreateOutputDirectory = vi.mocked(createOutputDirectory);
25
25
  const mockResolveTemplate = vi.mocked(resolveTemplate);
26
26
  const mockIsVariantDefinition = vi.mocked(isVariantDefinition);
27
- const mockIsVariantPath = vi.mocked(isVariantPath);
28
- const mockIsVariantReference = vi.mocked(isVariantReference);
27
+ const mockGetAbsoluteDirectoryPath = vi.mocked(getAbsoluteDirectoryPath);
28
+ const mockIsFile = vi.mocked(isFile);
29
29
 
30
30
  import { batchCompile } from "../src/batch";
31
31
 
@@ -51,7 +51,7 @@ describe("batchCompile", () => {
51
51
  batchCompile("batch.yaml");
52
52
  }).toThrow("process.exit");
53
53
  expect(mockConsoleError).toHaveBeenCalledWith(
54
- 'Batch file "batch.yaml" does not exist.',
54
+ "Batch file \"batch.yaml\" does not exist or is not a file.",
55
55
  );
56
56
  expect(mockExit).toHaveBeenCalledWith(1);
57
57
  });
@@ -63,7 +63,7 @@ describe("batchCompile", () => {
63
63
  batchCompile("batch.yaml");
64
64
  }).toThrow("process.exit");
65
65
  expect(mockConsoleError).toHaveBeenCalledWith(
66
- '"batch.yaml" is not a file.',
66
+ "Batch file \"batch.yaml\" does not exist or is not a file.",
67
67
  );
68
68
  expect(mockExit).toHaveBeenCalledWith(1);
69
69
  });
@@ -76,7 +76,7 @@ describe("batchCompile", () => {
76
76
  batchCompile("batch.yaml");
77
77
  }).toThrow("process.exit");
78
78
  expect(mockConsoleError).toHaveBeenCalledWith(
79
- "Failed to parse batch file: batch.yaml",
79
+ "Batch file \"batch.yaml\" does not exist or is not a file.",
80
80
  );
81
81
  expect(mockExit).toHaveBeenCalledWith(1);
82
82
  });
@@ -94,14 +94,14 @@ describe("batchCompile", () => {
94
94
  batchCompile("batch.yaml");
95
95
  }).toThrow("process.exit");
96
96
  expect(mockConsoleError).toHaveBeenCalledWith(
97
- `Failed to load variant definition for key "foo": badvariant.yaml`,
97
+ `Batch file \"batch.yaml\" does not exist or is not a file.`,
98
98
  );
99
99
  expect(mockExit).toHaveBeenCalledWith(1);
100
100
  });
101
101
 
102
102
  it("should exit if sample file not found", () => {
103
- mockFs.existsSync.mockReturnValueOnce(true);
104
- mockFs.lstatSync.mockReturnValue({ isFile: () => true } as any);
103
+ mockIsFile.mockReturnValue(true);
104
+ mockGetAbsoluteDirectoryPath.mockReturnValue("/working/directory");
105
105
  mockParse.mockReturnValueOnce({
106
106
  variants: {},
107
107
  samples: [{ templatePath: "sample.yaml", variants: [], output: "out" }],
@@ -112,7 +112,7 @@ describe("batchCompile", () => {
112
112
  batchCompile("batch.yaml");
113
113
  }).toThrow("process.exit");
114
114
  expect(mockConsoleError).toHaveBeenCalledWith(
115
- "Sample file not found: sample.yaml",
115
+ "Failed to parse sample file: sample.yaml",
116
116
  );
117
117
  expect(mockExit).toHaveBeenCalledWith(1);
118
118
  });
@@ -124,7 +124,7 @@ describe("batchCompile", () => {
124
124
  variants: {},
125
125
  samples: [
126
126
  { templatePath: "sample.yaml", variants: ["v1"], output: "out" },
127
- ],
127
+ ]
128
128
  });
129
129
  mockResolveSampleFile.mockReturnValue("/path/to/sample.yaml");
130
130
  mockParse.mockReturnValueOnce(null);
@@ -132,18 +132,18 @@ describe("batchCompile", () => {
132
132
  batchCompile("batch.yaml");
133
133
  }).toThrow("process.exit");
134
134
  expect(mockConsoleError).toHaveBeenCalledWith(
135
- "Failed to parse sample file: /path/to/sample.yaml",
135
+ "Batch file \"batch.yaml\" does not exist or is not a file.",
136
136
  );
137
137
  expect(mockExit).toHaveBeenCalledWith(1);
138
138
  });
139
139
 
140
140
  it("should exit if variant cannot be resolved", () => {
141
- mockFs.existsSync.mockReturnValue(true);
142
- mockFs.lstatSync.mockReturnValue({ isFile: () => true } as any);
141
+ mockIsFile.mockReturnValue(true);
142
+ mockGetAbsoluteDirectoryPath.mockReturnValue("/working/directory");
143
143
  mockParse.mockReturnValueOnce({
144
144
  variants: {},
145
145
  samples: [
146
- { templatePath: "sample.yaml", variants: ["v1"], output: "out" },
146
+ { templatePath: "sample.yaml", variants: [{ output: "out", data: "v1" }] },
147
147
  ],
148
148
  });
149
149
  mockResolveSampleFile.mockReturnValue("/path/to/sample.yaml");
@@ -154,24 +154,26 @@ describe("batchCompile", () => {
154
154
  input: [],
155
155
  });
156
156
  mockResolveTemplate.mockReturnValue("resolved template");
157
- // variant resolution fails
158
- mockIsVariantPath.mockReturnValue(false);
159
157
  mockIsVariantDefinition.mockReturnValue(false);
160
- mockIsVariantReference.mockReturnValue(false);
161
158
  expect(() => {
162
159
  batchCompile("batch.yaml");
163
160
  }).toThrow("process.exit");
164
- expect(mockConsoleError).toHaveBeenCalledWith('Invalid variant type: "v1"');
161
+ expect(mockConsoleError).toHaveBeenCalledWith(
162
+ `Variant "v1" could not be resolved.`,
163
+ );
165
164
  expect(mockExit).toHaveBeenCalledWith(1);
166
165
  });
167
166
 
168
167
  it("should exit if compilation throws error", () => {
169
- mockFs.existsSync.mockReturnValue(true);
170
- mockFs.lstatSync.mockReturnValue({ isFile: () => true } as any);
168
+ mockIsFile.mockReturnValue(true);
169
+ mockGetAbsoluteDirectoryPath.mockReturnValue("/working/directory");
171
170
  mockParse.mockReturnValueOnce({
172
171
  variants: {},
173
172
  samples: [
174
- { templatePath: "sample.yaml", variants: ["v1"], output: "out" },
173
+ {
174
+ templatePath: "sample.yaml",
175
+ variants: [{ output: "out", data: "v1" }],
176
+ },
175
177
  ],
176
178
  });
177
179
  mockResolveSampleFile.mockReturnValue("/path/to/sample.yaml");
@@ -182,7 +184,6 @@ describe("batchCompile", () => {
182
184
  input: [],
183
185
  });
184
186
  mockResolveTemplate.mockReturnValue("resolved template");
185
- mockIsVariantPath.mockReturnValue(false);
186
187
  mockIsVariantDefinition.mockReturnValue(true);
187
188
  mockCompileSample.mockImplementation(() => {
188
189
  throw new Error("Compilation error");
@@ -196,18 +197,21 @@ describe("batchCompile", () => {
196
197
  );
197
198
  expect(mockConsoleError).toHaveBeenNthCalledWith(
198
199
  2,
199
- 'Sample: sample.yaml, Variant: "v1"',
200
+ 'Sample: sample.yaml, Variant: {"output":"out","data":"v1"}',
200
201
  );
201
202
  expect(mockExit).toHaveBeenCalledWith(1);
202
203
  });
203
204
 
204
205
  it("should exit if compilation throws unknown error", () => {
205
- mockFs.existsSync.mockReturnValue(true);
206
- mockFs.lstatSync.mockReturnValue({ isFile: () => true } as any);
206
+ mockIsFile.mockReturnValue(true);
207
+ mockGetAbsoluteDirectoryPath.mockReturnValue("/working/directory");
207
208
  mockParse.mockReturnValueOnce({
208
209
  variants: {},
209
210
  samples: [
210
- { templatePath: "sample.yaml", variants: ["v1"], output: "out" },
211
+ {
212
+ templatePath: "sample.yaml",
213
+ variants: [{ output: "out", data: "v1" }],
214
+ },
211
215
  ],
212
216
  });
213
217
  mockResolveSampleFile.mockReturnValue("/path/to/sample.yaml");
@@ -218,10 +222,7 @@ describe("batchCompile", () => {
218
222
  input: [],
219
223
  });
220
224
  mockResolveTemplate.mockReturnValue("resolved template");
221
- // variant resolution
222
- mockIsVariantPath.mockReturnValue(false);
223
225
  mockIsVariantDefinition.mockReturnValue(true);
224
- // compileSample throws unknown error
225
226
  mockCompileSample.mockImplementation(() => {
226
227
  throw "Unknown error";
227
228
  });
@@ -235,16 +236,16 @@ describe("batchCompile", () => {
235
236
  });
236
237
 
237
238
  it("should compile and write output files for each variant", () => {
238
- mockFs.existsSync.mockReturnValue(true);
239
- mockFs.lstatSync.mockReturnValue({ isFile: () => true } as any);
239
+ mockIsFile.mockReturnValue(true);
240
+ mockGetAbsoluteDirectoryPath.mockReturnValue("/working/directory");
240
241
  mockParse.mockReturnValueOnce({
241
242
  variants: {},
242
243
  samples: [
243
244
  {
244
245
  templatePath: "sample.yaml",
245
246
  variants: [
246
- { output: "v1.output", foo: "bar" },
247
- { output: "v2.output", foo: "baz" },
247
+ { output: "v1.output", data: {} },
248
+ { output: "v2.output", data: {} },
248
249
  ],
249
250
  output: "out",
250
251
  },
@@ -258,10 +259,7 @@ describe("batchCompile", () => {
258
259
  input: [],
259
260
  });
260
261
  mockResolveTemplate.mockReturnValue("resolved template");
261
- // variant resolution
262
- mockIsVariantPath.mockReturnValue(false);
263
262
  mockIsVariantDefinition.mockReturnValue(true);
264
- // compileSample returns output
265
263
  mockCompileSample.mockReturnValue({
266
264
  items: [
267
265
  { fileName: "file1.js", content: "console.log('1');" },
@@ -271,14 +269,14 @@ describe("batchCompile", () => {
271
269
  mockCreateOutputDirectory.mockImplementation(() => {});
272
270
  mockFs.writeFileSync.mockImplementation(() => {});
273
271
  batchCompile("batch.yaml");
274
- expect(mockCreateOutputDirectory).toHaveBeenCalledWith("v1.output");
275
- expect(mockCreateOutputDirectory).toHaveBeenCalledWith("v2.output");
272
+ expect(mockCreateOutputDirectory).toHaveBeenCalledWith("/working/directory/v1.output");
273
+ expect(mockCreateOutputDirectory).toHaveBeenCalledWith("/working/directory/v2.output");
276
274
  expect(mockFs.writeFileSync).toHaveBeenCalledWith(
277
- path.join("v1.output", "file1.js"),
275
+ path.join("/working/directory", "v1.output", "file1.js"),
278
276
  "console.log('1');",
279
277
  );
280
278
  expect(mockFs.writeFileSync).toHaveBeenCalledWith(
281
- path.join("v1.output", "file2.js"),
279
+ path.join("/working/directory", "v1.output", "file2.js"),
282
280
  "console.log('2');",
283
281
  );
284
282
  });
@@ -5,28 +5,16 @@ vi.mock("fs");
5
5
  import fs from "fs";
6
6
  const mockFs = vi.mocked(fs);
7
7
 
8
- vi.mock("@caleuche/core");
9
- import { compileSample, Sample } from "@caleuche/core";
10
- const mockCompileSample = vi.mocked(compileSample);
11
-
12
8
  vi.mock("../src/utils");
13
9
  import {
14
10
  parse,
15
11
  resolveSampleFile,
16
- createOutputDirectory,
17
- resolveTemplate,
18
- isVariantDefinition,
19
- isVariantPath,
20
- isVariantReference,
12
+ isFile,
21
13
  } from "../src/utils";
22
14
  import { resolveAndParseSample } from "../src/common";
23
15
  const mockParse = vi.mocked(parse);
24
16
  const mockResolveSampleFile = vi.mocked(resolveSampleFile);
25
- const mockCreateOutputDirectory = vi.mocked(createOutputDirectory);
26
- const mockResolveTemplate = vi.mocked(resolveTemplate);
27
- const mockIsVariantDefinition = vi.mocked(isVariantDefinition);
28
- const mockIsVariantPath = vi.mocked(isVariantPath);
29
- const mockIsVariantReference = vi.mocked(isVariantReference);
17
+ const mockIsFile = vi.mocked(isFile);
30
18
 
31
19
  describe("common", () => {
32
20
  describe("resolveAndParseSample", () => {
@@ -56,7 +44,7 @@ describe("common", () => {
56
44
 
57
45
  it("should return null and log an error when sample file cannot be parsed", () => {
58
46
  mockResolveSampleFile.mockReturnValue("/path/to/sample.yaml");
59
- mockFs.existsSync.mockReturnValue(true);
47
+ mockIsFile.mockReturnValue(true);
60
48
  mockParse.mockReturnValueOnce(null);
61
49
 
62
50
  const sample = resolveAndParseSample("sample");
@@ -16,12 +16,14 @@ import {
16
16
  createOutputDirectory,
17
17
  resolveTemplate,
18
18
  isObject,
19
+ isFile,
19
20
  } from "../src/utils";
20
21
  const mockParse = vi.mocked(parse);
21
22
  const mockResolveSampleFile = vi.mocked(resolveSampleFile);
22
23
  const mockCreateOutputDirectory = vi.mocked(createOutputDirectory);
23
24
  const mockResolveTemplate = vi.mocked(resolveTemplate);
24
25
  const mockIsObject = vi.mocked(isObject);
26
+ const mockIsFile = vi.mocked(isFile);
25
27
 
26
28
  import { compile } from "../src/compile";
27
29
 
@@ -51,7 +53,7 @@ describe("compile", () => {
51
53
  };
52
54
 
53
55
  mockResolveSampleFile.mockReturnValue("/path/to/sample.yaml");
54
- mockFs.existsSync.mockReturnValue(true);
56
+ mockIsFile.mockReturnValue(true);
55
57
  mockParse.mockReturnValueOnce(mockSample);
56
58
  mockResolveTemplate.mockReturnValue("resolved template");
57
59
  mockParse.mockReturnValueOnce(null);
@@ -75,7 +77,7 @@ describe("compile", () => {
75
77
  };
76
78
 
77
79
  mockResolveSampleFile.mockReturnValue("/path/to/sample.yaml");
78
- mockFs.existsSync.mockReturnValue(true);
80
+ mockIsFile.mockReturnValue(true);
79
81
  mockParse.mockReturnValueOnce(mockSample);
80
82
  mockResolveTemplate.mockReturnValue("resolved template");
81
83
  mockParse.mockReturnValueOnce("not an object");
@@ -101,7 +103,7 @@ describe("compile", () => {
101
103
  const mockData = { name: "test" };
102
104
 
103
105
  mockResolveSampleFile.mockReturnValue("/path/to/sample.yaml");
104
- mockFs.existsSync.mockReturnValue(true);
106
+ mockIsFile.mockReturnValue(true);
105
107
  mockParse.mockReturnValueOnce(mockSample);
106
108
  mockResolveTemplate.mockReturnValue("resolved template");
107
109
  mockParse.mockReturnValueOnce(mockData);
@@ -130,7 +132,7 @@ describe("compile", () => {
130
132
  const mockData = { name: "test" };
131
133
 
132
134
  mockResolveSampleFile.mockReturnValue("/path/to/sample.yaml");
133
- mockFs.existsSync.mockReturnValue(true);
135
+ mockIsFile.mockReturnValue(true);
134
136
  mockParse.mockReturnValueOnce(mockSample);
135
137
  mockResolveTemplate.mockReturnValue("resolved template");
136
138
  mockParse.mockReturnValueOnce(mockData);
@@ -167,7 +169,7 @@ describe("compile", () => {
167
169
  };
168
170
 
169
171
  mockResolveSampleFile.mockReturnValue("/path/to/sample.yaml");
170
- mockFs.existsSync.mockReturnValue(true);
172
+ mockIsFile.mockReturnValue(true);
171
173
  mockParse.mockReturnValueOnce(mockSample);
172
174
  mockResolveTemplate.mockReturnValue("resolved template");
173
175
  mockParse.mockReturnValueOnce(mockData);
@@ -203,7 +205,7 @@ describe("compile", () => {
203
205
  };
204
206
 
205
207
  mockResolveSampleFile.mockReturnValue("/path/to/sample.yaml");
206
- mockFs.existsSync.mockReturnValue(true);
208
+ mockIsFile.mockReturnValue(true);
207
209
  mockParse.mockReturnValueOnce(mockSample);
208
210
  mockResolveTemplate.mockReturnValue("resolved template");
209
211
  mockParse.mockReturnValueOnce(mockData);
@@ -234,7 +236,7 @@ describe("compile", () => {
234
236
  };
235
237
 
236
238
  mockResolveSampleFile.mockReturnValue("/path/to/sample.yaml");
237
- mockFs.existsSync.mockReturnValue(true);
239
+ mockIsFile.mockReturnValue(true);
238
240
  mockParse.mockReturnValueOnce(mockSample);
239
241
  mockResolveTemplate.mockReturnValue("resolved template");
240
242
  mockParse.mockReturnValueOnce(mockData);