Skip to content

Commit db3cac8

Browse files
chore: finish changing to --api-name
1 parent dc11eb1 commit db3cac8

File tree

5 files changed

+74
-16
lines changed

5 files changed

+74
-16
lines changed

messages/agent.publish.authoring-bundle.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ Invalid bundle path. Please provide a valid path to an Agent Authoring Bundle.
3131

3232
Failed to publish agent with the following errors:
3333
%s
34+
35+
# error.afscriptNotFound
36+
37+
Could not find an .afscript file with API name '%s' in the project.
38+
39+
# error.afscriptNotFoundAction
40+
41+
Please check that the API name is correct and that the .afscript file exists in your project directory.

messages/agent.validate.authoring-bundle.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ Invalid bundle path. Please provide a valid path to an Agent Authoring Bundle.
2727

2828
AF Script compilation failed with the following errors:
2929
%s
30+
31+
# error.afscriptNotFound
32+
33+
Could not find an .afscript file with API name '%s' in the project.
34+
35+
# error.afscriptNotFoundAction
36+
37+
Please check that the API name is correct and that the .afscript file exists in your project directory.

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
* Licensed under the BSD 3-Clause license.
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
7-
import { resolve } from 'node:path';
8-
import { existsSync } from 'node:fs';
97
import { EOL } from 'node:os';
108
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
11-
import { Messages, Lifecycle } from '@salesforce/core';
9+
import { Messages, Lifecycle, SfError } from '@salesforce/core';
1210
import { Agent } from '@salesforce/agents';
1311
import { RetrieveResult, RequestStatus } from '@salesforce/source-deploy-retrieve';
1412
import { ensureArray } from '@salesforce/kit';
13+
import { findAndReadAfScript } from '../../../utils/afscriptFinder.js';
1514

1615
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
1716
const messages = Messages.loadMessages('@salesforce/plugin-agent', 'agent.publish.authoring-bundle');
@@ -40,11 +39,12 @@ export default class AgentPublishAuthoringBundle extends SfCommand<AgentPublishA
4039

4140
public async run(): Promise<AgentPublishAuthoringBundleResult> {
4241
const { flags } = await this.parse(AgentPublishAuthoringBundle);
43-
const bundlePath = resolve(flags['api-name']);
42+
const afScript = findAndReadAfScript(this.project!.getPath(), flags['api-name']);
4443

45-
// Validate bundle path exists
46-
if (!existsSync(bundlePath)) {
47-
throw messages.createError('error.invalidBundlePath');
44+
if (!afScript) {
45+
throw new SfError(messages.getMessage('error.afscriptNotFound', [flags['api-name']]), 'AfScriptNotFoundError', [
46+
messages.getMessage('error.afscriptNotFoundAction'),
47+
]);
4848
}
4949

5050
try {
@@ -66,7 +66,7 @@ export default class AgentPublishAuthoringBundle extends SfCommand<AgentPublishA
6666

6767
// First compile the AF script to get the Agent JSON
6868
this.log('Compiling authoring bundle...');
69-
const agentJson = await Agent.compileAfScript(conn, bundlePath);
69+
const agentJson = await Agent.compileAfScript(conn, afScript);
7070

7171
// Then publish the Agent JSON to create the agent
7272
this.log('Publishing agent...');

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
* Licensed under the BSD 3-Clause license.
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
7-
import { resolve } from 'node:path';
8-
import { existsSync } from 'node:fs';
97
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
10-
import { Messages } from '@salesforce/core';
8+
import { Messages, SfError } from '@salesforce/core';
119
import { Agent } from '@salesforce/agents';
10+
import { findAndReadAfScript } from '../../../utils/afscriptFinder.js';
1211

1312
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
1413
const messages = Messages.loadMessages('@salesforce/plugin-agent', 'agent.validate.authoring-bundle');
@@ -35,18 +34,19 @@ export default class AgentValidateAuthoringBundle extends SfCommand<AgentValidat
3534

3635
public async run(): Promise<AgentValidateAuthoringBundleResult> {
3736
const { flags } = await this.parse(AgentValidateAuthoringBundle);
38-
const bundlePath = resolve(flags['api-name']);
37+
const afScript = findAndReadAfScript(this.project!.getPath(), flags['api-name']);
3938

40-
// Validate bundle path exists
41-
if (!existsSync(bundlePath)) {
42-
throw messages.createError('error.invalidBundlePath');
39+
if (!afScript) {
40+
throw new SfError(messages.getMessage('error.afscriptNotFound', [flags['api-name']]), 'AfScriptNotFoundError', [
41+
messages.getMessage('error.afscriptNotFoundAction'),
42+
]);
4343
}
4444

4545
try {
4646
const targetOrg = flags['target-org'];
4747
const conn = targetOrg.getConnection(flags['api-version']);
4848
// Call Agent.compileAfScript() API
49-
await Agent.compileAfScript(conn, bundlePath);
49+
await Agent.compileAfScript(conn, afScript);
5050
this.log('Successfully compiled');
5151
return {
5252
success: true,

src/utils/afscriptFinder.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2023, 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+
import { join } from 'node:path';
8+
import { readdirSync, readFileSync, statSync } from 'node:fs';
9+
import { type AfScript } from '@salesforce/agents';
10+
11+
/**
12+
* Finds an .afscript file with the given API name in the project directory.
13+
* Searches for pattern: any/file/path/authoringbundles/<apiName>/<apiName>.afscript
14+
*
15+
* @param projectDir - The root directory to start searching from
16+
* @param apiName - The API name to search for
17+
* @returns The full path to the .afscript file if found, undefined otherwise
18+
*/
19+
export function findAndReadAfScript(projectDir: string, apiName: string): AfScript | undefined {
20+
const walk = (dir: string): string | undefined => {
21+
const files = readdirSync(dir);
22+
23+
// If we find authoringbundles dir, check for the expected file structure
24+
if (files.includes('authoringbundles')) {
25+
const expectedPath = join(dir, 'authoringbundles', apiName, `${apiName}.afscript`);
26+
if (statSync(expectedPath, { throwIfNoEntry: false })?.isFile()) {
27+
return readFileSync(expectedPath, 'utf-8');
28+
}
29+
}
30+
31+
// Otherwise keep searching directories
32+
for (const file of files) {
33+
const filePath = join(dir, file);
34+
if (statSync(filePath, { throwIfNoEntry: false })?.isDirectory()) {
35+
const found = walk(filePath);
36+
if (found) return found;
37+
}
38+
}
39+
};
40+
41+
return walk(projectDir);
42+
}

0 commit comments

Comments
 (0)