generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 18
/
convert.ts
152 lines (132 loc) · 5.32 KB
/
convert.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
/*
* 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 * as os from 'os';
import { join, resolve } from 'path';
import * as fs from 'fs';
import { flags, FlagsConfig } from '@salesforce/command';
import { Messages } from '@salesforce/core';
import { ComponentSetBuilder, ConvertResult, MetadataConverter } from '@salesforce/source-deploy-retrieve';
import { getString } from '@salesforce/ts-types';
import { SourceCommand } from '../../../sourceCommand';
import { ConvertCommandResult, ConvertResultFormatter } from '../../../formatters/convertResultFormatter';
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-source', 'convert');
export class Convert extends SourceCommand {
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessage('examples').split(os.EOL);
public static readonly requiresProject = true;
public static readonly flagsConfig: FlagsConfig = {
rootdir: flags.directory({
char: 'r',
description: messages.getMessage('flags.rootdir'),
}),
outputdir: flags.directory({
default: `metadataPackage_${Date.now()}`,
char: 'd',
description: messages.getMessage('flags.outputdir'),
}),
packagename: flags.string({
char: 'n',
description: messages.getMessage('flags.packagename'),
}),
manifest: flags.string({
char: 'x',
description: messages.getMessage('flags.manifest'),
longDescription: messages.getMessage('flagsLong.manifest'),
}),
sourcepath: flags.array({
char: 'p',
description: messages.getMessage('flags.sourcepath'),
longDescription: messages.getMessage('flagsLong.sourcepath'),
exclusive: ['manifest', 'metadata'],
}),
metadata: flags.array({
char: 'm',
description: messages.getMessage('flags.metadata'),
exclusive: ['manifest', 'sourcepath'],
}),
};
protected convertResult: ConvertResult;
public async run(): Promise<ConvertCommandResult> {
await this.convert();
this.resolveSuccess();
return this.formatResult();
}
protected async convert(): Promise<void> {
const paths: string[] = [];
const { sourcepath, metadata, manifest, rootdir } = this.flags;
if (sourcepath) {
paths.push(...(sourcepath as string[]));
}
// 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: await this.getSourceApiVersion(),
sourcepath: paths,
manifest: manifest && {
manifestPath: this.getFlag<string>('manifest'),
directoryPaths: this.getPackageDirs(),
},
metadata: metadata && {
metadataEntries: this.getFlag<string[]>('metadata'),
directoryPaths: this.getPackageDirs(),
},
});
const packageName = this.getFlag<string>('packagename');
const outputDirectory = resolve(this.getFlag<string>('outputdir'));
const converter = new MetadataConverter();
this.convertResult = await converter.convert(this.componentSet, 'metadata', {
type: 'directory',
outputDirectory,
packageName,
genUniqueDir: false,
});
if (packageName) {
// 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
this.copyDir(this.convertResult.packagePath, outputDirectory);
try {
fs.rmSync(this.convertResult.packagePath, { recursive: true });
} catch (e) {
// rmdirSync is being deprecated and emits a warning
// but rmSync is introduced in node 14 so fall back to rmdirSync
fs.rmdirSync(this.convertResult.packagePath, { recursive: true });
}
this.convertResult.packagePath = outputDirectory;
}
}
// TODO: move into fs in sfdx-core
protected copyDir(src: string, dest: string): void {
fs.mkdirSync(dest, { recursive: true });
const entries = fs.readdirSync(src, { withFileTypes: true });
entries.map((entry) => {
const srcPath = join(src, entry.name);
const destPath = join(dest, entry.name);
return entry.isDirectory() ? this.copyDir(srcPath, destPath) : fs.copyFileSync(srcPath, destPath);
});
}
protected resolveSuccess(): void {
if (!getString(this.convertResult, 'packagePath')) {
this.setExitCode(1);
}
}
protected formatResult(): ConvertCommandResult {
const formatter = new ConvertResultFormatter(this.logger, this.ux, this.convertResult);
if (!this.isJsonOutput()) {
formatter.display();
}
return formatter.getJson();
}
}