@caleuche/cli 0.2.2 → 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 +12 -0
- package/dist/batch.js +28 -32
- package/dist/common.js +1 -1
- package/dist/utils.js +6 -6
- package/package.json +1 -1
- package/src/batch.ts +44 -34
- package/src/common.ts +2 -1
- package/src/utils.ts +6 -8
- package/test/bach.test.ts +40 -42
- package/test/common.test.ts +3 -15
- package/test/compile.test.ts +9 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
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
|
+
|
|
9
|
+
## 0.2.3
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 0d6214d: Making batch output path relative. Fixing variant references.
|
|
14
|
+
|
|
3
15
|
## 0.2.2
|
|
4
16
|
|
|
5
17
|
### 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 ((
|
|
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,60 +36,56 @@ function loadVariantDefinitions(variants) {
|
|
|
36
36
|
}
|
|
37
37
|
return definitions;
|
|
38
38
|
}
|
|
39
|
-
function resolveVariantDefinition(variant, variantRegistry) {
|
|
40
|
-
if ((0, utils_1.
|
|
41
|
-
|
|
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)) {
|
|
49
|
-
return variant;
|
|
39
|
+
function resolveVariantDefinition(variant, variantRegistry, workingDirectory) {
|
|
40
|
+
if ((0, utils_1.isVariantDefinition)(variant.data)) {
|
|
41
|
+
return variant.data;
|
|
50
42
|
}
|
|
51
|
-
|
|
52
|
-
const v =
|
|
53
|
-
if (
|
|
54
|
-
|
|
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(`
|
|
53
|
+
console.error(`Variant "${variant.data}" could not be resolved.`);
|
|
60
54
|
return null;
|
|
61
55
|
}
|
|
62
56
|
function batchCompile(batchFile) {
|
|
63
|
-
if (!
|
|
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);
|
|
69
|
+
console.log(`Loaded ${Object.keys(variants || {}).length} variant definitions from batch file.`);
|
|
77
70
|
if (!variants) {
|
|
78
71
|
process.exit(1);
|
|
79
72
|
}
|
|
80
73
|
const samples = bachDefinition.samples;
|
|
81
74
|
for (const sampleDefinition of samples) {
|
|
82
|
-
|
|
75
|
+
console.log(`Processing sample: ${sampleDefinition.templatePath}`);
|
|
76
|
+
const templatePath = path_1.default.join(workingDirectory, sampleDefinition.templatePath);
|
|
83
77
|
const sample = (0, common_1.resolveAndParseSample)(templatePath);
|
|
84
78
|
if (!sample) {
|
|
85
79
|
process.exit(1);
|
|
86
80
|
}
|
|
87
81
|
for (const variant of sampleDefinition.variants) {
|
|
88
|
-
|
|
82
|
+
console.log("Processing variant...");
|
|
83
|
+
const resolvedVariant = resolveVariantDefinition(variant, variants, workingDirectory);
|
|
89
84
|
if (!resolvedVariant) {
|
|
90
85
|
process.exit(1);
|
|
91
86
|
}
|
|
92
|
-
|
|
87
|
+
const effectiveOutputPath = path_1.default.join(workingDirectory, variant.output);
|
|
88
|
+
if (!(0, common_1.compileAndWriteOutput)(sample, resolvedVariant, effectiveOutputPath, {
|
|
93
89
|
project: true,
|
|
94
90
|
})) {
|
|
95
91
|
console.error(`Sample: ${sampleDefinition.templatePath}, Variant: ${JSON.stringify(variant)}`);
|
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 (!
|
|
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.
|
|
14
|
-
exports.
|
|
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
|
|
64
|
-
return
|
|
63
|
+
function getAbsoluteDirectoryPath(filePath) {
|
|
64
|
+
return path_1.default.dirname(path_1.default.resolve(filePath));
|
|
65
65
|
}
|
|
66
|
-
function
|
|
67
|
-
return
|
|
66
|
+
function isFile(filePath) {
|
|
67
|
+
return fs_1.default.existsSync(filePath) && fs_1.default.lstatSync(filePath).isFile();
|
|
68
68
|
}
|
package/package.json
CHANGED
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 (
|
|
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
|
|
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}`,
|
|
@@ -43,52 +45,54 @@ function loadVariantDefinitions(
|
|
|
43
45
|
}
|
|
44
46
|
|
|
45
47
|
function resolveVariantDefinition(
|
|
46
|
-
variant:
|
|
48
|
+
variant: SampleVariantConfig,
|
|
47
49
|
variantRegistry: Record<string, SampleVariantDefinition>,
|
|
50
|
+
workingDirectory: string
|
|
48
51
|
): SampleVariantDefinition | null {
|
|
49
|
-
if (
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return variant;
|
|
58
|
-
} else if (isVariantReference(variant)) {
|
|
59
|
-
const v = variantRegistry[variant];
|
|
60
|
-
if (!v) {
|
|
61
|
-
console.error(`Variant reference "${variant}" not found in registry.`);
|
|
62
|
-
return null;
|
|
52
|
+
if (isVariantDefinition(variant.data)) {
|
|
53
|
+
return variant.data;
|
|
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;
|
|
63
60
|
}
|
|
64
|
-
return v;
|
|
65
61
|
}
|
|
66
|
-
|
|
67
|
-
|
|
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
|
|
68
70
|
}
|
|
69
71
|
|
|
70
72
|
export function batchCompile(batchFile: string) {
|
|
71
|
-
if (!
|
|
72
|
-
console.error(`Batch file "${batchFile}" does not exist.`);
|
|
73
|
-
process.exit(1);
|
|
74
|
-
}
|
|
75
|
-
if (!fs.lstatSync(batchFile).isFile()) {
|
|
76
|
-
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.`);
|
|
77
75
|
process.exit(1);
|
|
78
76
|
}
|
|
77
|
+
const workingDirectory = getAbsoluteDirectoryPath(batchFile);
|
|
78
|
+
console.log(`Working directory: ${workingDirectory}`);
|
|
79
79
|
const bachDefinition = parse<BatchCompileOptions>(batchFile);
|
|
80
80
|
if (!bachDefinition) {
|
|
81
81
|
console.error(`Failed to parse batch file: ${batchFile}`);
|
|
82
82
|
process.exit(1);
|
|
83
83
|
}
|
|
84
|
-
const variants = loadVariantDefinitions(bachDefinition.variants);
|
|
84
|
+
const variants = loadVariantDefinitions(bachDefinition.variants, workingDirectory);
|
|
85
|
+
console.log(
|
|
86
|
+
`Loaded ${Object.keys(variants || {}).length} variant definitions from batch file.`,
|
|
87
|
+
);
|
|
85
88
|
if (!variants) {
|
|
86
89
|
process.exit(1);
|
|
87
90
|
}
|
|
88
91
|
const samples = bachDefinition.samples;
|
|
89
92
|
for (const sampleDefinition of samples) {
|
|
93
|
+
console.log(`Processing sample: ${sampleDefinition.templatePath}`);
|
|
90
94
|
const templatePath = path.join(
|
|
91
|
-
|
|
95
|
+
workingDirectory,
|
|
92
96
|
sampleDefinition.templatePath,
|
|
93
97
|
);
|
|
94
98
|
const sample = resolveAndParseSample(templatePath);
|
|
@@ -97,13 +101,19 @@ export function batchCompile(batchFile: string) {
|
|
|
97
101
|
}
|
|
98
102
|
|
|
99
103
|
for (const variant of sampleDefinition.variants) {
|
|
100
|
-
|
|
104
|
+
console.log("Processing variant...");
|
|
105
|
+
const resolvedVariant = resolveVariantDefinition(variant, variants, workingDirectory);
|
|
101
106
|
if (!resolvedVariant) {
|
|
102
107
|
process.exit(1);
|
|
103
108
|
}
|
|
104
109
|
|
|
110
|
+
const effectiveOutputPath = path.join(
|
|
111
|
+
workingDirectory,
|
|
112
|
+
variant.output,
|
|
113
|
+
);
|
|
114
|
+
|
|
105
115
|
if (
|
|
106
|
-
!compileAndWriteOutput(sample, resolvedVariant
|
|
116
|
+
!compileAndWriteOutput(sample, resolvedVariant, effectiveOutputPath, {
|
|
107
117
|
project: true,
|
|
108
118
|
})
|
|
109
119
|
) {
|
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 (!
|
|
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
|
|
62
|
-
|
|
63
|
-
):
|
|
64
|
-
return
|
|
61
|
+
export function getAbsoluteDirectoryPath(
|
|
62
|
+
filePath: string,
|
|
63
|
+
): string {
|
|
64
|
+
return path.dirname(path.resolve(filePath));
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
export function
|
|
68
|
-
|
|
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
|
-
|
|
20
|
-
|
|
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
|
|
28
|
-
const
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
"
|
|
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
|
-
`
|
|
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
|
-
|
|
104
|
-
|
|
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
|
-
"
|
|
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
|
-
"
|
|
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
|
-
|
|
142
|
-
|
|
141
|
+
mockIsFile.mockReturnValue(true);
|
|
142
|
+
mockGetAbsoluteDirectoryPath.mockReturnValue("/working/directory");
|
|
143
143
|
mockParse.mockReturnValueOnce({
|
|
144
144
|
variants: {},
|
|
145
145
|
samples: [
|
|
146
|
-
{ templatePath: "sample.yaml", variants: ["
|
|
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(
|
|
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
|
-
|
|
170
|
-
|
|
168
|
+
mockIsFile.mockReturnValue(true);
|
|
169
|
+
mockGetAbsoluteDirectoryPath.mockReturnValue("/working/directory");
|
|
171
170
|
mockParse.mockReturnValueOnce({
|
|
172
171
|
variants: {},
|
|
173
172
|
samples: [
|
|
174
|
-
{
|
|
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
|
-
|
|
206
|
-
|
|
206
|
+
mockIsFile.mockReturnValue(true);
|
|
207
|
+
mockGetAbsoluteDirectoryPath.mockReturnValue("/working/directory");
|
|
207
208
|
mockParse.mockReturnValueOnce({
|
|
208
209
|
variants: {},
|
|
209
210
|
samples: [
|
|
210
|
-
{
|
|
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
|
-
|
|
239
|
-
|
|
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",
|
|
247
|
-
{ output: "v2.output",
|
|
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
|
});
|
package/test/common.test.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
|
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
|
-
|
|
47
|
+
mockIsFile.mockReturnValue(true);
|
|
60
48
|
mockParse.mockReturnValueOnce(null);
|
|
61
49
|
|
|
62
50
|
const sample = resolveAndParseSample("sample");
|
package/test/compile.test.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
239
|
+
mockIsFile.mockReturnValue(true);
|
|
238
240
|
mockParse.mockReturnValueOnce(mockSample);
|
|
239
241
|
mockResolveTemplate.mockReturnValue("resolved template");
|
|
240
242
|
mockParse.mockReturnValueOnce(mockData);
|