Repository navigation
Expand file tree
/
Copy pathtree.ts
More file actions
132 lines (123 loc) · 4.49 KB
/
Copy pathtree.ts
File metadata and controls
132 lines (123 loc) · 4.49 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright 2026, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 { ImportResults, 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<ImportResults> {
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<ImportResults> {
const { flags } = await this.parse(Import);
const conn = flags['target-org'].getConnection(flags['api-version']);
try {
const { results, warnings } = flags.plan
? await importFromPlan(conn, flags.plan)
: await importFromFiles(conn, flags.files ?? []);
for (const warning of warnings) {
this.warn(warning);
}
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;
}
}
}