diff --git a/README.md b/README.md index 947b85e..159ed81 100644 --- a/README.md +++ b/README.md @@ -83,16 +83,20 @@ pg-proto-parser --inFile pg_query.proto --outDir out This table describes the options available for `PgProtoParserOptions`, their functionality, and default values. -| Option | Description | Default Value | -|-------------------------|---------------------------------------------------------------------------------------------------|----------------------| -| `includeEnumsJSON` | Whether to generate JSON files mapping enum names to integer values and vice versa. | `true` | -| `includeTypes` | Whether to generate TypeScript interfaces for protobuf messages. | `true` | -| `includeUtils` | Whether to generate TypeScript utility functions for enums. | `true` | -| `outDir` | The directory where the generated files will be saved. | `process.cwd() + "/out"` | -| `parser.keepCase` | Keeps field casing as defined in the protobuf file. If false, fields will be converted to camelCase. | `true` | -| `parser.alternateCommentMode` | Use alternate comment mode. Useful if the default parsing of comments is problematic. | `true` | -| `parser.preferTrailingComment` | Give preference to trailing comments during parsing. | `false` | -| `removeUndefinedAt0` | Removes the initial `UNDEFINED` enum entry and adjusts the subsequent values by decrementing them. | `true` | +| Option | Description | Default Value | +|-------------------------------|---------------------------------------------------------------------------------------------------------------------------------|--------------------------| +| `includeEnums` | Outputs TypeScript enum types for the PostgreSQL enums. | `true` | +| `includeEnumsJSON` | Whether to generate JSON files mapping enum names to integer values and vice versa. | `true` | +| `includeEnumTypeUnion` | Uses strings to define enum types as specified for the fields of each proto message type (the PostgreSQL node types' fields type). | `true` | +| `includeHeader` | Includes a header at the top of generated TypeScript files to avoid manual manipulation which could cause issues in CI/CD pipelines. | `true` | +| `includeTypes` | Whether to generate TypeScript interfaces for protobuf messages. | `true` | +| `includeUtils` | Whether to generate TypeScript utility functions for enums. | `true` | +| `optionalFields` | Generates TypeScript interfaces with optional fields mapping to the PostgreSQL node types' fields; sets all fields to optional. | `true` | +| `outDir` | The directory where the generated files will be saved. | `process.cwd() + "/out"` | +| `parser.alternateCommentMode` | Use alternate comment mode. Useful if the default parsing of comments is problematic. | `true` | +| `parser.keepCase` | Keeps field casing as defined in the protobuf file. If false, fields will be converted to camelCase. | `true` | +| `parser.preferTrailingComment`| Give preference to trailing comments during parsing. | `false` | +| `removeUndefinedAt0` | Removes the initial `UNDEFINED` enum entry and adjusts the subsequent values by decrementing them. | `true` | Each of these options can be set when initializing the `PgProtoParser` to customize its behavior and output. diff --git a/packages/cli/.npmignore b/packages/cli/.npmignore index b8f6a37..b12f994 100644 --- a/packages/cli/.npmignore +++ b/packages/cli/.npmignore @@ -30,4 +30,3 @@ __tests__ .travis.yml .vscode CHANGELOG.md -package.json diff --git a/packages/cli/README.md b/packages/cli/README.md index 3178bd5..480f80d 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -57,20 +57,37 @@ pg-proto-parser --inFile --outDir ## Options ```bash -pg-proto-parser --inFile --outDir [--includeEnumsJSON] [--includeTypes] [--includeUtils] [--keepCase] [--alternateCommentMode] [--preferTrailingComment] +pg-proto-parser --inFile \ + --outDir \ + [--alternateCommentMode] \ + [--includeEnums] \ + [--includeEnumsJSON] \ + [--includeEnumTypeUnion] \ + [--includeHeader] \ + [--includeTypes] \ + [--includeUtils] \ + [--keepCase] \ + [--optionalFields] \ + [--preferTrailingComment] \ + [--removeUndefinedAt0] + ``` -| Option | Description | Default Value | -|-------------------------|---------------------------------------------------------------------------------------------------|----------------------| -| `--inFile` | Path to the `.proto` file to be parsed. | *Required* | -| `--outDir` | Directory to save the generated TypeScript files. | *Required* | -| `--includeEnumsJSON` | Generate JSON files mapping enum names to integer values and vice versa. | `true` | -| `--includeTypes` | Generate TypeScript interfaces for protobuf messages. | `true` | -| `--includeUtils` | Generate TypeScript utility functions for enums. | `true` | -| `--keepCase` | Keeps field casing as defined in the protobuf file. If false, fields will be converted to camelCase. | `true` | -| `--alternateCommentMode`| Use alternate comment mode. Useful if the default parsing of comments is problematic. | `true` | -| `--preferTrailingComment`| Give preference to trailing comments during parsing. | `false` | -| `--removeUndefinedAt0` | Removes the initial `UNDEFINED` enum entry and adjusts the subsequent values by decrementing them. | `true` | +| Option | Description | Default Value | +|-------------------------------|---------------------------------------------------------------------------------------------------------------------------------|--------------------------| +| `--inFile` | Path to the `.proto` file to be parsed. | *Required* | +| `--outDir` | Directory to save the generated TypeScript files. | *Required* | +| `--alternateCommentMode` | Use alternate comment mode. Useful if the default parsing of comments is problematic. | `true` | +| `--includeEnums` | Outputs TypeScript enum types for the PostgreSQL enums. | `true` | +| `--includeEnumsJSON` | Generate JSON files mapping enum names to integer values and vice versa. | `true` | +| `--includeEnumTypeUnion` | Uses strings to define enum types as specified for the fields of each proto message type (the PostgreSQL node types' fields type). | `true` | +| `--includeHeader` | Includes a header at the top of generated TypeScript files to avoid manual manipulation which could cause issues in CI/CD pipelines. | `true` | +| `--includeTypes` | Generate TypeScript interfaces for protobuf messages. | `true` | +| `--includeUtils` | Generate TypeScript utility functions for enums. | `true` | +| `--keepCase` | Keeps field casing as defined in the protobuf file. If false, fields will be converted to camelCase. | `true` | +| `--optionalFields` | Generates TypeScript interfaces with optional fields mapping to the PostgreSQL node types' fields; sets all fields to optional. | `true` | +| `--preferTrailingComment` | Give preference to trailing comments during parsing. | `false` | +| `--removeUndefinedAt0` | Removes the initial `UNDEFINED` enum entry and adjusts the subsequent values by decrementing them. | `true` | To explicitly set a boolean option to false, prepend the option with `--no-`. For example, to disable JSON enum mapping, use `--no-includeEnumsJSON`. @@ -79,8 +96,11 @@ Note: Boolean flags default to true when specified without a value. To set them ## Related -- [launchql/pgsql-parser](https://github.com/launchql/pgsql-parser): A PostgreSQL parser that interprets and converts SQL syntax into a structured format. -- [launchql/libpg-query-node](https://github.com/launchql/libpg-query-node): Node.js bindings for the libpg_query library, allowing parsing of PostgreSQL queries into parse trees. +* [launchql/pgsql-parser](https://github.com/launchql/pgsql-parser): A node.js PostgreSQL parser/deparser that interprets and converts PostgresSQL syntax. +* [launchql/libpg-query-node](https://github.com/launchql/libpg-query-node): Node.js bindings for the libpg_query library, allowing parsing of PostgreSQL queries into parse trees. +* [@pgsql/enums](https://github.com/launchql/pgsql-parser/tree/main/packages/enums): Provides PostgreSQL AST enums in TypeScript, enhancing type safety and usability in projects interacting with PostgreSQL AST nodes. +* [@pgsql/types](https://github.com/launchql/pgsql-parser/tree/main/packages/types): Offers TypeScript type definitions for PostgreSQL AST nodes, facilitating type-safe construction, analysis, and manipulation of ASTs. +* [@pgsql/utils](https://github.com/launchql/pgsql-parser/tree/main/packages/utils): A utility library for working with PostgreSQL AST node enumerations in a type-safe way, easing enum name and value conversions. ## Disclaimer diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index d885bda..92e27dc 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -1,28 +1,67 @@ +import pkg from '../package.json'; import { PgProtoParser, PgProtoParserOptions } from 'pg-proto-parser'; +export const help = (): void => { + console.log(` + Usage: + + pg-proto-parser --inFile --outDir + [--alternateCommentMode] [--includeEnums] [--includeEnumsJSON] + [--includeEnumTypeUnion] [--includeHeader] [--includeTypes] + [--includeUtils] [--keepCase] [--optionalFields] + [--preferTrailingComment] [--removeUndefinedAt0] + + Options: + + --help Show this help message and exit. + --inFile Path to the .proto file to be parsed. + --outDir Directory to save the generated TypeScript files. + --alternateCommentMode Use alternate comment mode for parsing comments. + --includeEnums Generate TypeScript enum types for PostgreSQL enums. + --includeEnumsJSON Generate JSON files mapping enum names to values. + --includeEnumTypeUnion Generate TypeScript unions from enum types. + --includeHeader Include a header in the generated TypeScript files. + --includeTypes Generate TypeScript interfaces for protobuf messages. + --includeUtils Generate TypeScript utility functions for enums. + --keepCase Keep field casing as defined in the protobuf file. + --optionalFields Generate TypeScript interfaces with optional fields. + --preferTrailingComment Prefer trailing comments during parsing. + --removeUndefinedAt0 Remove the 'UNDEFINED' enum entry if it exists. + `); +} + export default async (argv) => { if (!argv.inFile || !argv.outDir) { console.log('inFile and outDir are required!'); - console.log('Usage:'); - console.log('pg-proto-parser --inFile input.proto --outDir out [--includeEnumsJSON] [--includeTypes] [--includeUtils]'); + help(); process.exit(1); } + if (argv.help || argv.h) { + help(); + process.exit(0); + } + + if (argv.version || argv.v) { + console.log(`Version: ${pkg.version}`); + process.exit(0); + } + const options: PgProtoParserOptions = { outDir: argv.outDir, - includeEnumsJSON: argv.hasOwnProperty('includeEnumsJSON') ? argv.includeEnumsJSON : true, - includeTypes: argv.hasOwnProperty('includeTypes') ? argv.includeTypes : true, - includeUtils: argv.hasOwnProperty('includeUtils') ? argv.includeUtils : true, - includeEnumTypeUnion: argv.hasOwnProperty('includeEnumTypeUnion') ? argv.includeEnumTypeUnion : true, - includeEnums: argv.hasOwnProperty('includeEnums') ? argv.includeEnums : true, - includeHeader: argv.hasOwnProperty('includeHeader') ? argv.includeHeader : true, - optionalFields: argv.hasOwnProperty('optionalFields') ? argv.optionalFields : true, - removeUndefinedAt0: argv.hasOwnProperty('removeUndefinedAt0') ? argv.removeUndefinedAt0 : true, + includeEnumsJSON: argv.includeEnumsJSON ?? true, + includeTypes: argv.includeTypes ?? true, + includeUtils: argv.includeUtils ?? true, + includeEnumTypeUnion: argv.includeEnumTypeUnion ?? true, + includeEnums: argv.includeEnums ?? true, + includeHeader: argv.includeHeader ?? true, + optionalFields: argv.optionalFields ?? true, + removeUndefinedAt0: argv.removeUndefinedAt0 ?? true, parser: { - keepCase: argv.hasOwnProperty('keepCase') ? argv.keepCase : true, - alternateCommentMode: argv.hasOwnProperty('alternateCommentMode') ? argv.alternateCommentMode : true, - preferTrailingComment: argv.hasOwnProperty('preferTrailingComment') ? argv.preferTrailingComment : false + keepCase: argv.keepCase ?? true, + alternateCommentMode: argv.alternateCommentMode ?? true, + preferTrailingComment: argv.preferTrailingComment ?? false } }; diff --git a/packages/parser/README.md b/packages/parser/README.md index a6698bd..4900c25 100644 --- a/packages/parser/README.md +++ b/packages/parser/README.md @@ -54,24 +54,30 @@ You can configure `pg-proto-parser` by passing different parameters to the `Prot This table describes the options available for `PgProtoParserOptions`, their functionality, and default values. -| Option | Description | Default Value | -|-------------------------|---------------------------------------------------------------------------------------------------|----------------------| -| `outDir` | The directory where the generated files will be saved. | `process.cwd() + "/out"` | -| `includeEnumsJSON` | Whether to generate JSON files mapping enum names to integer values and vice versa. | `true` | -| `includeTypes` | Whether to generate TypeScript interfaces for protobuf messages. | `true` | -| `includeUtils` | Whether to generate TypeScript utility functions for enums. | `true` | -| `parser.keepCase` | Keeps field casing as defined in the protobuf file. If false, fields will be converted to camelCase. | `true` | -| `parser.alternateCommentMode` | Use alternate comment mode. Useful if the default parsing of comments is problematic. | `true` | -| `parser.preferTrailingComment` | Give preference to trailing comments during parsing. | `false` | -| `removeUndefinedAt0` | Removes the initial `UNDEFINED` enum entry and adjusts the subsequent values by decrementing them. | `true` | +| Option | Description | Default Value | +|-------------------------------|---------------------------------------------------------------------------------------------------------------------------------|--------------------------| +| `includeEnums` | Outputs TypeScript enum types for the PostgreSQL enums. | `true` | +| `includeEnumsJSON` | Whether to generate JSON files mapping enum names to integer values and vice versa. | `true` | +| `includeEnumTypeUnion` | Uses strings to define enum types as specified for the fields of each proto message type (the PostgreSQL node types' fields type). | `true` | +| `includeHeader` | Includes a header at the top of generated TypeScript files to avoid manual manipulation which could cause issues in CI/CD pipelines. | `true` | +| `includeTypes` | Whether to generate TypeScript interfaces for protobuf messages. | `true` | +| `includeUtils` | Whether to generate TypeScript utility functions for enums. | `true` | +| `optionalFields` | Generates TypeScript interfaces with optional fields mapping to the PostgreSQL node types' fields; sets all fields to optional. | `true` | +| `outDir` | The directory where the generated files will be saved. | `process.cwd() + "/out"` | +| `parser.alternateCommentMode` | Use alternate comment mode. Useful if the default parsing of comments is problematic. | `true` | +| `parser.keepCase` | Keeps field casing as defined in the protobuf file. If false, fields will be converted to camelCase. | `true` | +| `parser.preferTrailingComment`| Give preference to trailing comments during parsing. | `false` | +| `removeUndefinedAt0` | Removes the initial `UNDEFINED` enum entry and adjusts the subsequent values by decrementing them. | `true` | Each of these options can be set when initializing the `PgProtoParser` to customize its behavior and output. - ## Related -- [launchql/pgsql-parser](https://github.com/launchql/pgsql-parser): A node.js PostgreSQL parser/deparser that interprets and converts PostgresSQL syntax. -- [launchql/libpg-query-node](https://github.com/launchql/libpg-query-node): Node.js bindings for the libpg_query library, allowing parsing of PostgreSQL queries into parse trees. +* [launchql/pgsql-parser](https://github.com/launchql/pgsql-parser): A node.js PostgreSQL parser/deparser that interprets and converts PostgresSQL syntax. +* [launchql/libpg-query-node](https://github.com/launchql/libpg-query-node): Node.js bindings for the libpg_query library, allowing parsing of PostgreSQL queries into parse trees. +* [@pgsql/enums](https://github.com/launchql/pgsql-parser/tree/main/packages/enums): Provides PostgreSQL AST enums in TypeScript, enhancing type safety and usability in projects interacting with PostgreSQL AST nodes. +* [@pgsql/types](https://github.com/launchql/pgsql-parser/tree/main/packages/types): Offers TypeScript type definitions for PostgreSQL AST nodes, facilitating type-safe construction, analysis, and manipulation of ASTs. +* [@pgsql/utils](https://github.com/launchql/pgsql-parser/tree/main/packages/utils): A utility library for working with PostgreSQL AST node enumerations in a type-safe way, easing enum name and value conversions. ## Disclaimer