|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +import { Flags, loglevel, orgApiVersionFlagWithDeprecations, SfCommand, Ux } from '@salesforce/sf-plugins-core'; |
| 9 | +import { CreateOutput, FlexipageOptions, TemplateType } from '@salesforce/templates'; |
| 10 | +import { Messages } from '@salesforce/core'; |
| 11 | +import { getCustomTemplates, runGenerator } from '../../../../utils/templateCommand.js'; |
| 12 | +import { internalFlag, outputDirFlag } from '../../../../utils/flags.js'; |
| 13 | + |
| 14 | +Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); |
| 15 | +const messages = Messages.loadMessages('@salesforce/plugin-templates', 'flexipage'); |
| 16 | + |
| 17 | +export default class FlexipageGenerate extends SfCommand<CreateOutput> { |
| 18 | + public static readonly summary = messages.getMessage('summary'); |
| 19 | + public static readonly description = messages.getMessage('description'); |
| 20 | + public static readonly examples = messages.getMessages('examples'); |
| 21 | + public static readonly state = 'beta'; |
| 22 | + public static readonly flags = { |
| 23 | + name: Flags.string({ |
| 24 | + char: 'n', |
| 25 | + summary: messages.getMessage('flags.name.summary'), |
| 26 | + description: messages.getMessage('flags.name.description'), |
| 27 | + required: true, |
| 28 | + aliases: ['flexipagename'], |
| 29 | + deprecateAliases: true, |
| 30 | + }), |
| 31 | + template: Flags.option({ |
| 32 | + char: 't', |
| 33 | + summary: messages.getMessage('flags.template.summary'), |
| 34 | + required: true, |
| 35 | + options: ['RecordPage', 'AppPage', 'HomePage'] as const, |
| 36 | + })(), |
| 37 | + 'output-dir': outputDirFlag, |
| 38 | + 'api-version': orgApiVersionFlagWithDeprecations, |
| 39 | + label: Flags.string({ |
| 40 | + summary: messages.getMessage('flags.label.summary'), |
| 41 | + aliases: ['masterlabel'], |
| 42 | + deprecateAliases: true, |
| 43 | + }), |
| 44 | + description: Flags.string({ |
| 45 | + summary: messages.getMessage('flags.description.summary'), |
| 46 | + }), |
| 47 | + sobject: Flags.string({ |
| 48 | + char: 's', |
| 49 | + summary: messages.getMessage('flags.sobject.summary'), |
| 50 | + description: messages.getMessage('flags.sobject.description'), |
| 51 | + aliases: ['entity-name', 'entity'], |
| 52 | + deprecateAliases: true, |
| 53 | + }), |
| 54 | + 'primary-field': Flags.string({ |
| 55 | + summary: messages.getMessage('flags.primary-field.summary'), |
| 56 | + }), |
| 57 | + 'secondary-fields': Flags.string({ |
| 58 | + summary: messages.getMessage('flags.secondary-fields.summary'), |
| 59 | + multiple: true, |
| 60 | + delimiter: ',', |
| 61 | + }), |
| 62 | + 'detail-fields': Flags.string({ |
| 63 | + summary: messages.getMessage('flags.detail-fields.summary'), |
| 64 | + multiple: true, |
| 65 | + delimiter: ',', |
| 66 | + }), |
| 67 | + internal: internalFlag, |
| 68 | + loglevel, |
| 69 | + }; |
| 70 | + |
| 71 | + private static readonly MAX_SECONDARY_FIELDS = 11; |
| 72 | + |
| 73 | + public async run(): Promise<CreateOutput> { |
| 74 | + const { flags } = await this.parse(FlexipageGenerate); |
| 75 | + |
| 76 | + // Validate RecordPage requires sobject |
| 77 | + if (flags.template === 'RecordPage' && !flags.sobject) { |
| 78 | + throw new Error(messages.getMessage('errors.recordPageRequiresSobject')); |
| 79 | + } |
| 80 | + |
| 81 | + // Validate RecordPage-specific flags are only used with RecordPage template |
| 82 | + const recordPageOnlyFlags = ['primary-field', 'secondary-fields', 'detail-fields'] as const; |
| 83 | + if (flags.template !== 'RecordPage') { |
| 84 | + for (const flagName of recordPageOnlyFlags) { |
| 85 | + if (flags[flagName]) { |
| 86 | + throw new Error(messages.getMessage('errors.flagRequiresRecordPage', [flagName])); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Validate secondary fields limit (Dynamic Highlights Panel supports max 11) |
| 92 | + const secondaryFieldsCount = flags['secondary-fields']?.length ?? 0; |
| 93 | + if (secondaryFieldsCount > FlexipageGenerate.MAX_SECONDARY_FIELDS) { |
| 94 | + throw new Error( |
| 95 | + messages.getMessage('errors.tooManySecondaryFields', [ |
| 96 | + secondaryFieldsCount.toString(), |
| 97 | + FlexipageGenerate.MAX_SECONDARY_FIELDS.toString(), |
| 98 | + ]) |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + // Convert CLI flags to library options |
| 103 | + const flagsAsOptions: FlexipageOptions = { |
| 104 | + flexipagename: flags.name, |
| 105 | + template: flags.template, |
| 106 | + outputdir: flags['output-dir'], |
| 107 | + apiversion: flags['api-version'], |
| 108 | + masterlabel: flags.label, |
| 109 | + description: flags.description, |
| 110 | + entityName: flags.sobject, |
| 111 | + primaryField: flags['primary-field'], |
| 112 | + secondaryFields: flags['secondary-fields'] ?? [], |
| 113 | + detailFields: flags['detail-fields'] ?? [], |
| 114 | + internal: flags.internal, |
| 115 | + }; |
| 116 | + |
| 117 | + return runGenerator({ |
| 118 | + templateType: TemplateType.Flexipage, |
| 119 | + opts: flagsAsOptions, |
| 120 | + ux: new Ux({ jsonEnabled: this.jsonEnabled() }), |
| 121 | + templates: getCustomTemplates(this.configAggregator), |
| 122 | + }); |
| 123 | + } |
| 124 | +} |
0 commit comments