Skip to content

Commit 59c9fa9

Browse files
committed
feat: remove node dependency form exports
1 parent ce0d27f commit 59c9fa9

10 files changed

Lines changed: 250 additions & 72 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## [2.0.0-beta.3] – 2025-08-25
44

5+
- Remove node dependency form exports
56
- Make export friendly use in web
67

78
## [2.0.0-beta.2] – 2025-08-25

package.json

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "2.0.0-beta.3",
44
"description": "TLB code generator",
55
"files": [
6-
"./build/*",
6+
"./build/**/*",
77
"LICENSE",
88
"README.md"
99
],
@@ -16,12 +16,6 @@
1616
"import": "./build/index.js",
1717
"require": "./build/index.js",
1818
"default": "./index.js"
19-
},
20-
"./node": {
21-
"types": "./build/node.d.ts",
22-
"import": "./build/node.js",
23-
"require": "./build/node.js",
24-
"default": "./build/node.js"
2519
}
2620
},
2721
"typesVersions": {
@@ -39,7 +33,7 @@
3933
"scripts": {
4034
"lint": "eslint . --max-warnings 0",
4135
"lint:fix": "eslint . --max-warnings 0 --fix",
42-
"test:gen": "ts-node test/generate.ts",
36+
"test:coverage": "jest --coverage",
4337
"test": "jest",
4438
"build": "npm run compile",
4539
"prepublishOnly": "npm run build",

src/cli.ts

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env node
2+
import fs from 'fs/promises';
3+
24
import meow from 'meow';
35

4-
import { generateCode } from './node';
6+
import { generateCode } from '.';
57

68
const cli = meow(
79
`
@@ -36,21 +38,38 @@ const cli = meow(
3638
},
3739
);
3840

39-
let input = cli.input.at(0);
40-
if (input) {
41-
if (input.match(/\.tlb$/) == null) {
41+
async function main() {
42+
let input = cli.input.at(0);
43+
if (input) {
44+
if (input.match(/\.tlb$/) == null) {
45+
// eslint-disable-next-line no-console
46+
console.error('Input file must have .tlb extension');
47+
process.exit(1);
48+
}
49+
50+
let output = input.replace('.tlb', '.ts');
51+
if (cli.flags.output) {
52+
output = cli.flags.output;
53+
}
54+
let language = 'typescript';
55+
if (cli.flags.language) {
56+
language = cli.flags.language;
57+
}
58+
const tlb = await fs.readFile(input, 'utf-8');
59+
const code = generateCode(tlb, language);
60+
await fs.writeFile(output, code, {});
4261
// eslint-disable-next-line no-console
43-
console.error('Input file must have .tlb extension');
44-
process.exit(1);
62+
console.log(`Generated code is saved to ${output}`);
4563
}
64+
}
4665

47-
let output = input.replace('.tlb', '.ts');
48-
if (cli.flags.output) {
49-
output = cli.flags.output;
50-
}
51-
let language = 'typescript';
52-
if (cli.flags.language) {
53-
language = cli.flags.language;
66+
main().catch((error) => {
67+
if (error instanceof Error) {
68+
// eslint-disable-next-line no-console
69+
console.error(error.message);
70+
} else {
71+
// eslint-disable-next-line no-console
72+
console.error('Unknown error:', error);
5473
}
55-
generateCode(input, output, language);
56-
}
74+
process.exit(1);
75+
});

src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export * from './ast';
2-
export { getTLBCodeByAST, generateCodeByAST, generateCodeFromData } from './main';
3-
export { generateCode, getTLBCode, generateCodeWithGenerator } from './node';
2+
export { getTLBCodeByAST, generateCodeByAST, getTLBCode, generateCodeWithGenerator, generateCode } from './main';
43
export { isBigInt, isBigIntExpr } from './generators/typescript/utils';
54
export type { CodeGenerator } from './generators/generator';
65
export { TypescriptGenerator } from './generators/typescript/generator';

src/main.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ export function getTLBCodeByAST(tree: Program, input: string): TLBCode {
1515
return convertCodeToReadonly(oldTlbCode);
1616
}
1717

18-
export function generateCodeByAST(tree: Program, input: string, getGenerator: (tlbCode: TLBCode) => CodeGenerator) {
18+
export function generateCodeByAST(
19+
tree: Program,
20+
input: string,
21+
getGenerator: (tlbCode: TLBCode) => CodeGenerator,
22+
): string {
1923
let tlbCode = getTLBCodeByAST(tree, input);
2024
let codeGenerator: CodeGenerator = getGenerator(tlbCode);
2125

@@ -70,7 +74,14 @@ export function getGenerator(resultLanguage: string) {
7074
};
7175
}
7276

73-
export async function generateCodeFromData(input: string, resultLanguage: string): Promise<string> {
74-
const tree = ast(input);
75-
return generateCodeByAST(tree, input, getGenerator(resultLanguage));
77+
export function getTLBCode(input: string) {
78+
return getTLBCodeByAST(ast(input), input);
79+
}
80+
81+
export function generateCodeWithGenerator(input: string, getGenerator: (tlbCode: TLBCode) => CodeGenerator): string {
82+
return generateCodeByAST(ast(input), input, getGenerator);
83+
}
84+
85+
export function generateCode(input: string, resultLanguage: string): string {
86+
return generateCodeWithGenerator(input, getGenerator(resultLanguage));
7687
}

src/node.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`main generateCode 1`] = `
4+
"import { Builder } from '@ton/core'
5+
import { Slice } from '@ton/core'
6+
import { beginCell } from '@ton/core'
7+
import { BitString } from '@ton/core'
8+
import { Cell } from '@ton/core'
9+
import { Address } from '@ton/core'
10+
import { ExternalAddress } from '@ton/core'
11+
import { Dictionary } from '@ton/core'
12+
import { DictionaryValue } from '@ton/core'
13+
import { TupleItem } from '@ton/core'
14+
import { parseTuple } from '@ton/core'
15+
import { serializeTuple } from '@ton/core'
16+
export function bitLen(n: number) {
17+
return n.toString(2).length;
18+
}
19+
20+
export interface Bool {
21+
readonly kind: 'Bool';
22+
readonly value: boolean;
23+
}
24+
25+
export function loadBool(slice: Slice): Bool {
26+
if (slice.remainingBits >= 1) {
27+
let value = slice.loadUint(1);
28+
return {
29+
kind: 'Bool',
30+
value: value == 1
31+
}
32+
33+
}
34+
throw new Error('Expected one of "BoolFalse" in loading "BoolFalse", but data does not satisfy any constructor');
35+
}
36+
37+
export function storeBool(bool: Bool): (builder: Builder) => void {
38+
return ((builder: Builder) => {
39+
builder.storeUint(bool.value ? 1: 0, 1);
40+
})
41+
42+
}
43+
44+
45+
46+
export function loadBoolFalse(slice: Slice): Bool {
47+
if (((slice.remainingBits >= 1) && (slice.preloadUint(1) == 0b0))) {
48+
slice.loadUint(1);
49+
return {
50+
kind: 'Bool',
51+
value: false
52+
}
53+
54+
}
55+
throw new Error('Expected one of "BoolFalse" in loading "BoolFalse", but data does not satisfy any constructor');
56+
}
57+
58+
export function loadBoolTrue(slice: Slice): Bool {
59+
if (((slice.remainingBits >= 1) && (slice.preloadUint(1) == 0b1))) {
60+
slice.loadUint(1);
61+
return {
62+
kind: 'Bool',
63+
value: true
64+
}
65+
66+
}
67+
throw new Error('Expected one of "BoolTrue" in loading "BoolTrue", but data does not satisfy any constructor');
68+
}
69+
70+
export function copyCellToBuilder(from: Cell, to: Builder): void {
71+
let slice = from.beginParse();
72+
to.storeBits(slice.loadBits(slice.remainingBits));
73+
while (slice.remainingRefs) {
74+
to.storeRef(slice.loadRef());
75+
}
76+
}
77+
// _ x:# = Foo;
78+
79+
export interface Foo {
80+
readonly kind: 'Foo';
81+
readonly x: number;
82+
}
83+
84+
// _ x:# = Foo;
85+
86+
export function loadFoo(slice: Slice): Foo {
87+
let x: number = slice.loadUint(32);
88+
return {
89+
kind: 'Foo',
90+
x: x,
91+
}
92+
93+
}
94+
95+
export function storeFoo(foo: Foo): (builder: Builder) => void {
96+
return ((builder: Builder) => {
97+
builder.storeUint(foo.x, 32);
98+
})
99+
100+
}
101+
102+
"
103+
`;
104+
105+
exports[`main getTLBCode 1`] = `
106+
TLBCode {
107+
"types": Map {
108+
"Foo" => TLBType {
109+
"constructors": [
110+
TLBConstructor {
111+
"constraints": [],
112+
"declaration": "_ x:# = Foo;",
113+
"fields": [
114+
{
115+
"anonymous": false,
116+
"fieldType": {
117+
"bits": TLBNumberExpr {
118+
"hasNeg": false,
119+
"n": 32,
120+
"variables": Set {},
121+
},
122+
"kind": "TLBNumberType",
123+
"maxBits": 32,
124+
"signed": false,
125+
"storeBits": TLBNumberExpr {
126+
"hasNeg": false,
127+
"n": 32,
128+
"variables": Set {},
129+
},
130+
},
131+
"name": "x",
132+
"subFields": [],
133+
},
134+
],
135+
"name": "_",
136+
"parameters": [],
137+
"parametersMap": Map {},
138+
"tag": {
139+
"binary": "",
140+
"bitLen": 0,
141+
},
142+
"tlbType": "Foo",
143+
"variables": [
144+
TLBVariable {
145+
"deriveExpr": undefined,
146+
"initialExpr": undefined,
147+
"isConst": false,
148+
"isField": true,
149+
"name": "x",
150+
"negated": false,
151+
"type": "#",
152+
},
153+
],
154+
"variablesMap": Map {
155+
"x" => TLBVariable {
156+
"deriveExpr": undefined,
157+
"initialExpr": undefined,
158+
"isConst": false,
159+
"isField": true,
160+
"name": "x",
161+
"negated": false,
162+
"type": "#",
163+
},
164+
},
165+
},
166+
],
167+
"name": "Foo",
168+
},
169+
},
170+
}
171+
`;

test/generate.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import path from 'path';
2+
import fs from 'fs/promises';
23

3-
import { generateCode } from '../src/node';
4+
import { generateCode } from '../src';
45

5-
function genCodeForTest(name: string) {
6+
export async function genCodeForTest(name: string) {
67
const fixturesDir = path.resolve(__dirname, 'tlb');
7-
generateCode(
8-
path.resolve(fixturesDir, name + '.tlb'),
9-
'test/generated_files/generated_' + name + '.ts',
10-
'typescript',
11-
);
8+
const tlb = await fs.readFile(path.resolve(fixturesDir, name + '.tlb'), 'utf-8');
9+
const code = generateCode(tlb, 'typescript');
10+
const output = path.resolve(__dirname, 'generated_files/generated_' + name + '.ts');
11+
await fs.writeFile(output, code, {});
1212
}
13-
14-
genCodeForTest('block');
15-
genCodeForTest('test');

test/main.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { describe, expect, test } from '@jest/globals';
2+
3+
import { generateCode, getTLBCode } from '../src';
4+
5+
describe('main', () => {
6+
test('generateCode', () => {
7+
expect(generateCode('_ x:# = Foo;', 'typescript')).toMatchSnapshot();
8+
});
9+
10+
test('getTLBCode', () => {
11+
expect(getTLBCode('_ x:# = Foo;')).toMatchSnapshot();
12+
});
13+
});

0 commit comments

Comments
 (0)