Skip to content

Commit 0c6517a

Browse files
Merge pull request #230 from salesforcecli/er/promptUserToSelectAgentFile
W-19829287: if api-name flag is not used, prompt user to select an .agent file from the project
2 parents f51e7b8 + 3f3a360 commit 0c6517a

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export default class AgentGenerateAuthoringBundle extends SfCommand<AgentGenerat
129129

130130
// Generate file paths
131131
const agentPath = join(targetOutputDir, `${bundleApiName}.agent`);
132-
const metaXmlPath = join(targetOutputDir, `${bundleApiName}.aiAuthoringBundle-meta.xml`);
132+
const metaXmlPath = join(targetOutputDir, `${bundleApiName}.bundle-meta.xml`);
133133

134134
// Write Agent file
135135
const conn = targetOrg.getConnection(flags['api-version']);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { Messages, Lifecycle, SfError } from '@salesforce/core';
2222
import { Agent, findAuthoringBundle } from '@salesforce/agents';
2323
import { RequestStatus, type ScopedPostRetrieve } from '@salesforce/source-deploy-retrieve';
2424
import { ensureArray } from '@salesforce/kit';
25-
import { FlaggablePrompt, promptForFlag } from '../../../flags.js';
25+
import { FlaggablePrompt, promptForFileByExtensions } from '../../../flags.js';
2626

2727
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
2828
const messages = Messages.loadMessages('@salesforce/plugin-agent', 'agent.publish.authoring-bundle');
@@ -67,9 +67,10 @@ export default class AgentPublishAuthoringBundle extends SfCommand<AgentPublishA
6767

6868
public async run(): Promise<AgentPublishAuthoringBundleResult> {
6969
const { flags } = await this.parse(AgentPublishAuthoringBundle);
70-
// If we don't have an api name yet, prompt for it
70+
// If api-name is not provided, prompt user to select an .agent file from the project and extract the API name from it
7171
const apiName =
72-
flags['api-name'] ?? (await promptForFlag(AgentPublishAuthoringBundle.FLAGGABLE_PROMPTS['api-name']));
72+
flags['api-name'] ??
73+
(await promptForFileByExtensions(AgentPublishAuthoringBundle.FLAGGABLE_PROMPTS['api-name'], ['.agent'], true));
7374
// todo: this eslint warning can be removed once published
7475
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
7576
const authoringBundleDir = findAuthoringBundle(this.project!.getPath(), apiName);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ 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,9 +65,10 @@ 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
68+
// If api-name is not provided, prompt user to select an .agent file from the project and extract the API name from it
6969
const apiName =
70-
flags['api-name'] ?? (await promptForFlag(AgentValidateAuthoringBundle.FLAGGABLE_PROMPTS['api-name']));
70+
flags['api-name'] ??
71+
(await promptForFileByExtensions(AgentValidateAuthoringBundle.FLAGGABLE_PROMPTS['api-name'], ['.agent'], true));
7172
const authoringBundleDir = findAuthoringBundle(this.project!.getPath(), apiName);
7273
if (!authoringBundleDir) {
7374
throw new SfError(messages.getMessage('error.agentNotFound', [apiName]), 'AgentNotFoundError', [

src/flags.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { readdir } from 'node:fs/promises';
18-
import { join, relative } from 'node:path';
18+
import { basename, join, relative } from 'node:path';
1919
import { Interfaces } from '@oclif/core';
2020
import { Flags } from '@salesforce/sf-plugins-core';
2121
import { Connection, Messages, SfError } from '@salesforce/core';
@@ -151,21 +151,32 @@ export const promptForAiEvaluationDefinitionApiName = async (
151151
});
152152
};
153153

154-
export const promptForYamlFile = async (flagDef: FlaggablePrompt): Promise<string> => {
154+
export const promptForFileByExtensions = async (
155+
flagDef: FlaggablePrompt,
156+
extensions: string[],
157+
fileNameOnly = false
158+
): Promise<string> => {
155159
const hiddenDirs = await getHiddenDirs();
156-
const yamlFiles = await traverseForFiles(process.cwd(), ['.yml', '.yaml'], ['node_modules', ...hiddenDirs]);
160+
const files = await traverseForFiles(process.cwd(), extensions, ['node_modules', ...hiddenDirs]);
157161
return autocomplete({
158-
message: flagDef.message,
162+
message: flagDef.promptMessage ?? flagDef.message.replace(/\.$/, ''),
159163
// eslint-disable-next-line @typescript-eslint/require-await
160164
source: async (input) => {
161-
const arr = yamlFiles.map((o) => ({ name: relative(process.cwd(), o), value: o }));
162-
165+
let arr;
166+
if (fileNameOnly) {
167+
arr = files.map((o) => ({ name: basename(o).split('.')[0], value: basename(o).split('.')[0] }));
168+
} else {
169+
arr = files.map((o) => ({ name: relative(process.cwd(), o), value: o }));
170+
}
163171
if (!input) return arr;
164172
return arr.filter((o) => o.name.includes(input));
165173
},
166174
});
167175
};
168176

177+
export const promptForYamlFile = async (flagDef: FlaggablePrompt): Promise<string> =>
178+
promptForFileByExtensions(flagDef, ['.yml', '.yaml']);
179+
169180
export const promptForFlag = async (flagDef: FlaggablePrompt): Promise<string> => {
170181
const message = flagDef.promptMessage ?? flagDef.message.replace(/\.$/, '');
171182
if (flagDef.options) {

0 commit comments

Comments
 (0)