-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
17 changed files
with
136 additions
and
31 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,73 @@ | ||
#!/usr/bin/env node | ||
|
||
import { Command } from 'commander'; | ||
import { OptionalFieldPattern, Options, UntypedType } from '../options'; | ||
import { generateFiles } from '../generateFiles'; | ||
|
||
// These are valid values for the type BufferEncoding | ||
const validEncodings = ['ascii', 'utf8', 'utf-8', 'utf16le', 'ucs2', 'ucs-2', 'base64', 'base64url', 'latin1', 'binary', 'hex']; | ||
const validPatternNames = Object.keys(OptionalFieldPattern); | ||
const validTypeNames = Object.keys(UntypedType); | ||
|
||
const validateEncoding = (encoding: string): string => { | ||
if (validEncodings.includes(encoding)) { | ||
return encoding; | ||
} | ||
throw Error(`Invalid source file encoding: '${encoding}'. Valid options are: ${arrayToString(validEncodings)}`); | ||
}; | ||
|
||
const validatePattern = (patternName: string): string => { | ||
if (validPatternNames.includes(patternName)) { | ||
return patternName; | ||
} | ||
throw Error(`Invalid optional field pattern: '${patternName}'. Valid options are: ${arrayToString(validPatternNames)}`); | ||
}; | ||
|
||
const validateType = (typeName: string): string => { | ||
if (validTypeNames.includes(typeName)) { | ||
return typeName; | ||
} | ||
throw Error(`Invalid untyped type: '${typeName}'. Valid options are: ${arrayToString(validTypeNames)}`); | ||
}; | ||
|
||
const arrayToString = (array: string[]) => `['${array.join('\', \'')}']`; | ||
|
||
const program = new Command(); | ||
|
||
program | ||
.option('-c, --cwd <DIRECTORY>', 'Working directory', process.cwd()) | ||
.option('-s, --source <DIRECTORY>', 'Source directory of schema files, relative to working directory', 'src/schemas') | ||
.option('-d, --destination <DIRECTORY>', 'Destination directory of generated .ts files, relative to working directory', 'src/generated') | ||
.option('-e, --encoding <ENCODING>', `Encoding of source files, one of: ${arrayToString(validEncodings)}`, validateEncoding, 'utf-8') | ||
.option('-r, --recursive', 'Recurse into source directory and generate from nested files', true) | ||
.option('-R, --no-recursive', 'Only generate from schema files directly in source directory, do not recurse deeper') | ||
.option('-p, --pre-clean', 'Delete destination directory before generating files', false) | ||
.option('-P, --no-pre-clean', 'Overwrite destination directory when generating files') | ||
.option('-i, --index-files', 'Add index files', true) | ||
.option('-I, --no-index-files', 'Do not add index files') | ||
.option('-o, --optional-field-pattern <PATTERN>', `The pattern to use for optional fields, one of: ${arrayToString(validPatternNames)}`, validatePattern, 'QUESTION') | ||
.option('-u, --untyped-type <TYPE>', `The untyped field type, one of: ${arrayToString(validTypeNames)}`, validateType, 'UNKNOWN') | ||
.action(async (opts) => { | ||
const options: Options = { | ||
files: { | ||
cwd: opts.cwd, | ||
destination: { | ||
dir: opts.destination, | ||
indexFiles: opts.indexFiles, | ||
preClean: opts.preClean | ||
}, | ||
source: { | ||
dir: opts.source, | ||
encoding: opts.encoding, | ||
recursive: opts.recursive | ||
} | ||
}, | ||
ts: { | ||
optionalFields: opts.optionalFieldPattern as OptionalFieldPattern, | ||
untyped: opts.untypedType as UntypedType | ||
} | ||
}; | ||
await generateFiles(options); | ||
}); | ||
|
||
program.parse(process.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
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
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
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
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
Oops, something went wrong.