Skip to content
Merged
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 generate_tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import { generateCode } from './src/main';
import { generateCode } from './src/node';

function genCodeForTest(name: string) {
const fixturesDir = path.resolve(__dirname, 'test');
Expand Down
10 changes: 2 additions & 8 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
export {
generateCode,
getTLBCode,
getTLBCodeByAST,
generateCodeByAST,
generateCodeWithGenerator,
generateCodeFromData,
} from './src/main';
export { getTLBCodeByAST, generateCodeByAST, generateCodeFromData } from './src/main';
export { generateCode, getTLBCode, generateCodeWithGenerator } from './src/node';
export * from './src/ast';
export { isBigInt as isBigIntForJson, isBigIntExpr as isBigIntExprForJson } from './src/generators/typescript/utils';
export type { CodeGenerator } from './src/generators/generator';
Expand Down
2 changes: 1 addition & 1 deletion main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
import meow from 'meow';

import { generateCode } from './src/main';
import { generateCode } from './src/node';

const cli = meow(
`
Expand Down
30 changes: 1 addition & 29 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fs from 'fs/promises';

import { ast, Program } from '@ton-community/tlb-parser';

import { TLBCode, TLBType } from './ast';
Expand All @@ -19,14 +17,6 @@ export function getTLBCodeByAST(tree: Program, input: string) {
return tlbCode;
}

export async function getTLBCode(inputPath: string) {
const input = await fs.readFile(inputPath, 'utf-8');

const tree = ast(input);

return getTLBCodeByAST(tree, input);
}

export function generateCodeByAST(tree: Program, input: string, getGenerator: (tlbCode: TLBCode) => CodeGenerator) {
let tlbCode = getTLBCodeByAST(tree, input);
let codeGenerator: CodeGenerator = getGenerator(tlbCode);
Expand Down Expand Up @@ -72,7 +62,7 @@ export function generateCodeByAST(tree: Program, input: string, getGenerator: (t
return generatedCode;
}

function getGenerator(resultLanguage: string) {
export function getGenerator(resultLanguage: string) {
return (tlbCode: TLBCode) => {
if (resultLanguage == 'typescript') {
return new TypescriptGenerator(tlbCode);
Expand All @@ -86,21 +76,3 @@ export async function generateCodeFromData(input: string, resultLanguage: 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);

await fs.writeFile(outputPath, generateCodeByAST(tree, input, getGenerator), {});
// eslint-disable-next-line no-console
console.log(`Generated code is saved to ${outputPath}`);
}

export async function generateCode(inputPath: string, outputPath: string, resultLanguage: string) {
return generateCodeWithGenerator(inputPath, outputPath, getGenerator(resultLanguage));
}
33 changes: 33 additions & 0 deletions src/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fs from 'fs/promises';

import { ast } from '@ton-community/tlb-parser';

import { TLBCode } from './ast';
import { CodeGenerator } from './generators/generator';
import { generateCodeByAST, getGenerator, getTLBCodeByAST } from './main';

export async function getTLBCode(inputPath: string) {
const input = await fs.readFile(inputPath, 'utf-8');

const tree = ast(input);

return getTLBCodeByAST(tree, input);
}

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

const tree = ast(input);

await fs.writeFile(outputPath, generateCodeByAST(tree, input, getGenerator), {});
// eslint-disable-next-line no-console
console.log(`Generated code is saved to ${outputPath}`);
}

export async function generateCode(inputPath: string, outputPath: string, resultLanguage: string) {
return generateCodeWithGenerator(inputPath, outputPath, getGenerator(resultLanguage));
}