|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 | import { readFileSync } from 'node:fs'; |
17 | | -import { join } from 'node:path'; |
| 17 | +import { basename, join } from 'node:path'; |
18 | 18 | import { SfCommand, Flags } from '@salesforce/sf-plugins-core'; |
19 | 19 | import { Messages, SfError } from '@salesforce/core'; |
20 | 20 | import { MultiStageOutput } from '@oclif/multi-stage-output'; |
21 | 21 | import { Agent, findAuthoringBundle } from '@salesforce/agents'; |
22 | 22 | import { Duration, sleep } from '@salesforce/kit'; |
23 | 23 | import { colorize } from '@oclif/core/ux'; |
24 | | -import { FlaggablePrompt, promptForFlag } from '../../../flags.js'; |
| 24 | +import { FlaggablePrompt, promptForFileByExtensions } from '../../../flags.js'; |
25 | 25 |
|
26 | 26 | Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); |
27 | 27 | const messages = Messages.loadMessages('@salesforce/plugin-agent', 'agent.validate.authoring-bundle'); |
@@ -65,14 +65,22 @@ export default class AgentValidateAuthoringBundle extends SfCommand<AgentValidat |
65 | 65 |
|
66 | 66 | public async run(): Promise<AgentValidateAuthoringBundleResult> { |
67 | 67 | const { flags } = await this.parse(AgentValidateAuthoringBundle); |
68 | | - // If we don't have an api name yet, prompt for it |
69 | | - const apiName = |
70 | | - flags['api-name'] ?? (await promptForFlag(AgentValidateAuthoringBundle.FLAGGABLE_PROMPTS['api-name'])); |
71 | | - const authoringBundleDir = findAuthoringBundle(this.project!.getPath(), apiName); |
72 | | - if (!authoringBundleDir) { |
73 | | - throw new SfError(messages.getMessage('error.agentNotFound', [apiName]), 'AgentNotFoundError', [ |
74 | | - messages.getMessage('error.agentNotFoundAction'), |
| 68 | + let apiName = flags['api-name']; |
| 69 | + let agentFilePath; |
| 70 | + if (apiName) { |
| 71 | + const authoringBundleDir = findAuthoringBundle(this.project!.getPath(), apiName); |
| 72 | + if (!authoringBundleDir) { |
| 73 | + throw new SfError(messages.getMessage('error.agentNotFound', [apiName]), 'AgentNotFoundError', [ |
| 74 | + messages.getMessage('error.agentNotFoundAction'), |
| 75 | + ]); |
| 76 | + } |
| 77 | + agentFilePath = join(authoringBundleDir, `${apiName}.agent`); |
| 78 | + } else { |
| 79 | + // Prompt user to select an .agent file from the project and extract the API name from it |
| 80 | + agentFilePath = await promptForFileByExtensions(AgentValidateAuthoringBundle.FLAGGABLE_PROMPTS['api-name'], [ |
| 81 | + '.agent', |
75 | 82 | ]); |
| 83 | + apiName = basename(agentFilePath, '.agent'); |
76 | 84 | } |
77 | 85 | const mso = new MultiStageOutput<{ status: string; errors: string }>({ |
78 | 86 | jsonEnabled: this.jsonEnabled(), |
@@ -101,7 +109,7 @@ export default class AgentValidateAuthoringBundle extends SfCommand<AgentValidat |
101 | 109 | const conn = targetOrg.getConnection(flags['api-version']); |
102 | 110 | // Call Agent.compileAfScript() API |
103 | 111 | await sleep(Duration.seconds(2)); |
104 | | - await Agent.compileAfScript(conn, readFileSync(join(authoringBundleDir, `${apiName}.agent`), 'utf8')); |
| 112 | + await Agent.compileAfScript(conn, readFileSync(agentFilePath, 'utf8')); |
105 | 113 | mso.updateData({ status: 'COMPLETED' }); |
106 | 114 | mso.stop('completed'); |
107 | 115 | return { |
|
0 commit comments