ondc-code-generator 0.0.7 → 0.0.9

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.
@@ -17,7 +17,7 @@ export declare class ConfigCompiler {
17
17
  constructor(language: SupportedLanguages);
18
18
  initialize: (buildYaml: string, generatorConfig?: Partial<CodeGeneratorConfig>) => Promise<void>;
19
19
  performValidations: (valConfig: ValidationConfig) => Promise<void>;
20
- generateCode: (valConfig: ValidationConfig) => Promise<void>;
20
+ generateCode: (valConfig: ValidationConfig, codeName?: string) => Promise<void>;
21
21
  generateL0Schema: () => Promise<void>;
22
22
  }
23
23
  export {};
@@ -39,12 +39,14 @@ export class ConfigCompiler {
39
39
  throw new Error("Validation failed");
40
40
  }
41
41
  };
42
- this.generateCode = async (valConfig) => {
42
+ this.generateCode = async (valConfig, codeName = "L1-Validations") => {
43
43
  await this.performValidations(valConfig);
44
44
  // Generate code based on the language
45
45
  switch (this.language) {
46
46
  case SupportedLanguages.Typescript:
47
- await new TypescriptGenerator(valConfig, this.errorDefinitions ?? [], "./generated/L1-validations").generateCode();
47
+ await new TypescriptGenerator(valConfig, this.errorDefinitions ?? [], `./generated/${codeName}`).generateCode({
48
+ codeName: codeName,
49
+ });
48
50
  break;
49
51
  default:
50
52
  throw new Error("Language not supported");
@@ -1,5 +1,8 @@
1
1
  import { ValidationConfig } from "../../../types/config-types";
2
2
  import { ErrorDefinition } from "../../../types/error-codes";
3
+ export type CodeGeneratorProps = {
4
+ codeName: string;
5
+ };
3
6
  export declare abstract class CodeGenerator {
4
7
  validationConfig: ValidationConfig;
5
8
  rootPath: string;
@@ -7,5 +10,5 @@ export declare abstract class CodeGenerator {
7
10
  constructor(validationConfig: ValidationConfig, errorCodes: ErrorDefinition[], rootPath?: string);
8
11
  abstract generateSessionDataCode(): Promise<void>;
9
12
  abstract generateValidationCode(): Promise<void>;
10
- abstract generateCode(): Promise<void>;
13
+ abstract generateCode(codeConfig: CodeGeneratorProps): Promise<void>;
11
14
  }
@@ -1,12 +1,3 @@
1
- /*
2
- 1. generate code for session_data
3
- 1. a function take in payload and update the session_data in cache
4
- 2. get function which returns the session_data object
5
- 2. generate code for test-objects
6
- 1. single test object generation
7
- 1. handle nested
8
- 2. combine all the tests
9
- */
10
1
  export class CodeGenerator {
11
2
  constructor(validationConfig, errorCodes, rootPath = "./") {
12
3
  this.validationConfig = validationConfig;
@@ -1,9 +1,9 @@
1
1
  import { TestObject } from "../../../types/config-types.js";
2
- import { CodeGenerator } from "../classes/abstract-generator.js";
2
+ import { CodeGenerator, CodeGeneratorProps } from "../classes/abstract-generator.js";
3
3
  export declare class TypescriptGenerator extends CodeGenerator {
4
4
  generateSessionDataCode: () => Promise<never>;
5
5
  generateValidationCode: () => Promise<void>;
6
- generateCode: () => Promise<void>;
6
+ generateCode: (codeConfig: CodeGeneratorProps) => Promise<void>;
7
7
  generateTestFunction: (testObject: TestObject) => Promise<{
8
8
  funcName: string;
9
9
  code: string;
@@ -7,7 +7,7 @@ import { markdownMessageGenerator } from "../documentation/markdown-message-gene
7
7
  import { getVariablesFromTest as extractVariablesFromText } from "../../../utils/general-utils/test-object-utils.js";
8
8
  import { ConvertArrayToString } from "../../../utils/general-utils/string-utils.js";
9
9
  import { compileInputToTs } from "./ts-ast.js";
10
- import { CodeGenerator } from "../classes/abstract-generator.js";
10
+ import { CodeGenerator, } from "../classes/abstract-generator.js";
11
11
  import { writeAndFormatCode } from "../../../utils/fs-utils.js";
12
12
  import { MarkdownDocGenerator } from "../documentation/md-generator.js";
13
13
  const __filename = fileURLToPath(import.meta.url);
@@ -34,7 +34,7 @@ export class TypescriptGenerator extends CodeGenerator {
34
34
  await writeAndFormatCode(this.rootPath, `./api-tests/${key}.ts`, finalCode, "typescript");
35
35
  }
36
36
  };
37
- this.generateCode = async () => {
37
+ this.generateCode = async (codeConfig) => {
38
38
  const jsonPathUtilsCode = readFileSync(path.resolve(__dirname, "./templates/json-path-utils.mustache"), "utf-8");
39
39
  const validtionUtils = readFileSync(path.resolve(__dirname, "./templates/validation-utils.mustache"), "utf-8");
40
40
  const typesTemplate = readFileSync(path.resolve(__dirname, "./templates/test-config.mustache"), "utf-8");
@@ -46,7 +46,7 @@ export class TypescriptGenerator extends CodeGenerator {
46
46
  writeAndFormatCode(this.rootPath, "./types/test-config.ts", typesCode, "typescript");
47
47
  await this.generateValidationCode();
48
48
  await writeAndFormatCode(this.rootPath, "error.ts", this.generateErrorFile(this.errorCodes), "typescript");
49
- await writeAndFormatCode(this.rootPath, "index.ts", this.generateIndexFile(Object.keys(this.validationConfig[ConfigSyntax.Tests])), "typescript");
49
+ await writeAndFormatCode(this.rootPath, "index.ts", this.generateIndexFile(Object.keys(this.validationConfig[ConfigSyntax.Tests]), codeConfig.codeName), "typescript");
50
50
  await new MarkdownDocGenerator(this.validationConfig, this.errorCodes, this.rootPath).generateCode();
51
51
  };
52
52
  this.generateTestFunction = async (testObject) => {
@@ -155,12 +155,12 @@ export class TypescriptGenerator extends CodeGenerator {
155
155
  }
156
156
  return result;
157
157
  }
158
- generateIndexFile(apis) {
158
+ generateIndexFile(apis, functionName = "L1Validations") {
159
159
  const importsCode = apis
160
160
  .map((api) => `import ${api} from "./api-tests/${api}";`)
161
161
  .join("\n");
162
162
  const masterFunction = `
163
- export function performL1Validations(action: string, payload: any,allErrors = false, externalData = {}) {
163
+ export function perform${functionName}(action: string, payload: any,allErrors = false, externalData = {}) {
164
164
  switch (action) {
165
165
  ${apis
166
166
  .map((api) => `case "${api}": return ${api}({
package/dist/index.d.ts CHANGED
@@ -1 +1,2 @@
1
- export {};
1
+ import { ConfigCompiler } from "./generator/config-compiler.js";
2
+ export { ConfigCompiler };
package/dist/index.js CHANGED
@@ -1,42 +1,2 @@
1
- // import { ConfigCompiler } from "./generator/config-compiler.js";
2
- // export { ConfigCompiler };
3
- import { readFileSync } from "fs";
4
- import path from "path";
5
- import { fileURLToPath } from "url";
6
- import { SupportedLanguages } from "./types/compiler-types.js";
7
1
  import { ConfigCompiler } from "./generator/config-compiler.js";
8
- // Get __dirname equivalent
9
- const __filename = fileURLToPath(import.meta.url);
10
- const __dirname = path.dirname(__filename);
11
- const main = async () => {
12
- const compiler = new ConfigCompiler(SupportedLanguages.Typescript);
13
- const buildPath = path.resolve(__dirname, "../samples/build.yaml");
14
- const valConfigPath = path.resolve(__dirname, "../samples/validation-config.json");
15
- const buildYaml = readFileSync(buildPath, "utf-8");
16
- const valConfig = JSON.parse(readFileSync(valConfigPath, "utf-8"));
17
- await compiler.initialize(buildYaml);
18
- await compiler.generateCode(valConfig);
19
- // await compiler.generateL0Schema();
20
- // await compiler.performValidations(valConfig);
21
- // const inputs = [
22
- // "a are unique && b are unique",
23
- // "a are unique",
24
- // "(a are unique && b are unique) || c are unique",
25
- // "a are unique && b are unique || c are unique",
26
- // "a are unique && (b are unique || c are unique)",
27
- // "a are unique && b are unique && c are unique && d are unique",
28
- // "a are unique && !(b are unique || !(c are unique))",
29
- // "a are unique && (b are unique || !(!(c are unique))) && !(d are unique)",
30
- // ];
31
- // let output = "";
32
- // for (const input of inputs) {
33
- // const cst = parseReturnInput(input);
34
- // const ast = buildAst(cst);
35
- // const out = CompileToMarkdown(ast, "A", 0, false);
36
- // output += `\n## ${input}\n\n${out}\n`;
37
- // }
38
- // writeFileSync(path.resolve(__dirname, "../samples/output.md"), output);
39
- };
40
- (async () => {
41
- await main();
42
- })();
2
+ export { ConfigCompiler };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ondc-code-generator",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "generate code from build.yaml ",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",