@jalasem/dss
Version:
Dev Spaces Switcher (DSS) - Seamlessly manage isolated development environments with separate SSH keys and Git configurations. Enhanced UI with beautiful tables and improved documentation.
131 lines (130 loc) • 6.09 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.copyToClipboard = exports.testGithubAccess = exports.removeSSHKeyFromAgent = exports.setGitHubSSHKey = void 0;
const child_process_1 = require("child_process");
const util_1 = require("util");
const os_1 = __importDefault(require("os"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const prompts_1 = require("@inquirer/prompts");
const ui_1 = require("./ui");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
function setGitHubSSHKey(sshKeyPath) {
return __awaiter(this, void 0, void 0, function* () {
const sshConfigPath = path_1.default.join(os_1.default.homedir(), '.ssh', 'config');
const hostConfig = `Host github.com\n HostName github.com\n User git\n IdentityFile ${sshKeyPath}\n IdentitiesOnly yes\n`;
try {
yield fs_extra_1.default.ensureFile(sshConfigPath);
let sshConfig = yield fs_extra_1.default.readFile(sshConfigPath, 'utf8');
const githubConfigIndex = sshConfig.indexOf('Host github.com');
if (githubConfigIndex !== -1) {
const nextHostIndex = sshConfig.indexOf('Host ', githubConfigIndex + 1);
if (nextHostIndex !== -1) {
sshConfig = sshConfig.slice(0, githubConfigIndex) + hostConfig + sshConfig.slice(nextHostIndex);
}
else {
sshConfig = sshConfig.slice(0, githubConfigIndex) + hostConfig;
}
}
else {
sshConfig += `\n${hostConfig}`;
}
yield fs_extra_1.default.writeFile(sshConfigPath, sshConfig);
ui_1.UIHelper.success('SSH config for GitHub updated successfully.');
}
catch (error) {
ui_1.UIHelper.error('Failed to update SSH config for GitHub: ' + error.message);
}
});
}
exports.setGitHubSSHKey = setGitHubSSHKey;
function removeSSHKeyFromAgent(sshKeyPath) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield execAsync(`ssh-add -d ${sshKeyPath}`);
ui_1.UIHelper.success("SSH key removed from ssh-agent successfully.");
}
catch (error) {
ui_1.UIHelper.error("Error removing SSH key from ssh-agent: " + error.message);
}
});
}
exports.removeSSHKeyFromAgent = removeSSHKeyFromAgent;
function testGithubAccess(sshKeyPath) {
return __awaiter(this, void 0, void 0, function* () {
ui_1.UIHelper.printHeader("Testing SSH Access to GitHub");
try {
yield execAsync(`ssh-add ${sshKeyPath}`);
try {
yield execAsync('ssh -T git@github.com');
ui_1.UIHelper.success("🎉 Space configuration works! You've successfully authenticated with GitHub.");
}
catch (error) {
const { stderr } = error;
if (stderr.includes("successfully authenticated")) {
ui_1.UIHelper.success("🎉 Space configuration works! You've successfully authenticated with GitHub.");
}
else {
ui_1.UIHelper.error("🚨 Error testing SSH access to GitHub: " + error.message);
}
}
const showPublicKey = yield (0, prompts_1.confirm)({
message: "Would you like to see the public SSH key?",
default: false,
});
if (!showPublicKey)
return;
const publicKeyPath = `${sshKeyPath}.pub`;
const publicKey = yield fs_extra_1.default.readFile(publicKeyPath, 'utf8');
console.log(ui_1.UIHelper.dim("\nPublic SSH Key:"));
console.log(ui_1.UIHelper.highlight(publicKey));
}
catch (error) {
ui_1.UIHelper.error("🚨 Error testing SSH access to GitHub: " + error.message);
ui_1.UIHelper.info("Ensure the SSH key has been added to the ssh-agent and is associated with your GitHub account.");
}
});
}
exports.testGithubAccess = testGithubAccess;
const copyToClipboard = (publicKey) => {
return new Promise((resolve, reject) => {
// Platform-specific command to copy the SSH public key to clipboard
let copyCommand;
switch (process.platform) {
case "darwin":
copyCommand = "pbcopy";
break;
case "win32":
copyCommand = "clip";
break;
case "linux":
copyCommand = "xclip -selection clipboard";
break;
default:
ui_1.UIHelper.error(`Platform ${process.platform} is not supported for clipboard operations.`);
reject(new Error("Unsupported platform for clipboard operations."));
return;
}
(0, child_process_1.exec)(`echo "${publicKey}" | ${copyCommand}`, (error) => {
if (error) {
reject(error);
}
else {
resolve("Public SSH key copied to clipboard.");
}
});
});
};
exports.copyToClipboard = copyToClipboard;