|
| 1 | +import { Argument, program } from 'commander'; |
| 2 | + |
| 3 | +import { buildClients } from '../buildClients'; |
| 4 | +import { buildSpecs } from '../buildSpecs'; |
| 5 | +import { CI, DOCKER, LANGUAGES } from '../common'; |
| 6 | +import { ctsGenerateMany } from '../cts/generate'; |
| 7 | +import { runCts } from '../cts/runCts'; |
| 8 | +import { formatter } from '../formatter'; |
| 9 | +import { generate } from '../generate'; |
| 10 | +import { playground } from '../playground'; |
| 11 | + |
| 12 | +import type { Job, LangArg } from './utils'; |
| 13 | +import { |
| 14 | + ALL, |
| 15 | + getClientChoices, |
| 16 | + generatorList, |
| 17 | + prompt, |
| 18 | + PROMPT_CLIENTS, |
| 19 | + PROMPT_LANGUAGES, |
| 20 | +} from './utils'; |
| 21 | + |
| 22 | +if (!CI && !DOCKER) { |
| 23 | + // eslint-disable-next-line no-console |
| 24 | + console.log('You should run scripts via the docker container, see README.md'); |
| 25 | + // eslint-disable-next-line no-process-exit |
| 26 | + process.exit(1); |
| 27 | +} |
| 28 | + |
| 29 | +const args = { |
| 30 | + language: new Argument('[language]', 'The language').choices( |
| 31 | + PROMPT_LANGUAGES |
| 32 | + ), |
| 33 | + clients: (job: Job): Argument => |
| 34 | + new Argument('[client...]', 'The client').choices(getClientChoices(job)), |
| 35 | + client: new Argument('[client]', 'The client').choices(PROMPT_CLIENTS), |
| 36 | +}; |
| 37 | + |
| 38 | +const flags = { |
| 39 | + verbose: { |
| 40 | + flag: '-v, --verbose', |
| 41 | + description: 'make the generation verbose', |
| 42 | + }, |
| 43 | + interactive: { |
| 44 | + flag: '-i, --interactive', |
| 45 | + description: 'open prompt to query parameters', |
| 46 | + }, |
| 47 | + skipCache: { |
| 48 | + flag: '-s, --skip-cache', |
| 49 | + description: 'skip cache checking to force building specs', |
| 50 | + }, |
| 51 | + skipUtils: { |
| 52 | + flag: '-su, --skip-utils', |
| 53 | + description: 'skip utils build when building a JavaScript client', |
| 54 | + }, |
| 55 | + outputType: { |
| 56 | + flag: '-json, --output-json', |
| 57 | + description: 'outputs the spec in JSON instead of yml', |
| 58 | + }, |
| 59 | +}; |
| 60 | + |
| 61 | +program.name('cli'); |
| 62 | + |
| 63 | +program |
| 64 | + .command('generate') |
| 65 | + .description('Generate a specified client') |
| 66 | + .addArgument(args.language) |
| 67 | + .addArgument(args.clients('generate')) |
| 68 | + .option(flags.verbose.flag, flags.verbose.description) |
| 69 | + .option(flags.interactive.flag, flags.interactive.description) |
| 70 | + .action( |
| 71 | + async (langArg: LangArg, clientArg: string[], { verbose, interactive }) => { |
| 72 | + const { language, client, clientList } = await prompt({ |
| 73 | + langArg, |
| 74 | + clientArg, |
| 75 | + job: 'generate', |
| 76 | + interactive, |
| 77 | + }); |
| 78 | + |
| 79 | + await generate( |
| 80 | + generatorList({ language, client, clientList }), |
| 81 | + Boolean(verbose) |
| 82 | + ); |
| 83 | + } |
| 84 | + ); |
| 85 | + |
| 86 | +const buildCommand = program.command('build'); |
| 87 | + |
| 88 | +buildCommand |
| 89 | + .command('clients') |
| 90 | + .description('Build a specified client') |
| 91 | + .addArgument(args.language) |
| 92 | + .addArgument(args.clients('build')) |
| 93 | + .option(flags.verbose.flag, flags.verbose.description) |
| 94 | + .option(flags.interactive.flag, flags.interactive.description) |
| 95 | + .option(flags.skipUtils.flag, flags.skipUtils.description) |
| 96 | + .action( |
| 97 | + async ( |
| 98 | + langArg: LangArg, |
| 99 | + clientArg: string[], |
| 100 | + { verbose, interactive, skipUtils } |
| 101 | + ) => { |
| 102 | + const { language, client, clientList } = await prompt({ |
| 103 | + langArg, |
| 104 | + clientArg, |
| 105 | + job: 'build', |
| 106 | + interactive, |
| 107 | + }); |
| 108 | + |
| 109 | + await buildClients(generatorList({ language, client, clientList }), { |
| 110 | + verbose: Boolean(verbose), |
| 111 | + skipUtils: Boolean(skipUtils), |
| 112 | + }); |
| 113 | + } |
| 114 | + ); |
| 115 | + |
| 116 | +buildCommand |
| 117 | + .command('specs') |
| 118 | + .description('Build a specified spec') |
| 119 | + .addArgument(args.clients('specs')) |
| 120 | + .option(flags.verbose.flag, flags.verbose.description) |
| 121 | + .option(flags.interactive.flag, flags.interactive.description) |
| 122 | + .option(flags.skipCache.flag, flags.skipCache.description) |
| 123 | + .option(flags.outputType.flag, flags.outputType.description) |
| 124 | + .action( |
| 125 | + async ( |
| 126 | + clientArg: string[], |
| 127 | + { verbose, interactive, skipCache, outputJson } |
| 128 | + ) => { |
| 129 | + const { client, clientList } = await prompt({ |
| 130 | + langArg: ALL, |
| 131 | + clientArg, |
| 132 | + job: 'specs', |
| 133 | + interactive, |
| 134 | + }); |
| 135 | + |
| 136 | + const outputFormat = outputJson ? 'json' : 'yml'; |
| 137 | + |
| 138 | + // ignore cache when building from cli |
| 139 | + await buildSpecs( |
| 140 | + client[0] === ALL ? clientList : client, |
| 141 | + outputFormat, |
| 142 | + Boolean(verbose), |
| 143 | + !skipCache |
| 144 | + ); |
| 145 | + } |
| 146 | + ); |
| 147 | + |
| 148 | +const ctsCommand = program.command('cts'); |
| 149 | + |
| 150 | +ctsCommand |
| 151 | + .command('generate') |
| 152 | + .description('Generate the CTS tests') |
| 153 | + .addArgument(args.language) |
| 154 | + .addArgument(args.clients('generate')) |
| 155 | + .option(flags.verbose.flag, flags.verbose.description) |
| 156 | + .option(flags.interactive.flag, flags.interactive.description) |
| 157 | + .action( |
| 158 | + async (langArg: LangArg, clientArg: string[], { verbose, interactive }) => { |
| 159 | + const { language, client, clientList } = await prompt({ |
| 160 | + langArg, |
| 161 | + clientArg, |
| 162 | + job: 'generate', |
| 163 | + interactive, |
| 164 | + }); |
| 165 | + |
| 166 | + await ctsGenerateMany( |
| 167 | + generatorList({ language, client, clientList }), |
| 168 | + Boolean(verbose) |
| 169 | + ); |
| 170 | + } |
| 171 | + ); |
| 172 | + |
| 173 | +ctsCommand |
| 174 | + .command('run') |
| 175 | + .description('Run the tests for the CTS') |
| 176 | + .addArgument(args.language) |
| 177 | + .option(flags.verbose.flag, flags.verbose.description) |
| 178 | + .option(flags.interactive.flag, flags.interactive.description) |
| 179 | + .action(async (langArg: LangArg, { verbose, interactive }) => { |
| 180 | + const { language } = await prompt({ |
| 181 | + langArg, |
| 182 | + clientArg: [ALL], |
| 183 | + job: 'generate', |
| 184 | + interactive, |
| 185 | + }); |
| 186 | + |
| 187 | + await runCts(language === ALL ? LANGUAGES : [language], Boolean(verbose)); |
| 188 | + }); |
| 189 | + |
| 190 | +program |
| 191 | + .command('playground') |
| 192 | + .description('Run the playground') |
| 193 | + .addArgument(args.language) |
| 194 | + .addArgument(args.client) |
| 195 | + .option(flags.interactive.flag, flags.interactive.description) |
| 196 | + .action(async (langArg: LangArg, cliClient: string, { interactive }) => { |
| 197 | + const { language, client } = await prompt({ |
| 198 | + langArg, |
| 199 | + clientArg: [cliClient], |
| 200 | + job: 'build', |
| 201 | + interactive, |
| 202 | + }); |
| 203 | + |
| 204 | + await playground({ |
| 205 | + language, |
| 206 | + client: client[0], |
| 207 | + }); |
| 208 | + }); |
| 209 | + |
| 210 | +program |
| 211 | + .command('format') |
| 212 | + .description('Format the specified folder for a specific language') |
| 213 | + .addArgument(args.language) |
| 214 | + .argument('folder', 'The folder to format') |
| 215 | + .option(flags.verbose.flag, flags.verbose.description) |
| 216 | + .action(async (language: string, folder: string, { verbose }) => { |
| 217 | + await formatter(language, folder, verbose); |
| 218 | + }); |
| 219 | + |
| 220 | +program.parse(); |
0 commit comments