-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.ts
96 lines (84 loc) · 3.52 KB
/
create.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright (c) 2024, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as fs from 'node:fs';
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
import { Lifecycle, Messages } from '@salesforce/core';
import { MultiStageOutput } from '@oclif/multi-stage-output';
import { colorize } from '@oclif/core/ux';
import { Agent, AgentCreateConfig, AgentCreateLifecycleStages } from '@salesforce/agents';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-agent', 'agent.create');
export type AgentCreateResult = {
isSuccess: boolean;
errorMessage?: string;
};
export default class AgentCreate extends SfCommand<AgentCreateResult> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly requiresProject = true;
public static state = 'beta';
public static readonly flags = {
'target-org': Flags.requiredOrg(),
'api-version': Flags.orgApiVersion(),
spec: Flags.file({
char: 'f',
required: true,
summary: messages.getMessage('flags.spec.summary'),
}),
name: Flags.string({
char: 'n',
required: true,
summary: messages.getMessage('flags.name.summary'),
}),
};
public async run(): Promise<AgentCreateResult> {
const { flags } = await this.parse(AgentCreate);
const jsonParsingStage = `Parsing ${flags.spec}`;
const mso = new MultiStageOutput({
jsonEnabled: this.jsonEnabled(),
title: `Creating ${flags.name} Agent`,
stages: [
jsonParsingStage,
'Generating local metadata',
'Deploying metadata to org',
'Creating Agent in org',
'Retrieving Agent metadata',
],
});
mso.goto(jsonParsingStage);
const agentConfig = {
...(JSON.parse(fs.readFileSync(flags.spec, 'utf8')) as AgentCreateConfig),
name: flags.name,
};
// @ts-expect-error not using async method in callback
Lifecycle.getInstance().on(AgentCreateLifecycleStages.CreatingLocally, () => mso.goto('Generating local metadata'));
Lifecycle.getInstance().on(AgentCreateLifecycleStages.DeployingMetadata, () =>
// @ts-expect-error not using async method in callback
mso.goto('Deploying metadata to org')
);
// @ts-expect-error not using async method in callback
Lifecycle.getInstance().on(AgentCreateLifecycleStages.CreatingRemotely, () => mso.goto('Creating Agent in org'));
Lifecycle.getInstance().on(AgentCreateLifecycleStages.RetrievingMetadata, () =>
// @ts-expect-error not using async method in callback
mso.goto('Retrieving Agent metadata')
);
const agent = new Agent(flags['target-org'].getConnection(flags['api-version']), this.project!);
const created = await agent.create(agentConfig);
mso.stop();
this.log(
created.isSuccess
? colorize(
'green',
`Successfully created ${flags.name} in ${flags['target-org'].getUsername() ?? 'the target org'}.`
)
: colorize('red', `failed to create agent ${flags.name}: ${created.errorMessage ?? ''}`)
);
this.log(`Use ${colorize('dim', `sf org open agent --name ${flags.name}`)} to view the agent in the browser.`);
return { isSuccess: created.isSuccess };
}
}