From 7816d121ceb41dd9c703bb528870ca4b776663ff Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Thu, 7 Jul 2022 17:41:49 +0000 Subject: [PATCH] fix(@angular/cli): handle cases when completion is enabled and running in an older CLI workspace Previously when having completion enabled and the current workspaces has an older version of the Angular CLI installed in the terminal the below errors is show. This is because the older versions of the CLI do not implement this command. Now we exit gracefully. ``` The specified command ("completion") is invalid. For a list of available options, run "ng help". Did you mean "analytics"? ``` Closes #23518 --- packages/angular/cli/lib/init.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/angular/cli/lib/init.ts b/packages/angular/cli/lib/init.ts index e4ee414f05e9..ce4014a4db80 100644 --- a/packages/angular/cli/lib/init.ts +++ b/packages/angular/cli/lib/init.ts @@ -24,7 +24,7 @@ import { VERSION } from '../src/utilities/version'; */ let forceExit = false; -(async () => { +(async (): Promise => { /** * Disable Browserslist old data warning as otherwise with every release we'd need to update this dependency * which is cumbersome considering we pin versions and the warning is not user actionable. @@ -42,6 +42,8 @@ let forceExit = false; } let cli; + const rawCommandName = process.argv[2]; + try { // No error implies a projectLocalCli, which will load whatever // version of ng-cli you have installed in a local package.json @@ -68,6 +70,11 @@ let forceExit = false; // Ensure older versions of the CLI fully exit if (major(localVersion) < 14) { forceExit = true; + + // Versions prior to 14 didn't implement completion command. + if (rawCommandName === 'completion') { + return null; + } } let isGlobalGreater = false; @@ -78,7 +85,6 @@ let forceExit = false; console.error('Version mismatch check skipped. Unable to compare local version: ' + error); } - const rawCommandName = process.argv[2]; // When using the completion command, don't show the warning as otherwise this will break completion. if (isGlobalGreater && rawCommandName !== 'completion') { // If using the update command and the global version is greater, use the newer update command @@ -114,14 +120,12 @@ let forceExit = false; return cli; })() - .then((cli) => { - return cli({ + .then((cli) => + cli?.({ cliArgs: process.argv.slice(2), - inputStream: process.stdin, - outputStream: process.stdout, - }); - }) - .then((exitCode: number) => { + }), + ) + .then((exitCode = 0) => { if (forceExit) { process.exit(exitCode); }