completion.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.CompletionCommand = void 0;
  4. const tslib_1 = require("tslib");
  5. const cli_framework_1 = require("@ionic/cli-framework");
  6. const utils_terminal_1 = require("@ionic/utils-terminal");
  7. const path = tslib_1.__importStar(require("path"));
  8. const color_1 = require("../lib/color");
  9. const command_1 = require("../lib/command");
  10. const errors_1 = require("../lib/errors");
  11. class CompletionCommand extends command_1.Command {
  12. async getMetadata() {
  13. return {
  14. name: 'completion',
  15. type: 'global',
  16. summary: 'Enables tab-completion for Ionic CLI commands.',
  17. description: `
  18. This command is experimental and only works for Z shell (zsh) and non-Windows platforms.
  19. To enable completions for the Ionic CLI, you can add the completion code that this command prints to your ${(0, color_1.strong)('~/.zshrc')} (or any other file loaded with your shell). See the examples.
  20. `,
  21. groups: ["experimental" /* MetadataGroup.EXPERIMENTAL */],
  22. exampleCommands: [
  23. '',
  24. '>> ~/.zshrc',
  25. ],
  26. };
  27. }
  28. async run(inputs, options) {
  29. if (utils_terminal_1.TERMINAL_INFO.windows) {
  30. throw new errors_1.FatalException('Completion is not supported on Windows shells.');
  31. }
  32. if (path.basename(utils_terminal_1.TERMINAL_INFO.shell) !== 'zsh') {
  33. throw new errors_1.FatalException('Completion is currently only available for Z Shell (zsh).');
  34. }
  35. const words = options['--'];
  36. if (!words || words.length === 0) {
  37. const namespace = this.namespace.root;
  38. const formatter = new cli_framework_1.ZshCompletionFormatter({ namespace });
  39. process.stdout.write(await formatter.format());
  40. return;
  41. }
  42. const ns = this.namespace.root;
  43. const outputWords = await (0, cli_framework_1.getCompletionWords)(ns, words.slice(1));
  44. process.stdout.write(outputWords.join(' '));
  45. }
  46. }
  47. exports.CompletionCommand = CompletionCommand;