Skip to content

Wr/replace multiline commands #402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/commands/dev/convert/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,31 @@ export default class ConvertScript extends SfCommand<void> {
// sfdx force:package:install -p 04t1I0000000X0P -w 10 -u myorg
// sfdx force:package:beta:version:list -p 04t1I0000000X0P -u myorg

for (let line of lines) {
// 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);
Expand All @@ -92,12 +113,12 @@ export default class ConvertScript extends SfCommand<void> {
}
}
} catch (e) {
line = line.concat(` # ${messages.getMessage('errorComment')}`);
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);
data.push(line.replace(/\\ /g, `\\${os.EOL} `));
}
}

Expand Down Expand Up @@ -156,7 +177,7 @@ export default class ConvertScript extends SfCommand<void> {

if (replacement) {
// we can only replace flags for commands we know about
const commandWithSpaces = replacement.id.replace(/:/g, ' ');
const commandWithSpaces = (depTo ?? replacement.id).replace(/:/g, ' ');
if (await this.smartConfirm(messages.getMessage('replaceCommand', [commandId, commandWithSpaces]), !prompt)) {
line = line.replace(commandId, commandWithSpaces);
}
Expand Down
21 changes: 21 additions & 0 deletions test/commands/dev/convert/samples/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,24 @@ sfdx force:apex:test:run -u myorg -c -r=human -w 10

sfdx alias:set user=myuser
sfdx config:set defaultusername=user -g

# multi line commands
sfdx force:user:create -f config/user-def.json \
-a myuser \
-o myorg

sfdx alias:set user=myuser

sfdx force:package:beta:version:list \
-p 04t1I0000000X0P

sfdx package version promote \
-p 04t1I0000000X0P \
--target-dev-hub1.0.0.Beta_1 \
--target-org myorg

sf force org create \
--definitionfile config/project-scratch-def.json \
--setalias myorg \
--setdefaultusername \
--durationdays 1