@caleuche/cli 0.3.0 → 0.4.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/CHANGELOG.md +7 -0
- package/dist/batch.js +4 -4
- package/dist/index.js +10 -3
- package/package.json +1 -1
- package/src/batch.ts +6 -6
- package/src/index.ts +10 -3
- package/src/utils.ts +2 -0
- package/test/bach.test.ts +232 -177
- package/test/utils.test.ts +1 -1
package/CHANGELOG.md
CHANGED
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}"
|
|
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
|
|
14
|
-
.
|
|
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
|
|
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
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,7 @@ function resolveVariantDefinition(
|
|
|
82
80
|
}
|
|
83
81
|
}
|
|
84
82
|
|
|
85
|
-
export function batchCompile(batchFile: string) {
|
|
83
|
+
export function batchCompile(batchFile: string, options: { outputDir?: string }) {
|
|
86
84
|
if (!isFile(batchFile)) {
|
|
87
85
|
console.error(`Batch file "${batchFile}" does not exist or is not a file.`);
|
|
88
86
|
process.exit(1);
|
|
@@ -127,10 +125,12 @@ export function batchCompile(batchFile: string) {
|
|
|
127
125
|
process.exit(1);
|
|
128
126
|
}
|
|
129
127
|
|
|
130
|
-
const effectiveOutputPath = path.join(
|
|
128
|
+
const effectiveOutputPath = path.join(
|
|
129
|
+
options?.outputDir || workingDirectory,
|
|
130
|
+
variant.output);
|
|
131
131
|
|
|
132
132
|
if (
|
|
133
|
-
!compileAndWriteOutput(sample, resolvedVariant, effectiveOutputPath, {
|
|
133
|
+
!compileAndWriteOutput(sample, resolvedVariant.properties, effectiveOutputPath, {
|
|
134
134
|
project: true,
|
|
135
135
|
})
|
|
136
136
|
) {
|
package/src/index.ts
CHANGED
|
@@ -11,10 +11,17 @@ program
|
|
|
11
11
|
.version(version);
|
|
12
12
|
|
|
13
13
|
program
|
|
14
|
-
.command("compile
|
|
15
|
-
.
|
|
14
|
+
.command("compile")
|
|
15
|
+
.argument("<sample-path>", "Path to the sample file or the directory that containes it.")
|
|
16
|
+
.argument("<data-file>", "Path to the input to be used to compile the template.")
|
|
17
|
+
.argument("<output-directory>", "Directory where the compiled output will be written.")
|
|
18
|
+
.option("-p, --project", "Flag to indicate whether to generate the appropriate project file along with the compiled template.")
|
|
16
19
|
.action(compile);
|
|
17
20
|
|
|
18
|
-
program
|
|
21
|
+
program
|
|
22
|
+
.command("batch")
|
|
23
|
+
.argument("<batch-file>", "Path to the batch file")
|
|
24
|
+
.option("-d, --output-dir <outputDir>", "Output directory for compiled samples")
|
|
25
|
+
.action(batchCompile);
|
|
19
26
|
|
|
20
27
|
program.parse();
|
package/src/utils.ts
CHANGED
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
|
-
|
|
3
|
+
import path from "path";
|
|
4
|
+
import os from "os";
|
|
7
5
|
|
|
8
6
|
vi.mock("@caleuche/core");
|
|
9
|
-
import { compileSample
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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(
|
|
68
|
+
batchCompile(batchFilePath, {});
|
|
83
69
|
}).toThrow("process.exit");
|
|
84
70
|
expect(mockConsoleError).toHaveBeenCalledWith(
|
|
85
|
-
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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(
|
|
88
|
+
batchCompile(batchFilePath, {});
|
|
103
89
|
}).toThrow("process.exit");
|
|
104
90
|
expect(mockConsoleError).toHaveBeenCalledWith(
|
|
105
|
-
`
|
|
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
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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(
|
|
107
|
+
batchCompile(batchFilePath, {});
|
|
121
108
|
}).toThrow("process.exit");
|
|
122
109
|
expect(mockConsoleError).toHaveBeenCalledWith(
|
|
123
|
-
|
|
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
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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(
|
|
139
|
+
batchCompile(batchFilePath, {});
|
|
141
140
|
}).toThrow("process.exit");
|
|
142
141
|
expect(mockConsoleError).toHaveBeenCalledWith(
|
|
143
|
-
|
|
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
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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(
|
|
175
|
+
batchCompile(batchFilePath, {});
|
|
173
176
|
}).toThrow("process.exit");
|
|
174
177
|
expect(mockConsoleError).toHaveBeenCalledWith(
|
|
175
|
-
`Variant "
|
|
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(
|
|
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":"
|
|
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
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
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(
|
|
289
|
-
|
|
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
|
-
|
|
292
|
-
|
|
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(
|
|
296
|
-
|
|
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
|
});
|
package/test/utils.test.ts
CHANGED
|
@@ -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];
|