-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathretrieveResultFormatter.ts
More file actions
110 lines (100 loc) · 3.82 KB
/
Copy pathretrieveResultFormatter.ts
File metadata and controls
110 lines (100 loc) · 3.82 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
/*
* 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 path from 'node:path';
import { Ux } from '@salesforce/sf-plugins-core';
import { FileResponse, RetrieveMessage, RetrieveResult } from '@salesforce/source-deploy-retrieve';
import { NamedPackageDir, SfProject } from '@salesforce/core';
import { ensureArray } from '@salesforce/kit';
import { Formatter, isSdrSuccess, RetrieveResultJson } from '../utils/types.js';
import { tableHeader, getFileResponseSuccessProps, fileResponseSortFn, makePathRelative } from '../utils/output.js';
export class RetrieveResultFormatter implements Formatter<RetrieveResultJson> {
private files: FileResponse[];
public constructor(
private ux: Ux,
private result: RetrieveResult,
private packageNames: string[] = [],
deleteResponses: FileResponse[] = []
) {
this.files = (this.result.getFileResponses() ?? []).concat(deleteResponses);
}
// eslint-disable-next-line @typescript-eslint/require-await
public async getJson(): Promise<RetrieveResultJson> {
const { zipFile, ...responseWithoutZip } = this.result.response;
return { ...responseWithoutZip, files: this.files };
}
public async display(): Promise<void> {
this.displaySuccesses();
await this.displayPackages();
}
private displaySuccesses(): void {
const successes = this.files
.filter(isSdrSuccess)
.map(makePathRelative)
.sort(fileResponseSortFn)
.map(getFileResponseSuccessProps);
if (!successes.length) {
// a retrieve happened, but nothing was retrieved
if (this.result.response.status) {
this.ux.warn('Nothing retrieved');
} else {
// a retrieve didn't happen, probably because everything was ignored or there were no remote changes to retrieve
this.ux.log('Nothing retrieved');
}
} else {
this.ux.log();
this.ux.table({
data: successes,
columns: ['state', { key: 'fullName', name: 'Name' }, 'type', { key: 'filePath', name: 'Path' }],
title: tableHeader('Retrieved Source'),
overflow: 'wrap',
});
}
const warnings = getWarnings(this.result);
if (warnings.length) {
this.ux.log();
this.ux.table({
data: warnings,
columns: [{ key: 'fileName', name: 'File' }, 'problem'],
title: tableHeader('Warnings'),
overflow: 'wrap',
});
}
}
private async displayPackages(): Promise<void> {
const packages = await this.getPackages();
if (packages?.length) {
this.ux.log();
this.ux.warn('Metadata from retrieved packages is meant for your reference only, not development.');
this.ux.table({
data: packages,
columns: [
{ key: 'name', name: 'Package Name' },
{ key: 'fullPath', name: 'Converted Location' },
],
title: tableHeader('Retrieved Packages'),
overflow: 'wrap',
});
}
}
private async getPackages(): Promise<NamedPackageDir[]> {
const projectPath = await SfProject.resolveProjectPath();
return this.packageNames.map((name) => {
const packagePath = path.join(projectPath, name);
return { name, path: packagePath, fullPath: path.resolve(packagePath) };
});
}
}
const getWarnings = (result: RetrieveResult): RetrieveMessage[] => ensureArray(result?.response?.messages ?? []);