Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
fix: handle non-existent npm executable
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Sep 9, 2021
1 parent c799408 commit 2cca0ba
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions src/hooks/postupdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,50 @@ function sendEvent(data: JsonMap): void {
*/
// eslint-disable-next-line @typescript-eslint/require-await
const hook = async function (): Promise<void> {
const sfdxVersion = exec('sfdx --version', { silent: true }).stdout;
const npmInstallation = which('npm').stdout;
if (!npmInstallation) {
const sfdxVersion = exec('sfdx --version', { silent: true })?.stdout || 'unknown';
try {
const npmInstallation = which('npm')?.stdout;
if (!npmInstallation) {
sendEvent({
eventName: 'POST_SFDX_UPDATE_SF_INSTALL_ERROR',
type: 'EVENT',
message: 'npm not installed on machine',
sfdxVersion,
});
return;
}

const installResult = exec('npm install -g @salesforce/cli', { silent: true });
if (installResult.code > 0) {
sendEvent({
eventName: 'POST_SFDX_UPDATE_SF_INSTALL_ERROR',
type: 'EVENT',
message: 'npm global install failed',
stackTrace: installResult.stderr?.replace(new RegExp(os.homedir(), 'g'), AppInsights.GDPR_HIDDEN),
sfdxVersion,
});
return;
}

const sfVersion = exec('sf --version', { silent: true }).stdout;
sendEvent({
eventName: 'POST_SFDX_UPDATE_SF_INSTALL_ERROR',
eventName: 'POST_SFDX_UPDATE_SF_INSTALL_SUCCESS',
type: 'EVENT',
message: 'npm not installed on machine',
message: 'sf install succeeded',
sfVersion,
sfdxVersion,
});
return;
}

const installResult = exec('npm install -g @salesforce/cli', { silent: true });
if (installResult.code > 0) {
} catch (error) {
const err = error as Error;
sendEvent({
eventName: 'POST_SFDX_UPDATE_SF_INSTALL_ERROR',
type: 'EVENT',
message: 'npm global install failed',
stackTrace: installResult.stdout.replace(new RegExp(os.homedir(), 'g'), AppInsights.GDPR_HIDDEN),
message: err.message,
stackTrace: err?.stack?.replace(new RegExp(os.homedir(), 'g'), AppInsights.GDPR_HIDDEN),
sfdxVersion,
});
return;
}

const sfVersion = exec('sf --version', { silent: true }).stdout;
sendEvent({
eventName: 'POST_SFDX_UPDATE_SF_INSTALL_SUCCESS',
type: 'EVENT',
message: 'sf install succeeded',
sfVersion,
sfdxVersion,
});
};

export default hook;

0 comments on commit 2cca0ba

Please sign in to comment.