-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from launchql/upgrade/cli
upgrade to use inquirerer
- Loading branch information
Showing
21 changed files
with
758 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { readAndParsePackageJson } from '../../package'; | ||
import { PgProtoParser, PgProtoParserOptions, getOptionsWithDefaults } from 'pg-proto-parser'; | ||
import o from 'nested-obj'; | ||
|
||
export const help = (): void => { | ||
console.log(` | ||
Usage: | ||
pg-proto-parser codegen --inFile <path-to-proto> --outDir <output-directory> | ||
[--enums] [--enumsJSON] [--typeUnion] | ||
[--header] [--types] [--utils] | ||
[--case] [--optional] [--removeUndefined] | ||
Options: | ||
--help, -h Show this help message and exit. | ||
--inFile Path to the .proto file to be parsed. | ||
--outDir Directory to save the generated TypeScript files. | ||
--enums Generate TypeScript enum types for PostgreSQL enums. | ||
--enumsJSON Generate JSON files mapping enum names to values. | ||
--typeUnion Generate TypeScript unions from enum types. | ||
--header Include a header in the generated TypeScript files. | ||
--types Generate TypeScript interfaces for protobuf messages. | ||
--utils Generate TypeScript utility functions for enums. | ||
--case Keep field casing as defined in the protobuf file. | ||
--optional Generate TypeScript interfaces with optional fields. | ||
--removeUndefined Remove the 'UNDEFINED' enum entry if it exists. | ||
--version, -v Show the version number and exit. | ||
`); | ||
} | ||
|
||
export default async (argv) => { | ||
|
||
if (argv.help || argv.h) { | ||
help(); | ||
process.exit(0); | ||
} | ||
|
||
if (argv.version || argv.v) { | ||
console.log(`Version: ${readAndParsePackageJson().version}`); | ||
process.exit(0); | ||
} | ||
|
||
if (!argv.inFile || !argv.outDir) { | ||
console.log('Input Error: inFile and outDir are required!'); | ||
help(); | ||
process.exit(1); | ||
} | ||
|
||
const options: PgProtoParserOptions = getOptionsWithDefaults({ | ||
outDir: argv.outDir | ||
}); | ||
|
||
// Setting nested options using objectPath | ||
o.set(options, 'enums.enabled', argv.enums); | ||
o.set(options, 'enums.enumsAsTypeUnion', argv.typeUnion); | ||
o.set(options, 'enums.json.enabled', argv.enumsJSON); | ||
o.set(options, 'enums.removeUndefinedAt0', argv.removeUndefined); | ||
o.set(options, 'includeHeader', argv.header); | ||
o.set(options, 'parser.keepCase', argv.case); | ||
o.set(options, 'types.enabled', argv.types); | ||
o.set(options, 'types.optionalFields', argv.optional); | ||
o.set(options, 'utils.astHelpers.enabled', argv.utils); | ||
|
||
const parser = new PgProtoParser(argv.inFile, options); | ||
// Generate TypeScript and JSON files | ||
await parser.write(); | ||
|
||
return argv; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { CLIOptions, Inquirerer } from 'inquirerer' | ||
import { ParsedArgs } from 'minimist'; | ||
import cli, { help } from './cli'; | ||
export const commands = async (argv: Partial<ParsedArgs>, prompter: Inquirerer, _options: CLIOptions) => { | ||
|
||
if (argv.help || argv.h) { | ||
help(); | ||
process.exit(0); | ||
} | ||
|
||
argv = await prompter.prompt(argv, [ | ||
{ | ||
type: 'text', | ||
name: 'inFile', | ||
message: 'provide inFile (./path/to/proto.proto):' | ||
}, | ||
{ | ||
type: 'text', | ||
name: 'outDir', | ||
message: 'provide outDir (./outputDir):' | ||
}, | ||
{ | ||
type: 'confirm', | ||
name: 'enums', | ||
message: 'Enable enums?', | ||
default: false, | ||
useDefault: true | ||
}, | ||
{ | ||
type: 'confirm', | ||
name: 'typeUnion', | ||
message: 'Use enums as type union?', | ||
default: false, | ||
useDefault: true | ||
}, | ||
{ | ||
type: 'confirm', | ||
name: 'enumsJSON', | ||
message: 'Enable JSON for enums?', | ||
default: false, | ||
useDefault: true | ||
}, | ||
{ | ||
type: 'confirm', | ||
name: 'removeUndefined', | ||
message: 'Remove undefined at index 0?', | ||
default: false, | ||
useDefault: true | ||
}, | ||
{ | ||
type: 'confirm', | ||
name: 'header', | ||
message: 'Include header in output?', | ||
default: false, | ||
useDefault: true | ||
}, | ||
{ | ||
type: 'confirm', | ||
name: 'case', | ||
message: 'Keep case in parser?', | ||
default: false, | ||
useDefault: true | ||
}, | ||
{ | ||
type: 'confirm', | ||
name: 'types', | ||
message: 'Enable types?', | ||
default: false, | ||
useDefault: true | ||
}, | ||
{ | ||
type: 'confirm', | ||
name: 'optional', | ||
message: 'Are optional fields enabled?', | ||
default: false, | ||
useDefault: true | ||
}, | ||
{ | ||
type: 'confirm', | ||
name: 'utils', | ||
message: 'Enable AST helpers in utils?', | ||
default: false, | ||
useDefault: true | ||
} | ||
]); | ||
|
||
argv = await cli(argv); | ||
|
||
return argv; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { CLIOptions, Inquirerer, Question } from 'inquirerer' | ||
import { ParsedArgs } from 'minimist'; | ||
|
||
import { commands as codegen } from './codegen'; | ||
import { commands as protogen } from './protogen'; | ||
|
||
import { help as codegenHelp } from './codegen/cli'; | ||
import { help as protogenHelp } from './protogen/cli'; | ||
|
||
export const commands = async (argv: Partial<ParsedArgs>, prompter: Inquirerer, _options: CLIOptions) => { | ||
let command; | ||
|
||
if (argv._.length > 0) { | ||
command = argv._.shift(); | ||
} | ||
|
||
if (command) { | ||
argv.command = command; | ||
} | ||
|
||
if (argv.help || argv.h) { | ||
codegenHelp(); | ||
protogenHelp(); | ||
process.exit(0); | ||
} | ||
|
||
|
||
const questions: Question[] = [ | ||
{ | ||
type: 'autocomplete', | ||
name: 'command', | ||
message: 'choose a command', | ||
options: [ | ||
'protogen', | ||
'codegen' | ||
] | ||
} | ||
]; | ||
|
||
({ command } = await prompter.prompt(argv, questions)); | ||
|
||
// recursive... | ||
delete argv.command; | ||
|
||
// @ts-ignore | ||
prompter.exit = () => {}; | ||
|
||
switch (command) { | ||
case 'protogen': | ||
argv = await protogen(argv, prompter, _options); | ||
break; | ||
|
||
case 'codegen': | ||
argv = await codegen(argv, prompter, _options); | ||
break; | ||
|
||
default: | ||
console.log(`No recognized command provided or no command given: ${command}`); | ||
break; | ||
} | ||
|
||
return argv; | ||
|
||
}; |
Oops, something went wrong.