Skip to content

Commit

Permalink
fix(@angular/cli): handle cases when completion is enabled and runnin…
Browse files Browse the repository at this point in the history
…g 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

(cherry picked from commit 2731fe7)
  • Loading branch information
alan-agius4 committed Jul 7, 2022
1 parent dfa6d73 commit 1785505
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions packages/angular/cli/lib/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { VERSION } from '../src/utilities/version';
*/
let forceExit = false;

(async () => {
(async (): Promise<typeof import('./cli').default | null> => {
/**
* 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.
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 1785505

Please sign in to comment.