generated from salesforcecli/plugin-template-sf
-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.ts
202 lines (173 loc) · 7.45 KB
/
script.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* 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 fs from 'node:fs';
import os from 'node:os';
import { Flags, SfCommand } from '@salesforce/sf-plugins-core';
import { Messages } from '@salesforce/core';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-dev', 'dev.convert.script');
type Flag = {
[key: string]: {
name: string;
char: string;
aliases: string[];
};
};
type Snapshot = {
id: string;
pluginName: string;
aliases: string[];
flags?: Flag;
state?: 'deprecated';
deprecationOptions?: {
to: string;
message: string;
};
};
export default class ConvertScript extends SfCommand<void> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly flags = {
script: Flags.file({
summary: messages.getMessage('flags.script.summary'),
char: 's',
required: true,
}),
'no-prompt': Flags.boolean({
hidden: true,
default: false,
summary: messages.getMessage('flags.no-prompt.summary'),
}),
};
// this command will replace the 'sfdx' "deprecated" style commands, with the 'sf' commands
// it will not guarantee breaking changes, or changed json results
// it will scan a script line by line, and prompt the user to replace the command
// it will not replace the command if the user does not confirm
// it uses the command-snapshot.json file at the CLI level to determine the command to replace
public async run(): Promise<void> {
const { flags } = await this.parse(ConvertScript);
this.warn(messages.getMessage('warnBreakingChange'));
this.warn(messages.getMessage('warnSfdxToSf'));
this.warn(messages.getMessage('warnSfV2'));
const agreeToTerms = await this.smartConfirm(messages.getMessage('continue'), !flags['no-prompt']);
if (!agreeToTerms) {
return;
}
const contents = await fs.promises.readFile(flags.script, 'utf8');
const lines = contents.split(os.EOL);
const data: string[] = [];
// examples:
// sfdx force:user:password:generate -u myuser
// sfdx force:package:install -p 04t1I0000000X0P -w 10 -u myorg
// sfdx force:package:beta:version:list -p 04t1I0000000X0P -u myorg
// sfdx force:package:install \
// -p 04t1I0000000X0P \
// --wait 10 \
// -u myorg
for (let index = 0; index < lines.length; index++) {
let line = lines[index];
try {
// if the line looks like it's a valid sfdx command
if (line.match(/sfdx \w+/g)?.length && line.includes(':') && !line.startsWith('#')) {
// if we have a multi line command, build it together
if (line.endsWith('\\')) {
while (line.endsWith('\\')) {
line = line.concat(lines[index + 1]);
lines.splice(index + 1, 1);
}
// we'll grab the next line after the last line that ends with '\', see ~L82
line = line.concat(`${lines[index + 1]} `);
if (lines[index + 2] === '') {
// if there's a space to the next command, the multi-line commands will miss that, so check and grab it here
line = line.concat(os.EOL);
}
lines.splice(index, 1);
}
const commandId = line.split('sfdx ')[1]?.split(' ')[0];
const replacement = this.findReplacement(commandId);
// eslint-disable-next-line no-await-in-loop
line = await this.replaceCommand(commandId, replacement, line, flags['no-prompt']);
if (replacement) {
// eslint-disable-next-line no-await-in-loop
line = await this.replaceFlag(replacement, line, flags['no-prompt']);
}
}
} catch (e) {
line = line.concat(` # ${messages.getMessage('errorComment')}${os.EOL}`);
this.warn(messages.getMessage('errorComment'));
} finally {
// bare minimum replace sfdx with sf, and -u -> --target-org, -v -> --target-dev-hub
line = line.replace('sfdx ', 'sf ').replace(' -u ', ' --target-org ').replace(' -v ', ' --target-dev-hub');
data.push(line.replace(/\\ /g, `\\${os.EOL} `));
}
}
fs.writeFileSync(flags.script, data.join(os.EOL));
}
private async replaceFlag(replacement: Snapshot, line: string, prompt: boolean): Promise<string> {
// we can only replace flags for commands we know about
const replacementFlags = Object.values(replacement.flags ?? {});
// find the flags in the line, and replace them
for (const flag of (line.match(/( -\w)|( --\w+)/g) ?? []).filter((f) => f)) {
// trim down the flag to remove '-', '--' and optionally a '='
const flagName =
flag.replace(' --', '').replace(' -', '').split(' ')[0] ??
flag.replace(' --', '').replace(' -', '').split('=')[0];
// possibly undefined - but ok and handled below correctly
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const replacementFlag: string | undefined = replacementFlags.find(
(f) => f.char === flagName || f.aliases?.includes(flagName) || f.name === flagName
).name;
// don't prompt if the flag already matches the replacement
if (
replacementFlag &&
replacementFlag !== flagName &&
// eslint-disable-next-line no-await-in-loop
(await this.smartConfirm(`\t${messages.getMessage('replaceFlag', [flagName, replacementFlag])}`, !prompt))
) {
line = line.replace(flag, ` --${replacementFlag}${flag.includes('=') ? '=' : ''}`);
}
}
return line;
}
private async replaceCommand(
commandId: string,
replacement: Snapshot,
line: string,
prompt: boolean
): Promise<string> {
// meaning it's deprecated to multiple commands and can't be replaced inline
// or if there is no "to", but there is a "message", then print the message
const depMessage = replacement.deprecationOptions?.message;
const depTo = replacement.deprecationOptions?.to;
if (depTo?.includes('/') ?? (!depTo && depMessage)) {
this.warn(messages.getMessage('cannotDetermine', [commandId]));
if (depMessage) {
this.warn(depMessage);
}
}
if (replacement) {
// we can only replace flags for commands we know about
const commandWithSpaces = (depTo ?? replacement.id).replace(/:/g, ' ');
if (await this.smartConfirm(messages.getMessage('replaceCommand', [commandId, commandWithSpaces]), !prompt)) {
line = line.replace(commandId, commandWithSpaces);
}
}
return line;
}
private async smartConfirm(message: string, prompt = true): Promise<boolean> {
return prompt ? this.confirm({ message, ms: 18_000_000 }) : true;
}
private findReplacement(commandId: string): Snapshot {
// first find the commands's aliases that match the commandId - and get their plugin name
// from the plugin find the command that matches the commandId or the alias
const pluginName = this.config.commands.find((c) => c.id === commandId)?.pluginName;
const plugin = this.config.plugins.get(pluginName ?? '');
return plugin?.commands.find((c) => c.id === commandId || c.aliases.includes(commandId)) as Snapshot;
}
}