|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { promises as fs } from 'fs'; |
| 10 | +import * as path from 'path'; |
| 11 | +import { env } from 'process'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Sets up autocompletion for the user's terminal. This attempts to find the configuration file for |
| 15 | + * the current shell (`.bashrc`, `.zshrc`, etc.) and append a command which enables autocompletion |
| 16 | + * for the Angular CLI. Supports only Bash and Zsh. Returns whether or not it was successful. |
| 17 | + * @return The full path of the configuration file modified. |
| 18 | + */ |
| 19 | +export async function initializeAutocomplete(): Promise<string> { |
| 20 | + // Get the currently active `$SHELL` and `$HOME` environment variables. |
| 21 | + const shell = env['SHELL']; |
| 22 | + if (!shell) { |
| 23 | + throw new Error( |
| 24 | + '`$SHELL` environment variable not set. Angular CLI autocompletion only supports Bash or' + |
| 25 | + ' Zsh.', |
| 26 | + ); |
| 27 | + } |
| 28 | + const home = env['HOME']; |
| 29 | + if (!home) { |
| 30 | + throw new Error( |
| 31 | + '`$HOME` environment variable not set. Setting up autocompletion modifies configuration files' + |
| 32 | + ' in the home directory and must be set.', |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + // Get all the files we can add `ng completion` to which apply to the user's `$SHELL`. |
| 37 | + const runCommandCandidates = getShellRunCommandCandidates(shell, home); |
| 38 | + if (!runCommandCandidates) { |
| 39 | + throw new Error( |
| 40 | + `Unknown \`$SHELL\` environment variable value (${shell}). Angular CLI autocompletion only supports Bash or Zsh.`, |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + // Get the first file that already exists or fallback to a new file of the first candidate. |
| 45 | + const candidates = await Promise.allSettled( |
| 46 | + runCommandCandidates.map((rcFile) => fs.access(rcFile).then(() => rcFile)), |
| 47 | + ); |
| 48 | + const rcFile = |
| 49 | + candidates.find( |
| 50 | + (result): result is PromiseFulfilledResult<string> => result.status === 'fulfilled', |
| 51 | + )?.value ?? runCommandCandidates[0]; |
| 52 | + |
| 53 | + // Append Angular autocompletion setup to RC file. |
| 54 | + try { |
| 55 | + await fs.appendFile( |
| 56 | + rcFile, |
| 57 | + '\n\n# Load Angular CLI autocompletion.\nsource <(ng completion)\n', |
| 58 | + ); |
| 59 | + } catch (err) { |
| 60 | + throw new Error(`Failed to append autocompletion setup to \`${rcFile}\`:\n${err.message}`); |
| 61 | + } |
| 62 | + |
| 63 | + return rcFile; |
| 64 | +} |
| 65 | + |
| 66 | +/** Returns an ordered list of possibile candidates of RC files used by the given shell. */ |
| 67 | +function getShellRunCommandCandidates(shell: string, home: string): string[] | undefined { |
| 68 | + if (shell.toLowerCase().includes('bash')) { |
| 69 | + return ['.bashrc', '.bash_profile', '.profile'].map((file) => path.join(home, file)); |
| 70 | + } else if (shell.toLowerCase().includes('zsh')) { |
| 71 | + return ['.zshrc', '.zsh_profile', '.profile'].map((file) => path.join(home, file)); |
| 72 | + } else { |
| 73 | + return undefined; |
| 74 | + } |
| 75 | +} |
0 commit comments