diff --git a/packages/parser/src/ast/enums/enums.ts b/packages/parser/src/ast/enums/enums.ts index 0729e9a..e1e78f0 100644 --- a/packages/parser/src/ast/enums/enums.ts +++ b/packages/parser/src/ast/enums/enums.ts @@ -2,7 +2,7 @@ import { Enum } from '@launchql/protobufjs'; import * as t from '@babel/types'; import { createNamedImport } from '../../utils'; -export const transformEnumToAST = (enumData: Enum) => { +export const convertEnumToTsEnumDeclaration = (enumData: Enum) => { const members = Object.entries(enumData.values).map(([key, value]) => t.tsEnumMember(t.identifier(key), t.numericLiteral(value as number)) ); @@ -11,11 +11,11 @@ export const transformEnumToAST = (enumData: Enum) => { return t.exportNamedDeclaration(enumDeclaration); }; -export const buildEnumNamedImports = (enums: Enum[], source: string) => { +export const generateEnumImports = (enums: Enum[], source: string) => { return createNamedImport(enums.map(e=>e.name), source); }; -export const transformEnumToTypeUnionAST = (enumData: Enum) => { +export const convertEnumToTsUnionType = (enumData: Enum) => { const literals = Object.keys(enumData.values).map(key => t.tsLiteralType(t.stringLiteral(key)) ); @@ -31,7 +31,7 @@ export const transformEnumToTypeUnionAST = (enumData: Enum) => { return t.exportNamedDeclaration(typeAlias); }; -export const buildEnumValueFunctionAST = (enumData: Enum[]) => { +export const generateEnumValueFunctions = (enumData: Enum[]) => { // Create the union type for EnumType const enumTypeIdentifier = t.identifier('EnumType'); const enumTypeUnion = t.tsUnionType( diff --git a/packages/parser/src/ast/types/types.ts b/packages/parser/src/ast/types/types.ts index cae3787..f95107b 100644 --- a/packages/parser/src/ast/types/types.ts +++ b/packages/parser/src/ast/types/types.ts @@ -5,13 +5,13 @@ import { PgProtoParserOptions } from '../../options'; import { SPECIAL_TYPES } from '../../constants'; import { resolveTypeName } from './utils'; -export const buildTypeNamedImports = (types: Type[], source: string, suffix?: string) => { +export const generateTypeImports = (types: Type[], source: string, suffix?: string) => { return suffix ? createNamedImportAsSuffix(types.map(e => e.name), source, suffix) : createNamedImport(types.map(e => e.name), source); }; -export const createAstHelperMethodsAST = (types: Type[]): t.ExportDefaultDeclaration => { +export const generateAstHelperMethods = (types: Type[]): t.ExportDefaultDeclaration => { const creators = types.map((type: Type) => { const typeName = type.name; const param = t.identifier('_p'); @@ -89,7 +89,7 @@ export const createAstHelperMethodsAST = (types: Type[]): t.ExportDefaultDeclara } // special for Node -export const createNodeUnionTypeAST = (types: Type[]) => { +export const generateNodeUnionType = (types: Type[]) => { const unionTypeNames = types.map(type => t.tsTypeReference(t.identifier(type.name))); const unionTypeAlias = t.tsTypeAliasDeclaration( @@ -101,7 +101,7 @@ export const createNodeUnionTypeAST = (types: Type[]) => { return t.exportNamedDeclaration(unionTypeAlias, []); }; -const getTypeFieldsAsTS = ( +const extractTypeFieldsAsTsProperties = ( type: Type, options: PgProtoParserOptions ) => { @@ -126,12 +126,12 @@ const getTypeFieldsAsTS = ( return properties; } -export const transformTypeToTSInterface = ( +export const convertTypeToTsInterface = ( type: Type, options: PgProtoParserOptions ) => { const typeName = type.name; - const properties = getTypeFieldsAsTS(type, options); + const properties = extractTypeFieldsAsTsProperties(type, options); const interfaceDecl = t.tsInterfaceDeclaration( t.identifier(typeName), @@ -143,14 +143,14 @@ export const transformTypeToTSInterface = ( // Wrap the interface declaration in an export statement return t.exportNamedDeclaration(interfaceDecl, []); } -export const transformTypeToTSWrappedInterface = ( +export const convertTypeToWrappedTsInterface = ( type: Type, options: PgProtoParserOptions ) => { const typeName = type.name; - if (SPECIAL_TYPES.includes(typeName)) return transformTypeToTSInterface(type, options); + if (SPECIAL_TYPES.includes(typeName)) return convertTypeToTsInterface(type, options); - const properties = getTypeFieldsAsTS(type, options); + const properties = extractTypeFieldsAsTsProperties(type, options); const nestedInterfaceBody = t.tsInterfaceBody([ t.tsPropertySignature( @@ -169,7 +169,7 @@ export const transformTypeToTSWrappedInterface = ( return t.exportNamedDeclaration(interfaceDecl, []); }; -export const generateImportSpecifiersAST = (types: Type[], options: PgProtoParserOptions) => { +export const generateTypeImportSpecifiers = (types: Type[], options: PgProtoParserOptions) => { const importSpecifiers = types.map(type => t.importSpecifier(t.identifier(type.name), t.identifier(type.name)) ); diff --git a/packages/parser/src/store.ts b/packages/parser/src/store.ts index 8860ca9..d647356 100644 --- a/packages/parser/src/store.ts +++ b/packages/parser/src/store.ts @@ -1,9 +1,9 @@ import { Service, Type, Field, Enum, Namespace, ReflectionObject } from '@launchql/protobufjs'; -import { buildEnumNamedImports, createAstHelperMethodsAST, generateImportSpecifiersAST, buildEnumValueFunctionAST, transformEnumToTypeUnionAST, transformEnumToAST, createNodeUnionTypeAST, buildTypeNamedImports, transformTypeToTSInterface, transformTypeToTSWrappedInterface } from './ast'; +import { generateEnumImports, generateAstHelperMethods, generateTypeImportSpecifiers, generateEnumValueFunctions, convertEnumToTsUnionType, convertEnumToTsEnumDeclaration, generateNodeUnionType, convertTypeToTsInterface, convertTypeToWrappedTsInterface } from './ast'; import { generateEnum2IntJSON, generateEnum2StrJSON } from './ast/enums/enums-json'; import { sync as mkdirp } from 'mkdirp'; import { join } from 'path'; -import { defaultPgProtoParserOptions, getOptionsWithDefaults, PgProtoStoreOptions } from './options'; +import { getOptionsWithDefaults, PgProtoStoreOptions } from './options'; import { cloneAndNameNode, convertAstToCode, createDefaultImport, getUndefinedKey, hasUndefinedInitialValue, stripExtension, writeFileToDisk } from './utils'; import { nestedObjCode } from './inline-helpers'; import * as t from '@babel/types'; @@ -125,10 +125,10 @@ export class ProtoStore implements IProtoStore { if (this.options.types.enabled) { const typesToProcess = this.typesToProcess(); const enumsToProcess = this.enumsToProcess(); - const node = createNodeUnionTypeAST(typesToProcess); - const enumImports = buildEnumNamedImports(enumsToProcess, this.options.types.enumsSource); + const node = generateNodeUnionType(typesToProcess); + const enumImports = generateEnumImports(enumsToProcess, this.options.types.enumsSource); const types = typesToProcess.reduce((m, type) => { - return [...m, transformTypeToTSInterface(type, this.options)] + return [...m, convertTypeToTsInterface(type, this.options)] }, []); this.writeCodeToFile(this.options.types.filename, [ enumImports, @@ -141,13 +141,13 @@ export class ProtoStore implements IProtoStore { writeWrappedTypes() { if (this.options.types.wrapped.enabled) { const typesToProcess = this.typesToProcess(); - const enumImports = buildEnumNamedImports( + const enumImports = generateEnumImports( this.enumsToProcess(), this.options.types.wrapped.enumsSource ); - const node = createNodeUnionTypeAST(typesToProcess); + const node = generateNodeUnionType(typesToProcess); const types = typesToProcess.reduce((m, type) => { - return [...m, transformTypeToTSWrappedInterface(type, this.options)] + return [...m, convertTypeToWrappedTsInterface(type, this.options)] }, []); this.writeCodeToFile(this.options.types.wrapped.filename, [ enumImports, @@ -161,8 +161,8 @@ export class ProtoStore implements IProtoStore { if (this.options.enums.enabled) { this.writeCodeToFile(this.options.enums.filename, this.enumsToProcess().map(enm => this.options.enums.enumsAsTypeUnion ? - transformEnumToTypeUnionAST(enm) : - transformEnumToAST(enm) + convertEnumToTsUnionType(enm) : + convertEnumToTsEnumDeclaration(enm) ) ); } @@ -170,7 +170,7 @@ export class ProtoStore implements IProtoStore { writeUtilsEnums() { if (this.options.utils.enums.enabled) { - const code = convertAstToCode(buildEnumValueFunctionAST(this.enumsToProcess())); + const code = convertAstToCode(generateEnumValueFunctions(this.enumsToProcess())); this.writeFile(this.options.utils.enums.filename, code); } } @@ -184,8 +184,8 @@ export class ProtoStore implements IProtoStore { const typesToProcess = this.typesToProcess(); const code = convertAstToCode([ imports, - generateImportSpecifiersAST(typesToProcess, this.options), - createAstHelperMethodsAST(typesToProcess) + generateTypeImportSpecifiers(typesToProcess, this.options), + generateAstHelperMethods(typesToProcess) ]); this.writeFile(this.options.utils.astHelpers.filename, code); diff --git a/packages/parser/src/utils/imports.ts b/packages/parser/src/utils/babel.ts similarity index 81% rename from packages/parser/src/utils/imports.ts rename to packages/parser/src/utils/babel.ts index 9c77bef..bd32163 100644 --- a/packages/parser/src/utils/imports.ts +++ b/packages/parser/src/utils/babel.ts @@ -1,5 +1,12 @@ +import generate from '@babel/generator'; import * as t from '@babel/types'; +export const convertAstToCode = (body: any[]) => { + const ast = t.file(t.program(body)); + const { code } = generate(ast); + return code; +}; + export const createDefaultImport = (importName: string, source: string) => { return t.importDeclaration( [t.importDefaultSpecifier(t.identifier(importName))], diff --git a/packages/parser/src/utils/files.ts b/packages/parser/src/utils/files.ts deleted file mode 100644 index 19c1937..0000000 --- a/packages/parser/src/utils/files.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { extname, basename } from 'path'; - -export const stripExtension = (filename) => { - const extension = extname(filename); - return basename(filename, extension); -} \ No newline at end of file diff --git a/packages/parser/src/utils/generate.ts b/packages/parser/src/utils/generate.ts deleted file mode 100644 index c49d016..0000000 --- a/packages/parser/src/utils/generate.ts +++ /dev/null @@ -1,8 +0,0 @@ -import generate from '@babel/generator'; -import * as t from '@babel/types'; - -export const convertAstToCode = (body: any[]) => { - const ast = t.file(t.program(body)); - const { code } = generate(ast); - return code; -}; \ No newline at end of file diff --git a/packages/parser/src/utils/index.ts b/packages/parser/src/utils/index.ts index 02a80bb..90f2e32 100644 --- a/packages/parser/src/utils/index.ts +++ b/packages/parser/src/utils/index.ts @@ -1,7 +1,5 @@ -export * from './proto'; -export * from './files'; -export * from './imports'; -export * from './generate'; +export * from './utils'; +export * from './babel'; export * from './deps'; export * from './types'; export * from './meta'; \ No newline at end of file diff --git a/packages/parser/src/utils/proto.ts b/packages/parser/src/utils/utils.ts similarity index 90% rename from packages/parser/src/utils/proto.ts rename to packages/parser/src/utils/utils.ts index fbc1a49..777d06d 100644 --- a/packages/parser/src/utils/proto.ts +++ b/packages/parser/src/utils/utils.ts @@ -1,8 +1,8 @@ -import { Type, Enum, Field, ReflectionObject } from '@launchql/protobufjs'; +import { Enum, Field, ReflectionObject } from '@launchql/protobufjs'; import pkg from '../../package.json'; import { PgProtoParserOptions } from '../options'; import { writeFileSync } from 'fs'; -import { extname } from 'path'; +import { extname, basename } from 'path'; export const getUndefinedKey = (enumName) => { // Split the name into parts where a lowercase letter is followed by an uppercase letter @@ -64,4 +64,10 @@ export const getHeader = () => { export const writeFileToDisk = (path: string, contents: string, options: PgProtoParserOptions) => { const c = (options.includeHeader && extname(path) === '.ts') ? `${getHeader()}${contents}` : contents; writeFileSync(path, c); -} \ No newline at end of file +} + +export const stripExtension = (filename) => { + const extension = extname(filename); + return basename(filename, extension); +} + diff --git a/packages/parser/types/ast/enums/enums.d.ts b/packages/parser/types/ast/enums/enums.d.ts index 5682979..3646557 100644 --- a/packages/parser/types/ast/enums/enums.d.ts +++ b/packages/parser/types/ast/enums/enums.d.ts @@ -1,6 +1,6 @@ import { Enum } from '@launchql/protobufjs'; import * as t from '@babel/types'; -export declare const transformEnumToAST: (enumData: Enum) => t.ExportNamedDeclaration; -export declare const buildEnumNamedImports: (enums: Enum[], source: string) => t.ImportDeclaration; -export declare const transformEnumToTypeUnionAST: (enumData: Enum) => t.ExportNamedDeclaration; -export declare const buildEnumValueFunctionAST: (enumData: Enum[]) => t.ExportNamedDeclaration[]; +export declare const convertEnumToTsEnumDeclaration: (enumData: Enum) => t.ExportNamedDeclaration; +export declare const generateEnumImports: (enums: Enum[], source: string) => t.ImportDeclaration; +export declare const convertEnumToTsUnionType: (enumData: Enum) => t.ExportNamedDeclaration; +export declare const generateEnumValueFunctions: (enumData: Enum[]) => t.ExportNamedDeclaration[]; diff --git a/packages/parser/types/ast/types/types.d.ts b/packages/parser/types/ast/types/types.d.ts index cb411f5..f959b3f 100644 --- a/packages/parser/types/ast/types/types.d.ts +++ b/packages/parser/types/ast/types/types.d.ts @@ -1,9 +1,9 @@ import { Type } from '@launchql/protobufjs'; import * as t from '@babel/types'; import { PgProtoParserOptions } from '../../options'; -export declare const buildTypeNamedImports: (types: Type[], source: string, suffix?: string) => t.ImportDeclaration; -export declare const createAstHelperMethodsAST: (types: Type[]) => t.ExportDefaultDeclaration; -export declare const createNodeUnionTypeAST: (types: Type[]) => t.ExportNamedDeclaration; -export declare const transformTypeToTSInterface: (type: Type, options: PgProtoParserOptions) => t.ExportNamedDeclaration; -export declare const transformTypeToTSWrappedInterface: (type: Type, options: PgProtoParserOptions) => t.ExportNamedDeclaration; -export declare const generateImportSpecifiersAST: (types: Type[], options: PgProtoParserOptions) => t.ImportDeclaration; +export declare const generateTypeImports: (types: Type[], source: string, suffix?: string) => t.ImportDeclaration; +export declare const generateAstHelperMethods: (types: Type[]) => t.ExportDefaultDeclaration; +export declare const generateNodeUnionType: (types: Type[]) => t.ExportNamedDeclaration; +export declare const convertTypeToTsInterface: (type: Type, options: PgProtoParserOptions) => t.ExportNamedDeclaration; +export declare const convertTypeToWrappedTsInterface: (type: Type, options: PgProtoParserOptions) => t.ExportNamedDeclaration; +export declare const generateTypeImportSpecifiers: (types: Type[], options: PgProtoParserOptions) => t.ImportDeclaration;