alwaysai
Version:
The alwaysAI command-line interface (CLI)
94 lines • 4.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.findOrWritePrivateKeyFileComponent = exports.PUBLIC_KEY_FILE_COMMENT = void 0;
const fs_1 = require("fs");
const child_process_1 = require("child_process");
const util_1 = require("util");
const alwayscli_1 = require("@alwaysai/alwayscli");
const mkdirp = require("mkdirp");
const path_1 = require("path");
const paths_1 = require("../../paths");
const environment_1 = require("../../environment");
const util_2 = require("../../util");
const confirm_write_file_prompt_component_1 = require("./confirm-write-file-prompt-component");
const WRITE_MESSAGE = `Write ${paths_1.PRIVATE_KEY_FILE_PRETTY_PATH}`;
exports.PUBLIC_KEY_FILE_COMMENT = 'Generated by the alwaysAI CLI';
async function findOrWritePrivateKeyFileComponent(props) {
const { yes } = props;
if ((0, fs_1.existsSync)(paths_1.PRIVATE_KEY_FILE_PATH)) {
// Make sure that the public part of the key is in place. It should be if it
// was created with ssh-keygen. It might not be if the private key was
// copied to this host from elsewhere.
if (!(0, fs_1.existsSync)(paths_1.PUBLIC_KEY_FILE_PATH)) {
const confirmed = yes ||
(await (0, confirm_write_file_prompt_component_1.confirmWriteFilePromptComponent)({
fileName: paths_1.PUBLIC_KEY_FILE_PRETTY_PATH,
description: 'Public key file'
}));
if (!confirmed) {
throw new alwayscli_1.CliTerseError((0, util_2.UnableToProceedWithoutMessage)(paths_1.PUBLIC_KEY_FILE_PATH));
}
const spinner = (0, util_2.Spinner)(`Write ${paths_1.PUBLIC_KEY_FILE_PRETTY_PATH}`);
try {
const { stdout } = await (0, util_1.promisify)(child_process_1.execFile)('ssh-keygen', [
'-y',
'-f',
paths_1.PRIVATE_KEY_FILE_PATH
]);
await (0, fs_1.writeFileSync)(paths_1.PUBLIC_KEY_FILE_PATH, stdout, { flag: 'wx' });
spinner.succeed();
}
catch (exception) {
if (exception.code === 'EEXIST') {
spinner.succeed();
// Unlikely scenario that the file did not exist but now does
}
else {
spinner.fail();
throw exception;
}
}
}
}
else {
// !exists
const confirmed = yes ||
(await (0, confirm_write_file_prompt_component_1.confirmWriteFilePromptComponent)({
fileName: paths_1.PRIVATE_KEY_FILE_PRETTY_PATH,
description: 'Private key file'
}));
if (!confirmed) {
throw new alwayscli_1.CliTerseError((0, util_2.UnableToProceedWithoutMessage)(paths_1.PRIVATE_KEY_FILE_PRETTY_PATH));
}
// ssh-keygen does not automatically create the .ssh directory on Windows if
// it does not already exist. On non-Windows let's just let ssh-keygen
// create the directory so that it has proper permissions.
if (environment_1.ALWAYSAI_OS_PLATFORM === 'win32') {
mkdirp.sync((0, path_1.dirname)(paths_1.PRIVATE_KEY_FILE_PATH));
}
// ssh-keygen creates the private key file and the corresponding .pub file
const spinner = (0, util_2.Spinner)(WRITE_MESSAGE);
try {
await (0, util_1.promisify)(child_process_1.execFile)('ssh-keygen', [
'-q',
'-b',
'2048',
'-t',
'rsa',
'-N',
'',
'-C',
exports.PUBLIC_KEY_FILE_COMMENT,
'-f',
paths_1.PRIVATE_KEY_FILE_PATH
]);
spinner.succeed();
}
catch (exception) {
spinner.fail();
throw exception;
}
}
}
exports.findOrWritePrivateKeyFileComponent = findOrWritePrivateKeyFileComponent;
//# sourceMappingURL=find-or-write-private-key-file-component.js.map