generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 13
/
metadata.ts
87 lines (78 loc) · 3.24 KB
/
metadata.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
/*
* 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 fs from 'node:fs';
import { Messages } from '@salesforce/core';
import type { FileProperties, ListMetadataQuery } from '@jsforce/jsforce-node/lib/api/metadata.js';
import { Flags, loglevel, requiredOrgFlagWithDeprecations, SfCommand } from '@salesforce/sf-plugins-core';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-org', 'list.metadata');
export type ListMetadataCommandResult = FileProperties[];
export class ListMetadata extends SfCommand<ListMetadataCommandResult> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly aliases = ['force:mdapi:listmetadata'];
public static readonly deprecateAliases = true;
public static readonly flags = {
'api-version': Flags.orgApiVersion({
aliases: ['apiversion', 'a'],
deprecateAliases: true,
summary: messages.getMessage('flags.api-version.summary'),
}),
loglevel,
'target-org': requiredOrgFlagWithDeprecations,
'output-file': Flags.file({
aliases: ['resultfile'],
deprecateAliases: true,
char: 'f',
summary: messages.getMessage('flags.output-file.summary'),
}),
'metadata-type': Flags.string({
aliases: ['metadatatype'],
deprecateAliases: true,
char: 'm',
summary: messages.getMessage('flags.metadata-type.summary'),
required: true,
}),
folder: Flags.string({
summary: messages.getMessage('flags.folder.summary'),
description: messages.getMessage('flags.folder.description'),
}),
};
public async run(): Promise<ListMetadataCommandResult> {
const { flags } = await this.parse(ListMetadata);
const conn = flags['target-org'].getConnection(flags['api-version']);
const query: ListMetadataQuery = flags.folder
? { type: flags['metadata-type'], folder: flags.folder }
: { type: flags['metadata-type'] };
const listResult = await conn.metadata.list(query, flags['api-version']);
if (flags['output-file']) {
fs.writeFileSync(flags['output-file'], JSON.stringify(listResult, null, 2));
this.logSuccess(`Wrote result file to ${flags['output-file']}.`);
} else if (listResult?.length) {
this.table({
data: listResult.map((md) => ({
'Created By': md.createdByName,
'Created Date': md.createdDate.split('T')[0],
'Full Name': md.fullName,
Id: md.id,
'Last Modified By': md.lastModifiedByName,
'Last Modified': md.lastModifiedDate.split('T')[0],
'Manageable State': md.manageableState,
'Namespace Prefix': md.namespacePrefix,
})),
title: flags['metadata-type'],
sort: {
'Manageable State': 'asc',
},
});
} else {
this.warn(messages.getMessage('noMatchingMetadata', [flags['metadata-type'], conn.getUsername()]));
}
return listResult;
}
}