Skip to content

Commit 656706e

Browse files
refactored code and moved outer catch block to inner catch block
1 parent 6af4520 commit 656706e

File tree

1 file changed

+46
-43
lines changed

1 file changed

+46
-43
lines changed

src/spec-common/injectHeadless.ts

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -484,22 +484,22 @@ async function runLifecycleCommand({ lifecycleHook }: ResolverParameters, contai
484484
},
485485
onDidChangeDimensions: lifecycleHook.output.onDidChangeDimensions,
486486
}, LogLevel.Info);
487-
try {
488-
const remoteCwd = containerProperties.remoteWorkspaceFolder || containerProperties.homeFolder;
489-
async function runSingleCommand(postCommand: string | string[], name?: string) {
490-
const progressDetail = typeof postCommand === 'string' ? postCommand : postCommand.join(' ');
491-
infoOutput.event({
492-
type: 'progress',
493-
name: progressName,
494-
status: 'running',
495-
stepDetail: progressDetail
496-
});
487+
const remoteCwd = containerProperties.remoteWorkspaceFolder || containerProperties.homeFolder;
488+
async function runSingleCommand(postCommand: string | string[], name?: string) {
489+
const progressDetail = typeof postCommand === 'string' ? postCommand : postCommand.join(' ');
490+
infoOutput.event({
491+
type: 'progress',
492+
name: progressName,
493+
status: 'running',
494+
stepDetail: progressDetail
495+
});
497496

498-
// If we have a command name then the command is running in parallel and
499-
// we need to hold output until the command is done so that the output
500-
// doesn't get interleaved with the output of other commands.
501-
const printMode = name ? 'off' : 'continuous';
502-
const env = { ...(await remoteEnv), ...(await secrets) };
497+
// If we have a command name then the command is running in parallel and
498+
// we need to hold output until the command is done so that the output
499+
// doesn't get interleaved with the output of other commands.
500+
const printMode = name ? 'off' : 'continuous';
501+
const env = { ...(await remoteEnv), ...(await secrets) };
502+
try {
503503
const { cmdOutput } = await runRemoteCommand({ ...lifecycleHook, output: infoOutput }, containerProperties, typeof postCommand === 'string' ? ['/bin/sh', '-c', postCommand] : postCommand, remoteCwd, { remoteEnv: env, pty: true, print: printMode });
504504

505505
// 'name' is set when parallel execution syntax is used.
@@ -512,38 +512,41 @@ async function runLifecycleCommand({ lifecycleHook }: ResolverParameters, contai
512512
name: progressName,
513513
status: 'succeeded',
514514
});
515+
} catch (err) {
516+
infoOutput.event({
517+
type: 'progress',
518+
name: progressName,
519+
status: 'failed',
520+
});
521+
if (err?.cmdOutput) {
522+
infoOutput.raw(`\r\n\x1b[1m${err.cmdOutput}\x1b[0m\r\n\r\n`);
523+
}
524+
if (err && (err.code === 130 || err.signal === 2)) { // SIGINT seen on darwin as code === 130, would also make sense as signal === 2.
525+
infoOutput.raw(`\r\n\x1b[1m${lifecycleHookName} interrupted.\x1b[0m\r\n\r\n`);
526+
} else {
527+
if (err?.code) {
528+
infoOutput.write(toErrorText(`${lifecycleHookName} failed with exit code ${err.code}. Skipping any further user-provided commands.`));
529+
}
530+
throw new ContainerError({
531+
description: `The ${lifecycleHookName} in the ${userCommandOrigin} failed.`,
532+
originalError: err
533+
});
534+
}
515535
}
536+
}
516537

517-
infoOutput.raw(`\x1b[1mRunning the ${lifecycleHookName} from ${userCommandOrigin}...\x1b[0m\r\n\r\n`);
538+
infoOutput.raw(`\x1b[1mRunning the ${lifecycleHookName} from ${userCommandOrigin}...\x1b[0m\r\n\r\n`);
518539

519-
let commands;
520-
if (typeof userCommand === 'string' || Array.isArray(userCommand)) {
521-
commands = [runSingleCommand(userCommand)];
522-
} else {
523-
commands = Object.keys(userCommand).map(name => {
524-
const command = userCommand[name];
525-
return runSingleCommand(command, name);
526-
});
527-
}
528-
await Promise.all(commands);
529-
} catch (err) {
530-
infoOutput.event({
531-
type: 'progress',
532-
name: progressName,
533-
status: 'failed',
540+
let commands;
541+
if (typeof userCommand === 'string' || Array.isArray(userCommand)) {
542+
commands = [runSingleCommand(userCommand)];
543+
} else {
544+
commands = Object.keys(userCommand).map(name => {
545+
const command = userCommand[name];
546+
return runSingleCommand(command, name);
534547
});
535-
if (err && (err.code === 130 || err.signal === 2)) { // SIGINT seen on darwin as code === 130, would also make sense as signal === 2.
536-
infoOutput.raw(`\r\n\x1b[1m${lifecycleHookName} interrupted.\x1b[0m\r\n\r\n`);
537-
} else {
538-
if (err?.code) {
539-
infoOutput.write(toErrorText(`${lifecycleHookName} failed with exit code ${err.code}. Skipping any further user-provided commands.`));
540-
}
541-
throw new ContainerError({
542-
description: `The ${lifecycleHookName} in the ${userCommandOrigin} failed.`,
543-
originalError: err
544-
});
545-
}
546548
}
549+
await Promise.all(commands);
547550
}
548551
}
549552

@@ -641,7 +644,7 @@ export async function runRemoteCommand(params: { output: Log; onDidInput?: Event
641644
}
642645
if (exit.code || exit.signal) {
643646
return Promise.reject({
644-
message: `Command failed: ${cmd.join(' ')}\r\n\r\n${cmdOutput}`,
647+
message: `Command failed: ${cmd.join(' ')}`,
645648
cmdOutput,
646649
code: exit.code,
647650
signal: exit.signal,

0 commit comments

Comments
 (0)