|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +const cli_command_1 = require("@contentstack/cli-command"); |
| 4 | +const client_1 = require("../lib/stack/client"); |
| 5 | +const runner_1 = require("../lib/tsgen/runner"); |
| 6 | +class TypeScriptCodeGeneratorCommand extends cli_command_1.Command { |
| 7 | + async run() { |
| 8 | + try { |
| 9 | + const { flags } = this.parse(TypeScriptCodeGeneratorCommand); |
| 10 | + const token = this.getToken(flags['token-alias']); |
| 11 | + const prefix = flags.prefix; |
| 12 | + const includeDocumentation = flags.doc; |
| 13 | + const outputPath = flags.output; |
| 14 | + if (token.type !== 'delivery') { |
| 15 | + this.warn('Possibly using a management token. You may not be able to connect to your Stack. Please use a delivery token.'); |
| 16 | + } |
| 17 | + if (!outputPath || !outputPath.trim()) { |
| 18 | + this.error('Please provide an output path.', { exit: 2 }); |
| 19 | + } |
| 20 | + const config = { |
| 21 | + apiKey: token.apiKey, |
| 22 | + token: token.token, |
| 23 | + region: this.region.name, |
| 24 | + environment: token.environment || '', |
| 25 | + }; |
| 26 | + const client = await client_1.stackConnect(this.deliveryAPIClient.Stack, config); |
| 27 | + if (client.types) { |
| 28 | + const result = await runner_1.default(outputPath, client.types, prefix, includeDocumentation); |
| 29 | + this.log(`Wrote ${result.definitions} Content Types to '${result.outputPath}'.`); |
| 30 | + } |
| 31 | + else { |
| 32 | + this.log('No Content Types exist in the Stack.'); |
| 33 | + } |
| 34 | + } |
| 35 | + catch (error) { |
| 36 | + this.error(error, { exit: 1 }); |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | +exports.default = TypeScriptCodeGeneratorCommand; |
| 41 | +TypeScriptCodeGeneratorCommand.description = 'generate TypeScript typings from a Stack'; |
| 42 | +TypeScriptCodeGeneratorCommand.examples = [ |
| 43 | + '$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts"', |
| 44 | + '$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" -p "I"', |
| 45 | + '$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" --no-doc', |
| 46 | +]; |
| 47 | +TypeScriptCodeGeneratorCommand.flags = { |
| 48 | + 'token-alias': cli_command_1.flags.string({ |
| 49 | + char: 'a', |
| 50 | + description: 'delivery token alias', |
| 51 | + hidden: false, |
| 52 | + multiple: false, |
| 53 | + required: true, |
| 54 | + }), |
| 55 | + output: cli_command_1.flags.string({ |
| 56 | + char: 'o', |
| 57 | + description: 'full path to output', |
| 58 | + hidden: false, |
| 59 | + multiple: false, |
| 60 | + required: true, |
| 61 | + }), |
| 62 | + prefix: cli_command_1.flags.string({ |
| 63 | + char: 'p', |
| 64 | + description: 'interface prefix, e.g. "I"', |
| 65 | + hidden: false, |
| 66 | + multiple: false, |
| 67 | + default: '', |
| 68 | + required: false, |
| 69 | + }), |
| 70 | + doc: cli_command_1.flags.boolean({ |
| 71 | + char: 'd', |
| 72 | + description: 'include documentation comments', |
| 73 | + default: true, |
| 74 | + allowNo: true, |
| 75 | + }), |
| 76 | +}; |
0 commit comments