@onboardbase/cli
Version:
[](https://www.npmjs.com/package/@onboardbase/cli) [](https://www.npmjs.com/package/@onboardbase/cli) [ • 4.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFallbackCommands = exports.createOnboardbaseProfile = exports.createFallbackCommands = exports.getCommandsFallbackDirectory = exports.transformCommandInputToObject = void 0;
const chalk = require("chalk");
const fs_1 = require("fs");
const path_1 = require("path");
const _1 = require(".");
const commands_1 = require("../http/commands");
const transformCommandInputToObject = async (teamId, global) => {
const dataObject = { fallback: false };
try {
const fallbackCommand = await (0, exports.getFallbackCommands)("command", teamId, global);
dataObject.fallback = false;
fallbackCommand.map((datum) => {
const presenceOfVariable = datum.commands.indexOf("$") !== -1;
dataObject[datum.executor] = Object.assign(Object.assign({}, datum), { presenceOfVariable });
});
}
catch (error) {
console.log(chalk.green("fetch from local storage failed, fetching from remote server ..."));
const { data } = await (0, commands_1.getAllCommands)();
const dataArray = global ? data.findAllPublicCommands : data.commands;
dataArray.map((datum) => {
const presenceOfVariable = datum.commandList.indexOf("$") !== -1;
dataObject[datum.executor] = {
commands: datum.commandList.split(","),
id: datum.id,
name: datum.name,
description: datum.description,
presenceOfVariable,
};
});
}
return dataObject;
};
exports.transformCommandInputToObject = transformCommandInputToObject;
const getCommandsFallbackDirectory = async (global) => {
const teamFallbackDirectory = (0, path_1.join)((0, _1.getFallbackDirectory)(), "commands");
const gloablFallbackDirectory = (0, path_1.join)((0, _1.getFallbackDirectory)(), "gcommands");
return global ? gloablFallbackDirectory : teamFallbackDirectory;
};
exports.getCommandsFallbackDirectory = getCommandsFallbackDirectory;
const createFallbackCommands = async (teamId, commandsData, filename, global = false) => {
try {
const commandsDirectory = await (0, exports.getCommandsFallbackDirectory)(global);
if (!(0, _1.isExist)(commandsDirectory)) {
(0, fs_1.mkdirSync)(commandsDirectory);
}
(0, fs_1.writeFileSync)((0, path_1.join)(commandsDirectory, `${filename}_${teamId}`), JSON.stringify(commandsData));
}
catch (error) {
console.log("An error occured while trying to cache commands locally");
}
};
exports.createFallbackCommands = createFallbackCommands;
const createOnboardbaseProfile = async (data, global = false) => {
const onboardbaseProfilePath = (0, path_1.join)((0, _1.getHomeDirectory)(), ".onboardbase", ".obbrc");
const globalOnboardbaseProfilePath = (0, path_1.join)((0, _1.getHomeDirectory)(), ".onboardbase", ".obbgrc");
const activeRCPath = global
? globalOnboardbaseProfilePath
: onboardbaseProfilePath;
const profileHomePath = (0, path_1.join)((0, _1.getHomeDirectory)(), ".profile");
try {
if ((0, _1.isUnix)()) {
if (!(0, fs_1.existsSync)(activeRCPath)) {
(0, fs_1.appendFileSync)(activeRCPath, data);
(0, _1.executeCommand)(`printf "source ${activeRCPath}" >> ${profileHomePath}`, {});
}
else {
(0, fs_1.appendFileSync)(activeRCPath, data);
}
console.log(`Run 'source ${activeRCPath}' to start using saved aliases immediately`);
}
if (process.platform === "win32") {
console.log("Aliases do not work for Windows just yet..");
}
}
catch (error) {
console.error("An error occured while trying to create a profile file for aliases");
}
};
exports.createOnboardbaseProfile = createOnboardbaseProfile;
const getFallbackCommands = async (filename, teamId, global) => {
const fallbackPath = await (0, exports.getCommandsFallbackDirectory)(global);
if (!(0, _1.isExist)(fallbackPath)) {
console.log("Could not access commands and no fallback exist for this team");
process.exit(1);
}
const fallbackFile = (0, fs_1.readFileSync)((0, path_1.join)(fallbackPath, `${filename}_${teamId}`), {
encoding: "utf8",
});
return JSON.parse(fallbackFile);
};
exports.getFallbackCommands = getFallbackCommands;