cel-proto-parser 0.2.0
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.
- package/LICENSE +21 -0
- package/README.md +136 -0
- package/ast/enums.d.ts +14 -0
- package/ast/enums.js +59 -0
- package/ast/index.d.ts +2 -0
- package/ast/index.js +18 -0
- package/ast/types.d.ts +40 -0
- package/ast/types.js +158 -0
- package/converter/index.d.ts +22 -0
- package/converter/index.js +242 -0
- package/deparser/index.d.ts +95 -0
- package/deparser/index.js +504 -0
- package/esm/ast/enums.js +22 -0
- package/esm/ast/index.js +2 -0
- package/esm/ast/types.js +117 -0
- package/esm/converter/index.js +239 -0
- package/esm/deparser/index.js +499 -0
- package/esm/index.js +6 -0
- package/esm/options/defaults.js +68 -0
- package/esm/options/index.js +2 -0
- package/esm/options/types.js +4 -0
- package/esm/parser.js +211 -0
- package/esm/utils/index.js +79 -0
- package/index.d.ts +6 -0
- package/index.js +28 -0
- package/options/defaults.d.ts +2 -0
- package/options/defaults.js +71 -0
- package/options/index.d.ts +2 -0
- package/options/index.js +18 -0
- package/options/types.d.ts +84 -0
- package/options/types.js +5 -0
- package/package.json +55 -0
- package/parser.d.ts +68 -0
- package/parser.js +251 -0
- package/utils/index.d.ts +46 -0
- package/utils/index.js +127 -0
package/esm/parser.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { parse } from '@launchql/protobufjs';
|
|
2
|
+
import { readFileSync, writeFileSync, mkdirSync } from 'fs';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import * as t from '@babel/types';
|
|
5
|
+
import generate from '@babel/generator';
|
|
6
|
+
import { getOptionsWithDefaults } from './options';
|
|
7
|
+
import { convertTypeToTsInterface, generateNodeUnionType, generateAstHelperMethods, generateTypeImportSpecifiers, generateEnumImports } from './ast';
|
|
8
|
+
import { convertEnumToTsUnionType, convertEnumToTsEnumDeclaration } from './ast/enums';
|
|
9
|
+
import { createDefaultImport } from './utils';
|
|
10
|
+
import { generateDeparserCode } from './deparser';
|
|
11
|
+
const GENERATED_HEADER = `/**
|
|
12
|
+
* This file was automatically generated by cel-proto-parser.
|
|
13
|
+
* DO NOT MODIFY IT BY HAND. Instead, modify the source proto file,
|
|
14
|
+
* and run the cel-proto-parser generate command to regenerate this file.
|
|
15
|
+
*/`;
|
|
16
|
+
function isProtoType(obj) {
|
|
17
|
+
return 'fields' in obj;
|
|
18
|
+
}
|
|
19
|
+
function isProtoEnum(obj) {
|
|
20
|
+
return 'values' in obj && !('fields' in obj);
|
|
21
|
+
}
|
|
22
|
+
export class CelProtoParser {
|
|
23
|
+
protoPath;
|
|
24
|
+
options;
|
|
25
|
+
root = null;
|
|
26
|
+
types = [];
|
|
27
|
+
enums = [];
|
|
28
|
+
constructor(protoPath, options) {
|
|
29
|
+
this.protoPath = protoPath;
|
|
30
|
+
this.options = getOptionsWithDefaults(options);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Parse the proto file and extract types and enums
|
|
34
|
+
*/
|
|
35
|
+
parse() {
|
|
36
|
+
const protoContent = readFileSync(this.protoPath, 'utf-8');
|
|
37
|
+
this.root = parse(protoContent, {
|
|
38
|
+
keepCase: this.options.parser.keepCase
|
|
39
|
+
}).root;
|
|
40
|
+
this.extractTypesAndEnums(this.root);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Extract types and enums from the parsed proto root
|
|
44
|
+
*/
|
|
45
|
+
extractTypesAndEnums(root) {
|
|
46
|
+
const exclude = new Set(this.options.exclude);
|
|
47
|
+
// Helper to recursively extract from nested structures
|
|
48
|
+
const extract = (obj, prefix = '') => {
|
|
49
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
50
|
+
if (!value || typeof value !== 'object')
|
|
51
|
+
continue;
|
|
52
|
+
const fullName = prefix ? `${prefix}.${key}` : key;
|
|
53
|
+
// Skip excluded types
|
|
54
|
+
if (exclude.has(key) || exclude.has(fullName))
|
|
55
|
+
continue;
|
|
56
|
+
const item = value;
|
|
57
|
+
if (isProtoType(item)) {
|
|
58
|
+
// It's a message type
|
|
59
|
+
this.types.push({
|
|
60
|
+
name: key,
|
|
61
|
+
fields: this.normalizeFields(item.fields),
|
|
62
|
+
nested: item.nested
|
|
63
|
+
});
|
|
64
|
+
// Extract nested types
|
|
65
|
+
if (item.nested) {
|
|
66
|
+
extract(item.nested, fullName);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else if (isProtoEnum(item)) {
|
|
70
|
+
// It's an enum
|
|
71
|
+
this.enums.push({
|
|
72
|
+
name: key,
|
|
73
|
+
values: item.values
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
// Start extraction from root.nested
|
|
79
|
+
if (root.nested) {
|
|
80
|
+
extract(root.nested);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Normalize fields to ensure consistent structure
|
|
85
|
+
*/
|
|
86
|
+
normalizeFields(fields) {
|
|
87
|
+
if (!fields)
|
|
88
|
+
return {};
|
|
89
|
+
const normalized = {};
|
|
90
|
+
for (const [key, field] of Object.entries(fields)) {
|
|
91
|
+
normalized[key] = {
|
|
92
|
+
name: field.name || key,
|
|
93
|
+
type: field.type,
|
|
94
|
+
rule: field.rule
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
return normalized;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Generate all output files
|
|
101
|
+
*/
|
|
102
|
+
write() {
|
|
103
|
+
if (!this.root) {
|
|
104
|
+
this.parse();
|
|
105
|
+
}
|
|
106
|
+
const { outDir } = this.options;
|
|
107
|
+
mkdirSync(outDir, { recursive: true });
|
|
108
|
+
if (this.options.enums.enabled) {
|
|
109
|
+
this.writeEnums();
|
|
110
|
+
}
|
|
111
|
+
if (this.options.types.enabled) {
|
|
112
|
+
this.writeTypes();
|
|
113
|
+
}
|
|
114
|
+
if (this.options.utils.astHelpers.enabled) {
|
|
115
|
+
this.writeAstHelpers();
|
|
116
|
+
}
|
|
117
|
+
if (this.options.deparser.enabled) {
|
|
118
|
+
this.writeDeparser();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Write enum definitions to file
|
|
123
|
+
*/
|
|
124
|
+
writeEnums() {
|
|
125
|
+
const { outDir, enums: enumOptions } = this.options;
|
|
126
|
+
const filePath = join(outDir, enumOptions.filename);
|
|
127
|
+
const statements = [];
|
|
128
|
+
for (const enumNode of this.enums) {
|
|
129
|
+
if (enumOptions.enumsAsTypeUnion) {
|
|
130
|
+
statements.push(convertEnumToTsUnionType(enumNode));
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
statements.push(convertEnumToTsEnumDeclaration(enumNode));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const code = this.generateCode(statements);
|
|
137
|
+
writeFileSync(filePath, code, 'utf-8');
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Write type definitions to file
|
|
141
|
+
*/
|
|
142
|
+
writeTypes() {
|
|
143
|
+
const { outDir, types: typeOptions, enums: enumOptions } = this.options;
|
|
144
|
+
const filePath = join(outDir, typeOptions.filename);
|
|
145
|
+
const statements = [];
|
|
146
|
+
// Add enum imports if there are enums
|
|
147
|
+
if (this.enums.length > 0 && enumOptions.enabled) {
|
|
148
|
+
const enumImport = generateEnumImports(this.enums, typeOptions.enumsSource);
|
|
149
|
+
statements.push(enumImport);
|
|
150
|
+
}
|
|
151
|
+
// Generate Node union type
|
|
152
|
+
if (this.types.length > 0) {
|
|
153
|
+
statements.push(generateNodeUnionType(this.types));
|
|
154
|
+
}
|
|
155
|
+
// Generate interfaces for each type
|
|
156
|
+
for (const type of this.types) {
|
|
157
|
+
statements.push(convertTypeToTsInterface(type, this.options));
|
|
158
|
+
}
|
|
159
|
+
const code = this.generateCode(statements);
|
|
160
|
+
writeFileSync(filePath, code, 'utf-8');
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Write AST helper functions to file
|
|
164
|
+
*/
|
|
165
|
+
writeAstHelpers() {
|
|
166
|
+
const { outDir, utils } = this.options;
|
|
167
|
+
const filePath = join(outDir, utils.astHelpers.filename);
|
|
168
|
+
const statements = [];
|
|
169
|
+
// Add nested-obj import
|
|
170
|
+
statements.push(createDefaultImport('_o', 'nested-obj'));
|
|
171
|
+
// Add type imports
|
|
172
|
+
if (this.types.length > 0) {
|
|
173
|
+
statements.push(generateTypeImportSpecifiers(this.types, this.options));
|
|
174
|
+
}
|
|
175
|
+
// Generate helper methods
|
|
176
|
+
if (this.types.length > 0) {
|
|
177
|
+
statements.push(generateAstHelperMethods(this.types));
|
|
178
|
+
}
|
|
179
|
+
const code = this.generateCode(statements);
|
|
180
|
+
writeFileSync(filePath, code, 'utf-8');
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Write deparser module to file
|
|
184
|
+
*/
|
|
185
|
+
writeDeparser() {
|
|
186
|
+
const { outDir, deparser: deparserOptions } = this.options;
|
|
187
|
+
const filePath = join(outDir, deparserOptions.filename);
|
|
188
|
+
const code = generateDeparserCode();
|
|
189
|
+
writeFileSync(filePath, code, 'utf-8');
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Generate TypeScript code from AST statements
|
|
193
|
+
*/
|
|
194
|
+
generateCode(statements) {
|
|
195
|
+
const program = t.program(statements);
|
|
196
|
+
const { code } = generate(program, { comments: true });
|
|
197
|
+
return `${GENERATED_HEADER}\n${code}\n`;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Get the parsed types
|
|
201
|
+
*/
|
|
202
|
+
getTypes() {
|
|
203
|
+
return this.types;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Get the parsed enums
|
|
207
|
+
*/
|
|
208
|
+
getEnums() {
|
|
209
|
+
return this.enums;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import * as t from '@babel/types';
|
|
2
|
+
import generate from '@babel/generator';
|
|
3
|
+
import { mkdirSync, writeFileSync } from 'fs';
|
|
4
|
+
import { dirname } from 'path';
|
|
5
|
+
/**
|
|
6
|
+
* Convert a string to camelCase
|
|
7
|
+
*/
|
|
8
|
+
export function toCamelCase(str) {
|
|
9
|
+
return str.charAt(0).toLowerCase() + str.slice(1);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Convert a string to PascalCase
|
|
13
|
+
*/
|
|
14
|
+
export function toPascalCase(str) {
|
|
15
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Get the field name, handling protobuf naming conventions
|
|
19
|
+
*/
|
|
20
|
+
export function getFieldName(field, fallback) {
|
|
21
|
+
return field.name || fallback;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Convert Babel AST nodes to TypeScript code string
|
|
25
|
+
*/
|
|
26
|
+
export function convertAstToCode(nodes) {
|
|
27
|
+
const program = t.program(Array.isArray(nodes) ? nodes : [nodes]);
|
|
28
|
+
const { code } = generate(program, {
|
|
29
|
+
comments: true
|
|
30
|
+
});
|
|
31
|
+
return code;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Create a named import statement
|
|
35
|
+
*/
|
|
36
|
+
export function createNamedImport(names, source) {
|
|
37
|
+
const specifiers = names.map((name) => t.importSpecifier(t.identifier(name), t.identifier(name)));
|
|
38
|
+
return t.importDeclaration(specifiers, t.stringLiteral(source));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Create a default import statement
|
|
42
|
+
*/
|
|
43
|
+
export function createDefaultImport(name, source) {
|
|
44
|
+
return t.importDeclaration([t.importDefaultSpecifier(t.identifier(name))], t.stringLiteral(source));
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Write content to a file, creating directories as needed
|
|
48
|
+
*/
|
|
49
|
+
export function writeFileToDisk(filePath, content, _options) {
|
|
50
|
+
const dir = dirname(filePath);
|
|
51
|
+
mkdirSync(dir, { recursive: true });
|
|
52
|
+
writeFileSync(filePath, content, 'utf-8');
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Clone a protobuf node and add a name property
|
|
56
|
+
*/
|
|
57
|
+
export function cloneAndNameNode(node, name) {
|
|
58
|
+
return { ...node, name };
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Strip file extension from a path
|
|
62
|
+
*/
|
|
63
|
+
export function stripExtension(filename) {
|
|
64
|
+
return filename.replace(/\.[^/.]+$/, '');
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Ensure a filename has the correct extension
|
|
68
|
+
*/
|
|
69
|
+
export function ensureExtension(filename, ext) {
|
|
70
|
+
if (!ext.startsWith('.')) {
|
|
71
|
+
ext = '.' + ext;
|
|
72
|
+
}
|
|
73
|
+
if (filename.endsWith(ext)) {
|
|
74
|
+
return filename;
|
|
75
|
+
}
|
|
76
|
+
// Remove any existing extension and add the new one
|
|
77
|
+
const base = stripExtension(filename);
|
|
78
|
+
return base + ext;
|
|
79
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { CelProtoParser } from './parser';
|
|
2
|
+
export { CelProtoParserOptions, ResolvedCelProtoParserOptions, getOptionsWithDefaults } from './options';
|
|
3
|
+
export { deparse, PRECEDENCE, DeparserOptions, Expr, Constant, Ident, Select, Call, CreateList, CreateStruct, Entry, Comprehension } from './deparser';
|
|
4
|
+
export { convertToProtoExpr, MarcAstNode } from './converter';
|
|
5
|
+
export * from './ast';
|
|
6
|
+
export * from './utils';
|
package/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.convertToProtoExpr = exports.PRECEDENCE = exports.deparse = exports.getOptionsWithDefaults = exports.CelProtoParser = void 0;
|
|
18
|
+
var parser_1 = require("./parser");
|
|
19
|
+
Object.defineProperty(exports, "CelProtoParser", { enumerable: true, get: function () { return parser_1.CelProtoParser; } });
|
|
20
|
+
var options_1 = require("./options");
|
|
21
|
+
Object.defineProperty(exports, "getOptionsWithDefaults", { enumerable: true, get: function () { return options_1.getOptionsWithDefaults; } });
|
|
22
|
+
var deparser_1 = require("./deparser");
|
|
23
|
+
Object.defineProperty(exports, "deparse", { enumerable: true, get: function () { return deparser_1.deparse; } });
|
|
24
|
+
Object.defineProperty(exports, "PRECEDENCE", { enumerable: true, get: function () { return deparser_1.PRECEDENCE; } });
|
|
25
|
+
var converter_1 = require("./converter");
|
|
26
|
+
Object.defineProperty(exports, "convertToProtoExpr", { enumerable: true, get: function () { return converter_1.convertToProtoExpr; } });
|
|
27
|
+
__exportStar(require("./ast"), exports);
|
|
28
|
+
__exportStar(require("./utils"), exports);
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getOptionsWithDefaults = getOptionsWithDefaults;
|
|
4
|
+
const defaultOptions = {
|
|
5
|
+
outDir: process.cwd() + '/out',
|
|
6
|
+
exclude: [],
|
|
7
|
+
parser: {
|
|
8
|
+
keepCase: false
|
|
9
|
+
},
|
|
10
|
+
types: {
|
|
11
|
+
enabled: true,
|
|
12
|
+
filename: 'types.ts',
|
|
13
|
+
optionalFields: true,
|
|
14
|
+
enumsSource: './enums'
|
|
15
|
+
},
|
|
16
|
+
enums: {
|
|
17
|
+
enabled: true,
|
|
18
|
+
filename: 'enums.ts',
|
|
19
|
+
enumsAsTypeUnion: true
|
|
20
|
+
},
|
|
21
|
+
utils: {
|
|
22
|
+
astHelpers: {
|
|
23
|
+
enabled: true,
|
|
24
|
+
filename: 'asts.ts',
|
|
25
|
+
typesSource: './types'
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
deparser: {
|
|
29
|
+
enabled: true,
|
|
30
|
+
filename: 'deparser.ts',
|
|
31
|
+
typesSource: './types'
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
function getOptionsWithDefaults(options) {
|
|
35
|
+
if (!options) {
|
|
36
|
+
return { ...defaultOptions };
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
outDir: options.outDir ?? defaultOptions.outDir,
|
|
40
|
+
exclude: options.exclude ?? defaultOptions.exclude,
|
|
41
|
+
parser: {
|
|
42
|
+
keepCase: options.parser?.keepCase ?? defaultOptions.parser.keepCase
|
|
43
|
+
},
|
|
44
|
+
types: {
|
|
45
|
+
enabled: options.types?.enabled ?? defaultOptions.types.enabled,
|
|
46
|
+
filename: options.types?.filename ?? defaultOptions.types.filename,
|
|
47
|
+
optionalFields: options.types?.optionalFields ?? defaultOptions.types.optionalFields,
|
|
48
|
+
enumsSource: options.types?.enumsSource ?? defaultOptions.types.enumsSource
|
|
49
|
+
},
|
|
50
|
+
enums: {
|
|
51
|
+
enabled: options.enums?.enabled ?? defaultOptions.enums.enabled,
|
|
52
|
+
filename: options.enums?.filename ?? defaultOptions.enums.filename,
|
|
53
|
+
enumsAsTypeUnion: options.enums?.enumsAsTypeUnion ?? defaultOptions.enums.enumsAsTypeUnion
|
|
54
|
+
},
|
|
55
|
+
utils: {
|
|
56
|
+
astHelpers: {
|
|
57
|
+
enabled: options.utils?.astHelpers?.enabled ??
|
|
58
|
+
defaultOptions.utils.astHelpers.enabled,
|
|
59
|
+
filename: options.utils?.astHelpers?.filename ??
|
|
60
|
+
defaultOptions.utils.astHelpers.filename,
|
|
61
|
+
typesSource: options.utils?.astHelpers?.typesSource ??
|
|
62
|
+
defaultOptions.utils.astHelpers.typesSource
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
deparser: {
|
|
66
|
+
enabled: options.deparser?.enabled ?? defaultOptions.deparser.enabled,
|
|
67
|
+
filename: options.deparser?.filename ?? defaultOptions.deparser.filename,
|
|
68
|
+
typesSource: options.deparser?.typesSource ?? defaultOptions.deparser.typesSource
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
package/options/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./types"), exports);
|
|
18
|
+
__exportStar(require("./defaults"), exports);
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for CEL proto parser
|
|
3
|
+
*/
|
|
4
|
+
export interface CelProtoParserOptions {
|
|
5
|
+
/** Output directory for generated files */
|
|
6
|
+
outDir: string;
|
|
7
|
+
/** List of type or enum names to exclude during processing */
|
|
8
|
+
exclude?: string[];
|
|
9
|
+
/** Parser options for protobufjs */
|
|
10
|
+
parser?: {
|
|
11
|
+
keepCase?: boolean;
|
|
12
|
+
};
|
|
13
|
+
/** Type generation options */
|
|
14
|
+
types?: {
|
|
15
|
+
/** Whether to generate TypeScript interfaces */
|
|
16
|
+
enabled?: boolean;
|
|
17
|
+
/** Filename for generated types */
|
|
18
|
+
filename?: string;
|
|
19
|
+
/** Whether all fields should be optional */
|
|
20
|
+
optionalFields?: boolean;
|
|
21
|
+
/** Source path for enum imports */
|
|
22
|
+
enumsSource?: string;
|
|
23
|
+
};
|
|
24
|
+
/** Enum generation options */
|
|
25
|
+
enums?: {
|
|
26
|
+
/** Whether to generate TypeScript enums */
|
|
27
|
+
enabled?: boolean;
|
|
28
|
+
/** Filename for generated enums */
|
|
29
|
+
filename?: string;
|
|
30
|
+
/** Use string unions instead of enums */
|
|
31
|
+
enumsAsTypeUnion?: boolean;
|
|
32
|
+
};
|
|
33
|
+
/** Utility generation options */
|
|
34
|
+
utils?: {
|
|
35
|
+
/** AST helper generation options */
|
|
36
|
+
astHelpers?: {
|
|
37
|
+
/** Whether to generate AST helpers */
|
|
38
|
+
enabled?: boolean;
|
|
39
|
+
/** Filename for generated helpers */
|
|
40
|
+
filename?: string;
|
|
41
|
+
/** Source path for type imports */
|
|
42
|
+
typesSource?: string;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
/** Deparser generation options */
|
|
46
|
+
deparser?: {
|
|
47
|
+
/** Whether to generate deparser */
|
|
48
|
+
enabled?: boolean;
|
|
49
|
+
/** Filename for generated deparser */
|
|
50
|
+
filename?: string;
|
|
51
|
+
/** Source path for type imports */
|
|
52
|
+
typesSource?: string;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export interface ResolvedCelProtoParserOptions {
|
|
56
|
+
outDir: string;
|
|
57
|
+
exclude: string[];
|
|
58
|
+
parser: {
|
|
59
|
+
keepCase: boolean;
|
|
60
|
+
};
|
|
61
|
+
types: {
|
|
62
|
+
enabled: boolean;
|
|
63
|
+
filename: string;
|
|
64
|
+
optionalFields: boolean;
|
|
65
|
+
enumsSource: string;
|
|
66
|
+
};
|
|
67
|
+
enums: {
|
|
68
|
+
enabled: boolean;
|
|
69
|
+
filename: string;
|
|
70
|
+
enumsAsTypeUnion: boolean;
|
|
71
|
+
};
|
|
72
|
+
utils: {
|
|
73
|
+
astHelpers: {
|
|
74
|
+
enabled: boolean;
|
|
75
|
+
filename: string;
|
|
76
|
+
typesSource: string;
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
deparser: {
|
|
80
|
+
enabled: boolean;
|
|
81
|
+
filename: string;
|
|
82
|
+
typesSource: string;
|
|
83
|
+
};
|
|
84
|
+
}
|
package/options/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cel-proto-parser",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "CEL (Common Expression Language) proto parser and deparser for TypeScript",
|
|
5
|
+
"author": "Constructive <developers@constructive.io>",
|
|
6
|
+
"homepage": "https://github.com/constructive-io/dev-utils",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"module": "esm/index.js",
|
|
10
|
+
"types": "index.d.ts",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"copy": "makage assets",
|
|
17
|
+
"clean": "makage clean",
|
|
18
|
+
"prepublishOnly": "npm run build",
|
|
19
|
+
"build": "makage build",
|
|
20
|
+
"lint": "eslint . --fix",
|
|
21
|
+
"test": "jest",
|
|
22
|
+
"test:watch": "jest --watch",
|
|
23
|
+
"test:debug": "node --inspect node_modules/.bin/jest --runInBand",
|
|
24
|
+
"generate": "ts-node scripts/generate.ts"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/constructive-io/dev-utils"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"cel",
|
|
32
|
+
"common-expression-language",
|
|
33
|
+
"parser",
|
|
34
|
+
"deparser",
|
|
35
|
+
"ast",
|
|
36
|
+
"protobuf"
|
|
37
|
+
],
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/constructive-io/dev-utils/issues"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@babel/generator": "^7.26.9",
|
|
43
|
+
"@babel/types": "^7.26.9",
|
|
44
|
+
"@launchql/protobufjs": "^7.2.6",
|
|
45
|
+
"@marcbachmann/cel-js": "^7.1.0",
|
|
46
|
+
"nested-obj": "0.1.10",
|
|
47
|
+
"strfy-js": "3.1.10"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@types/babel__generator": "^7.6.8",
|
|
51
|
+
"makage": "0.1.8",
|
|
52
|
+
"ts-node": "^10.9.2"
|
|
53
|
+
},
|
|
54
|
+
"gitHead": "362a5e9b960edc67fb906bd036d281250c812cc0"
|
|
55
|
+
}
|
package/parser.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { CelProtoParserOptions } from './options';
|
|
2
|
+
interface ProtoField {
|
|
3
|
+
name: string;
|
|
4
|
+
type: string;
|
|
5
|
+
rule?: string;
|
|
6
|
+
}
|
|
7
|
+
interface ProtoType {
|
|
8
|
+
name: string;
|
|
9
|
+
fields: Record<string, ProtoField>;
|
|
10
|
+
nested?: Record<string, ProtoType | ProtoEnum>;
|
|
11
|
+
}
|
|
12
|
+
interface ProtoEnum {
|
|
13
|
+
name: string;
|
|
14
|
+
values: Record<string, number>;
|
|
15
|
+
}
|
|
16
|
+
export declare class CelProtoParser {
|
|
17
|
+
private protoPath;
|
|
18
|
+
private options;
|
|
19
|
+
private root;
|
|
20
|
+
private types;
|
|
21
|
+
private enums;
|
|
22
|
+
constructor(protoPath: string, options?: CelProtoParserOptions);
|
|
23
|
+
/**
|
|
24
|
+
* Parse the proto file and extract types and enums
|
|
25
|
+
*/
|
|
26
|
+
parse(): void;
|
|
27
|
+
/**
|
|
28
|
+
* Extract types and enums from the parsed proto root
|
|
29
|
+
*/
|
|
30
|
+
private extractTypesAndEnums;
|
|
31
|
+
/**
|
|
32
|
+
* Normalize fields to ensure consistent structure
|
|
33
|
+
*/
|
|
34
|
+
private normalizeFields;
|
|
35
|
+
/**
|
|
36
|
+
* Generate all output files
|
|
37
|
+
*/
|
|
38
|
+
write(): void;
|
|
39
|
+
/**
|
|
40
|
+
* Write enum definitions to file
|
|
41
|
+
*/
|
|
42
|
+
private writeEnums;
|
|
43
|
+
/**
|
|
44
|
+
* Write type definitions to file
|
|
45
|
+
*/
|
|
46
|
+
private writeTypes;
|
|
47
|
+
/**
|
|
48
|
+
* Write AST helper functions to file
|
|
49
|
+
*/
|
|
50
|
+
private writeAstHelpers;
|
|
51
|
+
/**
|
|
52
|
+
* Write deparser module to file
|
|
53
|
+
*/
|
|
54
|
+
private writeDeparser;
|
|
55
|
+
/**
|
|
56
|
+
* Generate TypeScript code from AST statements
|
|
57
|
+
*/
|
|
58
|
+
private generateCode;
|
|
59
|
+
/**
|
|
60
|
+
* Get the parsed types
|
|
61
|
+
*/
|
|
62
|
+
getTypes(): ProtoType[];
|
|
63
|
+
/**
|
|
64
|
+
* Get the parsed enums
|
|
65
|
+
*/
|
|
66
|
+
getEnums(): ProtoEnum[];
|
|
67
|
+
}
|
|
68
|
+
export {};
|