Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non writing to fs generator #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateCode } from "./src/main";
export { generateCode, generateCodeByAST, generateCodeWithGenerator } from "./src/main";
export { generateCode, generateCodeByAST, generateCodeWithGenerator, generateCodeFromData } from "./src/main";
export { CodeGenerator } from "./src/generators/generator";
export { TypescriptGenerator } from "./src/generators/typescript/generator";
34 changes: 21 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { fillConstructors } from "./astbuilder/fill_constructors";
import { CodeBuilder } from "./generators/CodeBuilder";
import { CodeGenerator, CommonGenDeclaration } from "./generators/generator";
import { TypescriptGenerator } from "./generators/typescript/generator";
import fs from 'fs'
import fs from 'fs/promises'


export function generateCodeByAST(tree: Program, input: string, getGenerator: (tlbCode: TLBCode) => CodeGenerator) {
Expand Down Expand Up @@ -61,25 +61,33 @@ export function generateCodeByAST(tree: Program, input: string, getGenerator: (t
return generatedCode;
}

export function generateCodeWithGenerator(inputPath: string, outputPath: string, getGenerator: (tlbCode: TLBCode) => CodeGenerator) {
const input = fs.readFileSync(
function getGenerator(resultLanguage: string) {
return (tlbCode: TLBCode) => {
if (resultLanguage == 'typescript') {
return new TypescriptGenerator(tlbCode)
} else {
throw new Error(`Result language ${resultLanguage} is not supported`)
}
}
}

export async function generateCodeFromData(input: string, resultLanguage: string): Promise<string> {
const tree = ast(input)
return generateCodeByAST(tree, input, getGenerator(resultLanguage));
}

export async function generateCodeWithGenerator(inputPath: string, outputPath: string, getGenerator: (tlbCode: TLBCode) => CodeGenerator) {
const input = await fs.readFile(
inputPath,
'utf-8',
)

const tree = ast(input)

fs.writeFile(outputPath, generateCodeByAST(tree, input, getGenerator), () => { });
await fs.writeFile(outputPath, generateCodeByAST(tree, input, getGenerator), {});
console.log(`Generated code is saved to ${outputPath}`);
}

export function generateCode(inputPath: string, outputPath: string, resultLanguage: string) {
let getGenerator = (tlbCode: TLBCode) => {
if (resultLanguage == 'typescript') {
return new TypescriptGenerator(tlbCode)
} else {
throw new Error(`Result language ${resultLanguage} is not supported`)
}
}
generateCodeWithGenerator(inputPath, outputPath, getGenerator);
export async function generateCode(inputPath: string, outputPath: string, resultLanguage: string) {
return generateCodeWithGenerator(inputPath, outputPath, getGenerator(resultLanguage));
}