Skip to content

Commit 10e1153

Browse files
committed
Prepare transpileYaml in native + schema compiler
1 parent 7e0b17c commit 10e1153

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

packages/cubejs-backend-native/js/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,16 @@ export const transpileJs = async (transpileRequests: TransformConfig[]): Promise
517517
throw new Error('TranspileJs native implementation not found!');
518518
};
519519

520+
export const transpileYaml = async (transpileRequests: TransformConfig[]): Promise<TransformResponse[]> => {
521+
const native = loadNative();
522+
523+
if (native.transpileYaml) {
524+
return native.transpileYaml(transpileRequests);
525+
}
526+
527+
throw new Error('TranspileYaml native implementation not found!');
528+
};
529+
520530
export interface PyConfiguration {
521531
repositoryFactory?: (ctx: unknown) => Promise<unknown>,
522532
logger?: (msg: string, params: Record<string, any>) => void,

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import workerpool from 'workerpool';
1313
import { LRUCache } from 'lru-cache';
1414

1515
import { FileContent, getEnv, isNativeSupported, SchemaFileRepository } from '@cubejs-backend/shared';
16-
import { NativeInstance, PythonCtx, transpileJs } from '@cubejs-backend/native';
16+
import { NativeInstance, PythonCtx, transpileJs, transpileYaml } from '@cubejs-backend/native';
1717
import { UserError } from './UserError';
1818
import { ErrorReporter, ErrorReporterOptions, SyntaxErrorInterface } from './ErrorReporter';
1919
import { CONTEXT_SYMBOLS, CubeDefinition, CubeSymbols } from './CubeSymbols';
@@ -623,14 +623,33 @@ export class DataSchemaCompiler {
623623
return undefined;
624624
}
625625

626+
// We update the yaml file content to the transpiled js content
627+
// and raise related flag so it will go JS transpilation flow afterward
628+
// avoiding costly YAML/Python parsing again.
626629
private async transpileYamlFile(
627630
file: FileContent,
628631
errorsReport: ErrorReporter,
629-
{ cubeNames, cubeSymbols, contextSymbols, transpilerNames, compilerId, stage }: TranspileOptions
632+
{ cubeNames, cubeSymbols, compilerId }: TranspileOptions
630633
): Promise<(FileContent | undefined)> {
631-
/* if (getEnv('transpilationNative')) {
634+
if (getEnv('transpilationNative')) {
635+
const reqData = {
636+
fileName: file.fileName,
637+
fileContent: file.content,
638+
transpilers: [],
639+
compilerId: compilerId || '',
640+
};
641+
642+
errorsReport.inFile(file);
643+
const res = await transpileYaml([reqData]);
644+
errorsReport.addErrors(res[0].errors);
645+
errorsReport.addWarnings(res[0].warnings as unknown as SyntaxErrorInterface[]);
646+
errorsReport.exitFile();
647+
648+
file.content = res[0].code;
649+
file.convertedToJs = true;
632650

633-
} else */ if (getEnv('transpilationWorkerThreads')) {
651+
return { ...file, content: res[0].code };
652+
} else if (getEnv('transpilationWorkerThreads')) {
634653
const data = {
635654
fileName: file.fileName,
636655
content: file.content,
@@ -651,9 +670,6 @@ export class DataSchemaCompiler {
651670
const transpiledFile = this.yamlCompiler.transpileYamlFile(file, errorsReport);
652671

653672
if (transpiledFile) {
654-
// We update the yaml file content to the transpiled js content
655-
// and raise related flag so it will go JS transpilation flow afterward
656-
// avoiding costly YAML/Python parsing again.
657673
file.content = transpiledFile.content;
658674
file.convertedToJs = true;
659675
}

0 commit comments

Comments
 (0)