@@ -22,6 +22,7 @@ import { Messages, Lifecycle, SfError } from '@salesforce/core';
2222import { Agent , findAuthoringBundle } from '@salesforce/agents' ;
2323import { RequestStatus , type ScopedPostRetrieve } from '@salesforce/source-deploy-retrieve' ;
2424import { ensureArray } from '@salesforce/kit' ;
25+ import { FlaggablePrompt , promptForFlag } from '../../../flags.js' ;
2526
2627Messages . importMessagesDirectoryFromMetaUrl ( import . meta. url ) ;
2728const messages = Messages . loadMessages ( '@salesforce/plugin-agent' , 'agent.publish.authoring-bundle' ) ;
@@ -44,26 +45,45 @@ export default class AgentPublishAuthoringBundle extends SfCommand<AgentPublishA
4445 'api-name' : Flags . string ( {
4546 char : 'n' ,
4647 summary : messages . getMessage ( 'flags.api-name.summary' ) ,
47- required : true ,
4848 } ) ,
4949 } ;
5050
51+ private static readonly FLAGGABLE_PROMPTS = {
52+ 'api-name' : {
53+ message : messages . getMessage ( 'flags.api-name.summary' ) ,
54+ promptMessage : messages . getMessage ( 'flags.api-name.prompt' ) ,
55+ validate : ( d : string ) : boolean | string => {
56+ if ( d . length > 80 ) {
57+ return 'API name cannot be over 80 characters.' ;
58+ }
59+ const regex = / ^ [ A - Z a - z ] [ A - Z a - z 0 - 9 _ ] * [ A - Z a - z 0 - 9 ] + $ / ;
60+ if ( d . length === 0 || ! regex . test ( d ) ) {
61+ return 'Invalid API name.' ;
62+ }
63+ return true ;
64+ } ,
65+ } ,
66+ } satisfies Record < string , FlaggablePrompt > ;
67+
5168 public async run ( ) : Promise < AgentPublishAuthoringBundleResult > {
5269 const { flags } = await this . parse ( AgentPublishAuthoringBundle ) ;
70+ // If we don't have an api name yet, prompt for it
71+ const apiName =
72+ flags [ 'api-name' ] ?? ( await promptForFlag ( AgentPublishAuthoringBundle . FLAGGABLE_PROMPTS [ 'api-name' ] ) ) ;
5373 // todo: this eslint warning can be removed once published
5474 // eslint-disable-next-line @typescript-eslint/no-unsafe-call
55- const authoringBundleDir = findAuthoringBundle ( this . project ! . getPath ( ) , flags [ 'api-name' ] ) ;
75+ const authoringBundleDir = findAuthoringBundle ( this . project ! . getPath ( ) , apiName ) ;
5676
5777 if ( ! authoringBundleDir ) {
58- throw new SfError ( messages . getMessage ( 'error.afscriptNotFound ' , [ flags [ 'api-name' ] ] ) , 'AfScriptNotFoundError ' , [
59- messages . getMessage ( 'error.afscriptNotFoundAction ' ) ,
78+ throw new SfError ( messages . getMessage ( 'error.agentNotFound ' , [ apiName ] ) , 'AgentNotFoundError ' , [
79+ messages . getMessage ( 'error.agentNotFoundAction ' ) ,
6080 ] ) ;
6181 }
6282 // Create multi-stage output
6383 const mso = new MultiStageOutput < { agentName : string } > ( {
6484 stages : [ 'Validate Bundle' , 'Publish Agent' , 'Retrieve Metadata' ] ,
6585 title : 'Publishing Agent' ,
66- data : { agentName : flags [ 'api-name' ] } ,
86+ data : { agentName : apiName } ,
6787 jsonEnabled : this . jsonEnabled ( ) ,
6888 postStagesBlock : [
6989 {
@@ -83,7 +103,7 @@ export default class AgentPublishAuthoringBundle extends SfCommand<AgentPublishA
83103 // First compile the AF script to get the Agent JSON
84104 const agentJson = await Agent . compileAfScript (
85105 conn ,
86- readFileSync ( join ( authoringBundleDir , `${ flags [ 'api-name' ] } .agent` ) , 'utf8' )
106+ readFileSync ( join ( authoringBundleDir , `${ apiName } .agent` ) , 'utf8' )
87107 ) ;
88108 mso . skipTo ( 'Publish Agent' ) ;
89109
0 commit comments