@eightshone/sshman
Version:
A simple cli ssh manager
88 lines • 4.17 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());
});
};
import select from "@inquirer/select";
import normalizeServerName from "../../utils/normalizeServerName.js";
import readConfigFile from "../../utils/readConfigFile.js";
import validateServers from "../../utils/validateServers.js";
import init from "../functions/init.js";
import saveFile from "../../utils/saveFile.js";
import { CONFIG_DIR } from "../../utils/consts.js";
function importServers(configFile, options) {
return __awaiter(this, void 0, void 0, function* () {
// intialize the cli app
let { config } = yield init();
const existingServers = [...config.servers];
// read config file
const content = yield readConfigFile(configFile);
// validate config file format
if (!validateServers(content)) {
console.log("❌ The file you are trying to import is not a valid config file!");
return;
}
const newServerNames = content.map((s) => normalizeServerName(s.name));
const hasExistingServers = existingServers.some((s) => newServerNames.includes(normalizeServerName(s.name)));
let updatedServers = [...config.servers];
// make new servers list
if (!hasExistingServers) {
updatedServers = [...existingServers, ...content];
}
else {
if (options.force) {
updatedServers = [
...existingServers.filter((s) => !newServerNames.includes(normalizeServerName(s.name))),
...content,
];
}
else {
const update = yield select({
message: "One or more server configurations already exist with the same names. Do you want to update exiting items?",
choices: [
{
name: "Yes, overwrite",
value: true,
description: "Update exisiting configs with data from imported file",
},
{
name: "No, skip",
value: false,
description: "Keep exisiting items as they are",
},
{
name: "Cancel import",
value: null,
description: "Cancel configs import and exit",
},
],
});
if (typeof update !== "boolean") {
return;
}
if (update) {
updatedServers = [
...existingServers.filter((s) => !newServerNames.includes(normalizeServerName(s.name))),
...content,
];
}
else {
const existingServerNames = existingServers.map((s) => normalizeServerName(s.name));
updatedServers = [
...existingServers,
...content.filter((s) => !existingServerNames.includes(normalizeServerName(s.name))),
];
}
}
}
config.servers = updatedServers;
yield saveFile(`${CONFIG_DIR}/config.json`, config);
console.log("✅ All server configs have been imported");
});
}
export default importServers;
//# sourceMappingURL=importServers.js.map