Skip to content

Commit 0c0cafa

Browse files
fix: if api-name flag is not used, prompt user to select an .agent file from the project
1 parent f51e7b8 commit 0c0cafa

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/commands/agent/validate/authoring-bundle.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
* limitations under the License.
1515
*/
1616
import { readFileSync } from 'node:fs';
17-
import { join } from 'node:path';
17+
import { basename, join } from 'node:path';
1818
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
1919
import { Messages, SfError } from '@salesforce/core';
2020
import { MultiStageOutput } from '@oclif/multi-stage-output';
2121
import { Agent, findAuthoringBundle } from '@salesforce/agents';
2222
import { Duration, sleep } from '@salesforce/kit';
2323
import { colorize } from '@oclif/core/ux';
24-
import { FlaggablePrompt, promptForFlag } from '../../../flags.js';
24+
import { FlaggablePrompt, promptForFileByExtensions } from '../../../flags.js';
2525

2626
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
2727
const messages = Messages.loadMessages('@salesforce/plugin-agent', 'agent.validate.authoring-bundle');
@@ -65,14 +65,22 @@ export default class AgentValidateAuthoringBundle extends SfCommand<AgentValidat
6565

6666
public async run(): Promise<AgentValidateAuthoringBundleResult> {
6767
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',
7582
]);
83+
apiName = basename(agentFilePath, '.agent');
7684
}
7785
const mso = new MultiStageOutput<{ status: string; errors: string }>({
7886
jsonEnabled: this.jsonEnabled(),
@@ -101,7 +109,7 @@ export default class AgentValidateAuthoringBundle extends SfCommand<AgentValidat
101109
const conn = targetOrg.getConnection(flags['api-version']);
102110
// Call Agent.compileAfScript() API
103111
await sleep(Duration.seconds(2));
104-
await Agent.compileAfScript(conn, readFileSync(join(authoringBundleDir, `${apiName}.agent`), 'utf8'));
112+
await Agent.compileAfScript(conn, readFileSync(agentFilePath, 'utf8'));
105113
mso.updateData({ status: 'COMPLETED' });
106114
mso.stop('completed');
107115
return {

src/flags.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ export const promptForAiEvaluationDefinitionApiName = async (
151151
});
152152
};
153153

154-
export const promptForYamlFile = async (flagDef: FlaggablePrompt): Promise<string> => {
154+
export const promptForFileByExtensions = async (flagDef: FlaggablePrompt, extensions: string[]): Promise<string> => {
155155
const hiddenDirs = await getHiddenDirs();
156-
const yamlFiles = await traverseForFiles(process.cwd(), ['.yml', '.yaml'], ['node_modules', ...hiddenDirs]);
156+
const yamlFiles = await traverseForFiles(process.cwd(), extensions, ['node_modules', ...hiddenDirs]);
157157
return autocomplete({
158158
message: flagDef.message,
159159
// eslint-disable-next-line @typescript-eslint/require-await
@@ -166,6 +166,9 @@ export const promptForYamlFile = async (flagDef: FlaggablePrompt): Promise<strin
166166
});
167167
};
168168

169+
export const promptForYamlFile = async (flagDef: FlaggablePrompt): Promise<string> =>
170+
promptForFileByExtensions(flagDef, ['.yml', '.yaml']);
171+
169172
export const promptForFlag = async (flagDef: FlaggablePrompt): Promise<string> => {
170173
const message = flagDef.promptMessage ?? flagDef.message.replace(/\.$/, '');
171174
if (flagDef.options) {

0 commit comments

Comments
 (0)