Skip to content

Commit fb06228

Browse files
committed
feat(@angular/cli): don't prompt to set up autocompletion for ng update and ng completion commands
`ng update` is most likely called when upgrading a project to the next version and users should be more concerned about their project than their personal terminal setup. `ng completion` is unconditionally setting up autocompletion, while `ng completion script` is getting the shell script for autocompletion setup. As a result, both of these don't benefit from a prompt and should be safe to skip it.
1 parent 2e15df9 commit fb06228

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

packages/angular/cli/src/command-builder/command-module.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ export abstract class CommandModule<T extends {} = {}> implements CommandModuleI
125125
}
126126

127127
// Set up autocompletion if appropriate.
128-
const autocompletionExitCode = await considerSettingUpAutocompletion(this.context.logger);
128+
const autocompletionExitCode = await considerSettingUpAutocompletion(
129+
this.commandName,
130+
this.context.logger,
131+
);
129132
if (autocompletionExitCode !== undefined) {
130133
process.exitCode = autocompletionExitCode;
131134

packages/angular/cli/src/utilities/completion.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ interface CompletionConfig {
3131
* @returns an exit code if the program should terminate, undefined otherwise.
3232
*/
3333
export async function considerSettingUpAutocompletion(
34+
command: string,
3435
logger: logging.Logger,
3536
): Promise<number | undefined> {
3637
// Check if we should prompt the user to setup autocompletion.
3738
const completionConfig = await getCompletionConfig();
38-
if (!(await shouldPromptForAutocompletionSetup(completionConfig))) {
39+
if (!(await shouldPromptForAutocompletionSetup(command, completionConfig))) {
3940
return undefined; // Already set up or prompted previously, nothing to do.
4041
}
4142

@@ -106,12 +107,20 @@ async function setCompletionConfig(config: CompletionConfig): Promise<void> {
106107
await wksp.save();
107108
}
108109

109-
async function shouldPromptForAutocompletionSetup(config?: CompletionConfig): Promise<boolean> {
110+
async function shouldPromptForAutocompletionSetup(
111+
command: string,
112+
config?: CompletionConfig,
113+
): Promise<boolean> {
110114
// Force whether or not to prompt for autocomplete to give an easy path for e2e testing to skip.
111115
if (forceAutocomplete !== undefined) {
112116
return forceAutocomplete;
113117
}
114118

119+
// Don't prompt on `ng update` or `ng completion`.
120+
if (command === 'update' || command === 'completion') {
121+
return false;
122+
}
123+
115124
// Non-interactive and continuous integration systems don't care about autocompletion.
116125
if (!isTTY()) {
117126
return false;

tests/legacy-cli/e2e/tests/misc/completion-prompt.ts

+25
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,31 @@ export default async function () {
240240
}
241241
});
242242

243+
// Does *not* prompt for `ng update` commands.
244+
await mockHome(async (home) => {
245+
// Use `ng update --help` so it's actually a no-op and we don't need to setup a project.
246+
const { stdout } = await execWithEnv('ng', ['update', '--help'], {
247+
...DEFAULT_ENV,
248+
HOME: home,
249+
});
250+
251+
if (AUTOCOMPLETION_PROMPT.test(stdout)) {
252+
throw new Error('`ng update` command incorrectly prompted for autocompletion setup.');
253+
}
254+
});
255+
256+
// Does *not* prompt for `ng completion` commands.
257+
await mockHome(async (home) => {
258+
const { stdout } = await execWithEnv('ng', ['completion'], {
259+
...DEFAULT_ENV,
260+
HOME: home,
261+
});
262+
263+
if (AUTOCOMPLETION_PROMPT.test(stdout)) {
264+
throw new Error('`ng completion` command incorrectly prompted for autocompletion setup.');
265+
}
266+
});
267+
243268
// Does *not* prompt user for CI executions.
244269
{
245270
const { stdout } = await execWithEnv('ng', ['version'], {

0 commit comments

Comments
 (0)