|
| 1 | +import * as path from 'node:path'; |
| 2 | +import { parseArgs } from 'node:util'; |
| 3 | +import { PositionalArg, showHelp } from './help'; |
| 4 | +import { GenerateModuleMap, PatternKeys, generate, generateAll } from '../generate'; |
| 5 | +import { log, parsePattern } from '../util'; |
| 6 | + |
| 7 | +const command = 'spec2cdk'; |
| 8 | +const args: PositionalArg[] = [{ |
| 9 | + name: 'output-path', |
| 10 | + required: true, |
| 11 | + description: 'The directory the generated code will be written to', |
| 12 | +}]; |
| 13 | +const config = { |
| 14 | + 'help': { |
| 15 | + short: 'h', |
| 16 | + type: 'boolean', |
| 17 | + description: 'Show this help', |
| 18 | + }, |
| 19 | + 'debug': { |
| 20 | + type: 'boolean', |
| 21 | + description: 'Show additional debug output', |
| 22 | + }, |
| 23 | + 'pattern': { |
| 24 | + type: 'string', |
| 25 | + default: '%moduleName%/%serviceShortName%.generated.ts', |
| 26 | + description: 'File and path pattern for generated files', |
| 27 | + }, |
| 28 | + 'augmentations': { |
| 29 | + type: 'string', |
| 30 | + default: '%moduleName%/%serviceShortName%-augmentations.generated.ts', |
| 31 | + description: 'File and path pattern for generated augmentations files', |
| 32 | + }, |
| 33 | + 'metrics': { |
| 34 | + type: 'string', |
| 35 | + default: '%moduleName%/%serviceShortName%-canned-metrics.generated.ts', |
| 36 | + description: 'File and path pattern for generated canned metrics files ', |
| 37 | + }, |
| 38 | + 'service': { |
| 39 | + short: 's', |
| 40 | + type: 'string', |
| 41 | + description: 'Generate files only for a specific service, e.g. AWS::S3', |
| 42 | + multiple: true, |
| 43 | + }, |
| 44 | + 'clear-output': { |
| 45 | + type: 'boolean', |
| 46 | + default: false, |
| 47 | + description: 'Completely delete the output path before generating new files', |
| 48 | + }, |
| 49 | + 'augmentations-support': { |
| 50 | + type: 'boolean', |
| 51 | + default: false, |
| 52 | + description: 'Generates additional files required for augmentation files to compile. Use for testing only', |
| 53 | + }, |
| 54 | +} as const; |
| 55 | + |
| 56 | +const helpText = `Path patterns can use the following variables: |
| 57 | +
|
| 58 | + %moduleName% The name of the module, e.g. aws-lambda |
| 59 | + %serviceName% The full name of the service, e.g. aws-lambda |
| 60 | + %serviceShortName% The short name of the service, e.g. lambda |
| 61 | +
|
| 62 | +Note that %moduleName% and %serviceName% can be different if multiple services are generated into a single module.`; |
| 63 | + |
| 64 | +const help = () => showHelp(command, args, config, helpText); |
| 65 | +export const shortHelp = () => showHelp(command, args); |
| 66 | + |
| 67 | +export async function main(argv: string[]) { |
| 68 | + const { |
| 69 | + positionals, |
| 70 | + values: options, |
| 71 | + } = parseArgs({ |
| 72 | + args: argv, |
| 73 | + allowPositionals: true, |
| 74 | + options: config, |
| 75 | + }); |
| 76 | + |
| 77 | + if (options.help) { |
| 78 | + help(); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + if (options.debug) { |
| 83 | + process.env.DEBUG = '1'; |
| 84 | + } |
| 85 | + log.debug('CLI args', positionals, options); |
| 86 | + |
| 87 | + const outputDir = positionals[0]; |
| 88 | + if (!outputDir) { |
| 89 | + throw new EvalError('Please specify the output-path'); |
| 90 | + } |
| 91 | + |
| 92 | + const pss: Record<PatternKeys, true> = { moduleName: true, serviceName: true, serviceShortName: true }; |
| 93 | + |
| 94 | + const outputPath = outputDir ?? path.join(__dirname, '..', 'services'); |
| 95 | + const resourceFilePattern = parsePattern( |
| 96 | + stringOr(options.pattern, path.join('%moduleName%', '%serviceShortName%.generated.ts')), |
| 97 | + pss, |
| 98 | + ); |
| 99 | + |
| 100 | + const augmentationsFilePattern = parsePattern( |
| 101 | + stringOr(options.augmentations, path.join('%moduleName%', '%serviceShortName%-augmentations.generated.ts')), |
| 102 | + pss, |
| 103 | + ); |
| 104 | + |
| 105 | + const cannedMetricsFilePattern = parsePattern( |
| 106 | + stringOr(options.metrics, path.join('%moduleName%', '%serviceShortName%-canned-metrics.generated.ts')), |
| 107 | + pss, |
| 108 | + ); |
| 109 | + |
| 110 | + const generatorOptions = { |
| 111 | + outputPath, |
| 112 | + filePatterns: { |
| 113 | + resources: resourceFilePattern, |
| 114 | + augmentations: augmentationsFilePattern, |
| 115 | + cannedMetrics: cannedMetricsFilePattern, |
| 116 | + }, |
| 117 | + clearOutput: options['clear-output'], |
| 118 | + augmentationsSupport: options['augmentations-support'], |
| 119 | + debug: options.debug as boolean, |
| 120 | + }; |
| 121 | + |
| 122 | + if (options.service?.length) { |
| 123 | + const moduleMap: GenerateModuleMap = {}; |
| 124 | + for (const service of options.service) { |
| 125 | + if (!service.includes('::')) { |
| 126 | + throw new EvalError(`Each service must be in the form <Partition>::<Service>, e.g. AWS::S3. Got: ${service}`); |
| 127 | + } |
| 128 | + moduleMap[service.toLocaleLowerCase().split('::').join('-')] = { services: [service] }; |
| 129 | + } |
| 130 | + await generate(moduleMap, generatorOptions); |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + await generateAll(generatorOptions); |
| 135 | +} |
| 136 | + |
| 137 | +function stringOr(pat: unknown, def: string) { |
| 138 | + if (!pat) { |
| 139 | + return def; |
| 140 | + } |
| 141 | + if (typeof pat !== 'string') { |
| 142 | + throw new Error(`Expected string, got: ${JSON.stringify(pat)}`); |
| 143 | + } |
| 144 | + return pat; |
| 145 | +} |
0 commit comments