Skip to content

Commit 7fa1f41

Browse files
committed
refactored compileYamlFile() to not call compileJsFile directly
1 parent 7cf517c commit 7fa1f41

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

packages/cubejs-schema-compiler/src/compiler/DataSchemaCompiler.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,10 @@ export class DataSchemaCompiler {
678678
this.pythonContext!
679679
));
680680
} else if (file.fileName.endsWith('.yml') || file.fileName.endsWith('.yaml')) {
681-
this.yamlCompiler.compileYamlFile(file, errorsReport);
681+
const transpiledFile = this.yamlCompiler.compileYamlFile(file, errorsReport);
682+
if (transpiledFile) {
683+
this.compileJsFile(transpiledFile, errorsReport);
684+
}
682685
}
683686
}
684687

packages/cubejs-schema-compiler/src/compiler/YamlCompiler.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,21 @@ export class YamlCompiler {
6969
compileContext,
7070
pythonContext: PythonCtx
7171
) {
72-
const compiledFile = await this.renderTemplate(file, compileContext, pythonContext);
72+
const renderedFile = await this.renderTemplate(file, compileContext, pythonContext);
7373

74-
return this.compileYamlFile(compiledFile, errorsReport);
74+
const transpiledFile = this.compileYamlFile(renderedFile, errorsReport);
75+
76+
if (!transpiledFile) {
77+
return;
78+
}
79+
80+
this.dataSchemaCompiler?.compileJsFile(transpiledFile, errorsReport);
7581
}
7682

7783
public compileYamlFile(
7884
file: FileContent,
7985
errorsReport: ErrorReporter,
80-
) {
86+
): FileContent | undefined {
8187
if (!file.content.trim()) {
8288
return;
8389
}
@@ -87,33 +93,37 @@ export class YamlCompiler {
8793
return;
8894
}
8995

96+
const transpiledFilesContent: string[] = [];
97+
9098
for (const key of Object.keys(yamlObj)) {
9199
if (key === 'cubes') {
92100
(yamlObj.cubes || []).forEach(({ name, ...cube }) => {
93-
const transpiledFile = this.transpileAndPrepareJsFile(file, 'cube', { name, ...cube }, errorsReport);
94-
this.dataSchemaCompiler?.compileJsFile(transpiledFile, errorsReport);
101+
const transpiledFile = this.transpileAndPrepareJsFile('cube', { name, ...cube }, errorsReport);
102+
transpiledFilesContent.push(transpiledFile);
95103
});
96104
} else if (key === 'views') {
97105
(yamlObj.views || []).forEach(({ name, ...cube }) => {
98-
const transpiledFile = this.transpileAndPrepareJsFile(file, 'view', { name, ...cube }, errorsReport);
99-
this.dataSchemaCompiler?.compileJsFile(transpiledFile, errorsReport);
106+
const transpiledFile = this.transpileAndPrepareJsFile('view', { name, ...cube }, errorsReport);
107+
transpiledFilesContent.push(transpiledFile);
100108
});
101109
} else {
102110
errorsReport.error(`Unexpected YAML key: ${key}. Only 'cubes' and 'views' are allowed here.`);
103111
}
104112
}
113+
114+
// eslint-disable-next-line consistent-return
115+
return {
116+
fileName: file.fileName,
117+
content: transpiledFilesContent.join('\n\n'),
118+
} as FileContent;
105119
}
106120

107-
private transpileAndPrepareJsFile(file: FileContent, methodFn: ('cube' | 'view'), cubeObj, errorsReport: ErrorReporter): FileContent {
121+
private transpileAndPrepareJsFile(methodFn: ('cube' | 'view'), cubeObj, errorsReport: ErrorReporter): string {
108122
const yamlAst = this.transformYamlCubeObj(cubeObj, errorsReport);
109123

110124
const cubeOrViewCall = t.callExpression(t.identifier(methodFn), [t.stringLiteral(cubeObj.name), yamlAst]);
111125

112-
const content = babelGenerator(cubeOrViewCall, {}, '').code;
113-
return {
114-
fileName: file.fileName,
115-
content
116-
};
126+
return babelGenerator(cubeOrViewCall, {}, '').code;
117127
}
118128

119129
private transformYamlCubeObj(cubeObj, errorsReport: ErrorReporter) {

0 commit comments

Comments
 (0)