generated from salesforcecli/lerna-template
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtree.ts
120 lines (111 loc) · 4.04 KB
/
tree.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 { Messages, SfError } from '@salesforce/core';
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
import { ensureString, isObject } from '@salesforce/ts-types';
import { importFromPlan } from '../../../api/data/tree/importPlan.js';
import { importFromFiles } from '../../../api/data/tree/importFiles.js';
import { orgFlags } from '../../../flags.js';
import type { ImportResult, TreeResponse } from '../../../api/data/tree/importTypes.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-data', 'tree.import');
/**
* Command that provides data import capability via the SObject Tree Save API.
*/
export default class Import extends SfCommand<ImportResult[]> {
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:data:tree:import'];
public static readonly deprecateAliases = true;
public static readonly flags = {
...orgFlags,
files: Flags.string({
char: 'f',
summary: messages.getMessage('flags.files.summary'),
exactlyOne: ['files', 'plan'],
aliases: ['sobjecttreefiles'],
deprecateAliases: true,
multiple: true,
delimiter: ',',
}),
plan: Flags.file({
char: 'p',
summary: messages.getMessage('flags.plan.summary'),
description: messages.getMessage('flags.plan.description'),
exactlyOne: ['files', 'plan'],
exists: true,
}),
};
public async run(): Promise<ImportResult[]> {
const { flags } = await this.parse(Import);
const conn = flags['target-org'].getConnection(flags['api-version']);
try {
const results = flags.plan
? await importFromPlan(conn, flags.plan)
: await importFromFiles(conn, flags.files ?? []);
this.table({
data: results,
columns: [
{ key: 'refId', name: 'Reference ID' },
{ key: 'type', name: 'Type' },
{ key: 'id', name: 'ID' },
],
title: 'Import Results',
});
return results;
} catch (err) {
const error = err as Error;
if (
error.cause &&
error.cause instanceof Error &&
'data' in error.cause &&
isObject(error.cause.data) &&
'message' in error.cause.data
) {
const getTreeResponse = (payload: string): TreeResponse => {
try {
return JSON.parse(payload) as TreeResponse;
} catch (parseErr) {
// throw original error on invalid JSON payload
if (parseErr instanceof Error && parseErr.name === 'SyntaxError') {
throw error;
}
throw parseErr;
}
};
const errorData = getTreeResponse(ensureString(error.cause.data.message));
if (errorData.hasErrors) {
const errorResults = errorData.results
.map((result) =>
result.errors.map((errors) => ({
referenceId: result.referenceId,
StatusCode: errors.statusCode,
Message: errors.message,
fields: errors.fields.join(', ') || 'N/A',
}))
)
.flat();
this.table({
data: errorResults,
columns: [
{ key: 'referenceId', name: 'Reference ID' },
{ key: 'StatusCode', name: 'Status Code' },
{ key: 'Message', name: 'Error Message' },
{ key: 'fields', name: 'Fields' },
],
title: 'Tree import errors',
});
const errors = new SfError('Data Import failed');
errors.setData(errorResults);
throw errors;
}
}
throw error;
}
}
}