setup.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.SSHSetupCommand = void 0;
  27. const utils_fs_1 = require("@ionic/utils-fs");
  28. const utils_terminal_1 = require("@ionic/utils-terminal");
  29. const color_1 = require("../../lib/color");
  30. const errors_1 = require("../../lib/errors");
  31. const executor_1 = require("../../lib/executor");
  32. const base_1 = require("./base");
  33. class SSHSetupCommand extends base_1.SSHBaseCommand {
  34. async getMetadata() {
  35. const dashUrl = this.env.config.getDashUrl();
  36. return {
  37. name: 'setup',
  38. type: 'global',
  39. summary: 'Setup your Ionic SSH keys automatically',
  40. description: `
  41. This command offers a setup wizard for Ionic SSH keys using a series of prompts. For more control, see the commands available for managing SSH keys with the ${(0, color_1.input)('ionic ssh --help')} command. For an entirely manual approach, see ${(0, color_1.strong)('Personal Settings')} => ${(0, color_1.strong)('SSH Keys')} in the Dashboard[^dashboard-settings-ssh-keys].
  42. If you are having issues setting up SSH keys, please get in touch with our Support[^support-request].
  43. `,
  44. footnotes: [
  45. {
  46. id: 'dashboard-settings-ssh-keys',
  47. url: `${dashUrl}/settings/ssh-keys`,
  48. },
  49. {
  50. id: 'support-request',
  51. url: 'https://ion.link/support-request',
  52. },
  53. ],
  54. groups: ["deprecated" /* MetadataGroup.DEPRECATED */],
  55. };
  56. }
  57. async preRun() {
  58. await this.checkForOpenSSH();
  59. }
  60. async run(inputs, options, runinfo) {
  61. const { getGeneratedPrivateKeyPath } = await Promise.resolve().then(() => __importStar(require('../../lib/ssh')));
  62. const { getConfigPath } = await Promise.resolve().then(() => __importStar(require('../../lib/ssh-config')));
  63. const { promptToLogin } = await Promise.resolve().then(() => __importStar(require('../../lib/session')));
  64. if (!this.env.session.isLoggedIn()) {
  65. await promptToLogin(this.env);
  66. }
  67. const CHOICE_AUTOMATIC = 'automatic';
  68. const CHOICE_MANUAL = 'manual';
  69. const CHOICE_SKIP = 'skip';
  70. const CHOICE_IGNORE = 'ignore';
  71. if (this.env.config.get('git.setup')) {
  72. const rerun = await this.env.prompt({
  73. type: 'confirm',
  74. name: 'confirm',
  75. message: `SSH setup wizard has run before. Would you like to run it again?`,
  76. });
  77. if (!rerun) {
  78. return;
  79. }
  80. }
  81. else {
  82. this.env.log.msg(`Looks like you haven't configured your SSH settings yet.`);
  83. }
  84. // TODO: link to docs about manual git setup
  85. const setupChoice = await this.env.prompt({
  86. type: 'list',
  87. name: 'setupChoice',
  88. message: `How would you like to connect to Ionic?`,
  89. choices: [
  90. {
  91. name: 'Automatically setup new a SSH key pair for Ionic',
  92. value: CHOICE_AUTOMATIC,
  93. },
  94. {
  95. name: 'Use an existing SSH key pair',
  96. value: CHOICE_MANUAL,
  97. },
  98. {
  99. name: 'Skip for now',
  100. value: CHOICE_SKIP,
  101. },
  102. {
  103. name: 'Ignore this prompt forever',
  104. value: CHOICE_IGNORE,
  105. },
  106. ],
  107. });
  108. if (setupChoice === CHOICE_AUTOMATIC) {
  109. const sshconfigPath = getConfigPath();
  110. const keyPath = await getGeneratedPrivateKeyPath(this.env.config.get('user.id'));
  111. const pubkeyPath = `${keyPath}.pub`;
  112. const [pubkeyExists, keyExists] = await Promise.all([(0, utils_fs_1.pathExists)(keyPath), (0, utils_fs_1.pathExists)(pubkeyPath)]);
  113. if (!pubkeyExists && !keyExists) {
  114. this.env.log.info('The automatic SSH setup will do the following:\n' +
  115. `1) Generate a new SSH key pair with OpenSSH (will not overwrite any existing keys).\n` +
  116. `2) Upload the generated SSH public key to our server, registering it on your account.\n` +
  117. `3) Modify your SSH config (${(0, color_1.strong)((0, utils_terminal_1.prettyPath)(sshconfigPath))}) to use the generated SSH private key for our server(s).`);
  118. const confirm = await this.env.prompt({
  119. type: 'confirm',
  120. name: 'confirm',
  121. message: 'May we proceed?',
  122. });
  123. if (!confirm) {
  124. throw new errors_1.FatalException();
  125. }
  126. }
  127. if (pubkeyExists && keyExists) {
  128. this.env.log.msg(`Using your previously generated key: ${(0, color_1.strong)((0, utils_terminal_1.prettyPath)(keyPath))}.\n` +
  129. `You can generate a new one by deleting it.`);
  130. }
  131. else {
  132. await (0, executor_1.runCommand)(runinfo, ['ssh', 'generate', keyPath]);
  133. }
  134. await (0, executor_1.runCommand)(runinfo, ['ssh', 'add', pubkeyPath, '--use']);
  135. }
  136. else if (setupChoice === CHOICE_MANUAL) {
  137. await (0, executor_1.runCommand)(runinfo, ['ssh', 'add']);
  138. }
  139. if (setupChoice === CHOICE_SKIP) {
  140. this.env.log.warn(`Skipping for now. You can configure your SSH settings using ${(0, color_1.input)('ionic ssh setup')}.`);
  141. }
  142. else {
  143. if (setupChoice === CHOICE_IGNORE) {
  144. this.env.log.ok(`We won't pester you about SSH settings anymore!`);
  145. }
  146. else {
  147. this.env.log.ok('SSH setup successful!');
  148. }
  149. this.env.config.set('git.setup', true);
  150. }
  151. }
  152. }
  153. exports.SSHSetupCommand = SSHSetupCommand;