generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
create.ts
120 lines (110 loc) · 4.41 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Copyright (c) 2020, 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 type { JsonMap } from '@salesforce/ts-types';
import { Messages, SfError } from '@salesforce/core';
import {
Flags,
loglevel,
orgApiVersionFlagWithDeprecations,
requiredOrgFlagWithDeprecations,
SfCommand,
parseVarArgs,
} from '@salesforce/sf-plugins-core';
import { ConnectExecutor } from '../../shared/connect/services/ConnectExecutor.js';
import { CommunityCreateResource } from '../../shared/community/connect/CommunityCreateResource.js';
import { CommunityCreateResponse } from '../../shared/community/defs/CommunityCreateResponse.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-community', 'create');
const MESSAGE_KEY = 'message';
const NAME_KEY = 'name';
const JOBID_KEY = 'jobId';
const ACTION_KEY = 'action';
/**
* A command to create a community.
* This is just an sfdx wrapper around the community create connect endpoint
*/
export class CommunityCreateCommand extends SfCommand<CommunityCreateResponse> {
public static readonly deprecateAliases = true;
public static readonly aliases = ['force:community:create'];
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly strict = false;
public static readonly flags = {
name: Flags.string({
char: 'n',
summary: messages.getMessage('flags.name.summary'),
required: true,
}),
'template-name': Flags.string({
char: 't',
summary: messages.getMessage('flags.template-name.summary'),
description: messages.getMessage('flags.template-name.description'),
required: true,
deprecateAliases: true,
aliases: ['templatename'],
}),
'url-path-prefix': Flags.string({
char: 'p',
summary: messages.getMessage('flags.url-path-prefix.summary'),
description: messages.getMessage('flags.url-path-prefix.description'),
// The api requires you to pass this, it accepts an empty string
default: '',
deprecateAliases: true,
aliases: ['urlpathprefix'],
}),
description: Flags.string({
char: 'd',
summary: messages.getMessage('flags.description.summary'),
description: messages.getMessage('flags.description.description'),
}),
'target-org': requiredOrgFlagWithDeprecations,
loglevel,
'api-version': orgApiVersionFlagWithDeprecations,
};
public async run(): Promise<CommunityCreateResponse> {
const { flags, argv, args } = await this.parse(CommunityCreateCommand);
const templateParams = getTemplateParamObjectFromArgs(parseVarArgs(args, argv as string[]));
const createCommand = new CommunityCreateResource({
name: flags.name,
urlPathPrefix: flags['url-path-prefix'],
templateName: flags['template-name'],
description: flags.description,
templateParams,
});
return new ConnectExecutor(createCommand, flags['target-org'].getConnection(flags['api-version']))
.callConnectApi()
.then((results) => {
this.displayResults(results);
return results;
});
}
private displayResults(results: CommunityCreateResponse): void {
const columns = {
[NAME_KEY]: { header: 'Name' },
[MESSAGE_KEY]: { header: 'Message' },
[JOBID_KEY]: { header: 'JobId' },
[ACTION_KEY]: { header: 'Action' },
};
this.styledHeader(messages.getMessage('response.styleHeader'));
this.table([results], columns, { 'no-truncate': true });
}
}
export const getTemplateParamObjectFromArgs = (args: Record<string, string | undefined>): JsonMap => {
// make sure there's nothing bad
const badArgs = Object.keys(args).filter((key) => !key.startsWith('templateParams'));
if (badArgs.length) {
throw new SfError(`Invalid argument(s): ${badArgs.join(', ')}`, 'InvalidArgument', [
'Arguments should start with templateParams, like templateParams.AuthenticationType=UNAUTHENTICATED',
]);
}
// construct the return object
const templateParams = Object.fromEntries(
Object.entries(args).map(([key, value]) => [key.replace('templateParams.', ''), value])
);
return templateParams;
};