generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 21
/
source.ts
172 lines (155 loc) · 5.94 KB
/
source.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* 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 { resolve } from 'node:path';
import fs from 'node:fs';
import { Messages } from '@salesforce/core';
import {
ComponentSet,
ComponentSetBuilder,
ConvertResult,
MetadataConverter,
RegistryAccess,
} from '@salesforce/source-deploy-retrieve';
import {
arrayWithDeprecation,
Flags,
loglevel,
orgApiVersionFlagWithDeprecations,
SfCommand,
} from '@salesforce/sf-plugins-core';
import { Interfaces } from '@oclif/core';
import { getPackageDirs, getSourceApiVersion } from '../../../utils/project.js';
import { SourceConvertResultFormatter } from '../../../formatters/sourceConvertResultFormatter.js';
import { ConvertResultJson } from '../../../utils/types.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-deploy-retrieve', 'convert.source');
export class Source extends SfCommand<ConvertResultJson> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly requiresProject = true;
public static readonly aliases = ['force:source:convert'];
public static readonly deprecateAliases = true;
public static readonly flags = {
'api-version': { ...orgApiVersionFlagWithDeprecations, summary: messages.getMessage('flags.api-version.summary') },
loglevel,
'root-dir': Flags.directory({
aliases: ['rootdir'],
deprecateAliases: true,
char: 'r',
summary: messages.getMessage('flags.root-dir.summary'),
exists: true,
}),
'output-dir': Flags.directory({
aliases: ['outputdir'],
deprecateAliases: true,
default: `metadataPackage_${Date.now()}`,
defaultHelp: async () => Promise.resolve('metadataPackage_>timestamp<'),
char: 'd',
summary: messages.getMessage('flags.output-dir.summary'),
}),
'package-name': Flags.string({
char: 'n',
aliases: ['packagename'],
deprecateAliases: true,
summary: messages.getMessage('flags.package-name.summary'),
}),
manifest: Flags.file({
char: 'x',
summary: messages.getMessage('flags.manifest.summary'),
description: messages.getMessage('flags.manifest.description'),
exists: true,
}),
'source-dir': arrayWithDeprecation({
char: 'p',
aliases: ['sourcepath'],
deprecateAliases: true,
description: messages.getMessage('flags.source-dir.description'),
summary: messages.getMessage('flags.source-dir.summary'),
exclusive: ['manifest', 'metadata'],
}),
metadata: arrayWithDeprecation({
char: 'm',
summary: messages.getMessage('flags.metadata.summary'),
exclusive: ['manifest', 'sourcepath'],
}),
};
protected convertResult!: ConvertResult;
private flags!: Interfaces.InferredFlags<typeof Source.flags>;
private componentSet!: ComponentSet;
public async run(): Promise<ConvertResultJson> {
this.flags = (await this.parse(Source)).flags;
await this.convert();
this.resolveSuccess();
return this.formatResult();
}
protected async convert(): Promise<void> {
const paths: string[] = [];
const { metadata, manifest } = this.flags;
const sourcepath = this.flags['source-dir'];
const rootdir = this.flags['root-dir'];
if (sourcepath) {
paths.push(...sourcepath);
}
// rootdir behaves exclusively to sourcepath, metadata, and manifest... to maintain backwards compatibility
// we will check here, instead of adding the exclusive option to the flag definition so we don't break scripts
if (rootdir && !sourcepath && !metadata && !manifest && typeof rootdir === 'string') {
// only rootdir option passed
paths.push(rootdir);
}
// no options passed, convert the default package (usually force-app)
if (!sourcepath && !metadata && !manifest && !rootdir) {
paths.push(this.project!.getDefaultPackage().path);
}
this.componentSet = await ComponentSetBuilder.build({
sourceapiversion: this.flags['api-version'] ?? (await getSourceApiVersion()),
sourcepath: paths,
manifest: manifest
? {
manifestPath: manifest,
directoryPaths: await getPackageDirs(),
}
: undefined,
metadata: metadata
? {
metadataEntries: metadata,
directoryPaths: await getPackageDirs(),
}
: undefined,
projectDir: this.project?.getPath(),
});
const packageName = this.flags['package-name'];
const outputDirectory = resolve(this.flags['output-dir']);
const registry = new RegistryAccess(undefined, this.project?.getPath());
const converter = new MetadataConverter(registry);
this.convertResult = await converter.convert(this.componentSet, 'metadata', {
type: 'directory',
outputDirectory,
packageName,
genUniqueDir: false,
});
if (packageName && this.convertResult.packagePath) {
// SDR will build an output path like /output/directory/packageName/package.xml
// this was breaking from toolbelt, so to revert it we copy the directory up a level and delete the original
fs.cpSync(this.convertResult.packagePath, outputDirectory, { recursive: true });
fs.rmSync(this.convertResult.packagePath, { recursive: true });
this.convertResult.packagePath = outputDirectory;
}
}
protected resolveSuccess(): void {
if (!this.convertResult.packagePath) {
process.exitCode = 1;
}
}
protected async formatResult(): Promise<ConvertResultJson> {
const formatter = new SourceConvertResultFormatter(this.convertResult);
if (!this.jsonEnabled()) {
formatter.display();
}
return formatter.getJson();
}
}