Skip to content

Commit

Permalink
Improve code quality for overal package
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolap994 committed Dec 15, 2023
1 parent 4dbe2e7 commit 1f61935
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 102 deletions.
85 changes: 59 additions & 26 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,86 @@
#!/usr/bin/env node

// Import required modules
const colors = require("colors");
const navnode = require("../lib/navnode");
const path = require("path");

/**
* The main entry point for the Navnode command-line utility.
*/

// Parse command line arguments
const arguments = process.argv.slice(2);
const arg = arguments[0];
const [env, task] = arg.split(":");
const additionalArgument = arguments[1];

// Check if the required argument is missing
if (!arg) {
console.log(colors.red("Missing environment or task"));
} else if (arg === "init") {
// Handle 'init' command
handleInitCommand();
} else {
// Handle other Navnode commands
handleNavnodeCommand(env, task, additionalArgument);
}

/**
* Handles the 'init' command, creating the configuration file if it does not exist.
* @returns {Object|undefined} The configuration object if it exists, otherwise undefined.
*/
function handleInitCommand() {
// Specify the configuration file name
const configFile = "navnode-deployment.js";

try {
// Attempt to require the configuration file
const config = require(path.resolve(`./${configFile}`));
console.log(`${configFile} already found.`);
return config;
} catch (err) {
// Handle the case when the configuration file is not found
if (err.code === "MODULE_NOT_FOUND") {
console.log(`${configFile} not found. Creating the file...`);
navnode.init();
console.log(`${configFile} created successfully. Please configure it.`);
}
}
}

if (env && task) {
/**
* Handles other Navnode commands based on the provided environment and task.
* @param {string} env - The name of the environment.
* @param {string} task - The name of the task.
* @param {string} additionalArgument - Additional argument for specific tasks.
*/
function handleNavnodeCommand(env, task, additionalArgument) {
// Check if environment or task is missing
if (!env || !task) {
console.log(colors.red("Missing environment or task"));
return;
}

// Display information based on the task
if (task.includes("deploy")) {
console.log(colors.blue(`Navnode deploying to ${env}`));
} else if (!task.includes("rsync")) {
console.log(colors.blue(`Navnode executing ${env}:${task}`));
}

if (task === "rsync_pull") {
console.log(colors.blue(`Navnode syncing files from ${env}`));
navnode.rsyncPull(env, arguments[1]);
} else if (task === "rsync_push") {
console.log(colors.blue(`Navnode syncing files to ${env}`));
navnode.rsyncPush(env, arguments[1]);
// Handle rsync tasks
if (task === "rsync_pull" || task === "rsync_push") {
const direction = task === "rsync_pull" ? "from" : "to";
console.log(colors.blue(`Navnode syncing files ${direction} ${env}`));
navnode[`rsync${task.split("_")[1]}`](env, additionalArgument);
} else {
// Handle deployment task
const deployment = navnode.deploy(env, task);

// Display deployment information
if (typeof deployment !== "undefined") {
console.log(colors.blue(deployment));
}
}
} else {
if (arg === "init") {
const configFile = "navnode-deployment.js";
try {
const config = require(path.resolve(`./${configFile}`));
if (config) {
console.log(`${configFile} already found.`);
}
return config;
} catch (err) {
if (err.code === "MODULE_NOT_FOUND") {
console.log(`${configFile} not found. Creating the file...`);
navnode.init();
console.log(`${configFile} created successfully. Please configure it.`);
}
}
} else {
console.log(colors.red("Missing environment or task"));
}
}
154 changes: 80 additions & 74 deletions lib/navnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ const fs = require("fs");
*/
function getNavnodeConfig() {
try {
const config = require(path.resolve("./navnode-deployment.js"));
return config;
return require(path.resolve("./navnode-deployment.js"));
} catch (err) {
console.log(colors.bgRed(err));
exit();
handleError(err);
}
}

Expand All @@ -27,8 +25,7 @@ function getConfig(env) {
try {
return getNavnodeConfig().environments[env];
} catch (err) {
console.log(colors.bgRed(err));
exit();
handleError(err);
}
}

Expand All @@ -41,74 +38,96 @@ function getTask(task) {
try {
return getNavnodeConfig().tasks[task].task;
} catch (err) {
console.log(colors.bgRed(err));
exit();
handleError(err);
}
}

/**
* Deploys code to a specific environment.
* Handles errors by logging and exiting the process.
* @param {Error} err - The error object.
*/
function handleError(err) {
console.log(colors.bgRed(err));
exit();
}

/**
* Executes SSH commands on a remote server for deployment.
* @param {string} env - The name of the environment to deploy to.
* @param {string} task - The name of the task to execute during deployment.
*/
exports.deploy = function (env, task) {
if (env) {
try {
const environmentConfig = getConfig(env);
const user = environmentConfig.user;
const url = `${user}@${environmentConfig.server}`;
const remotePath = environmentConfig.path;

const commands = getTask(task);

commands.forEach((command) => {
if (command.includes("--navnode_extend")) {
try {
command = command.replace("--navnode_extend", "").trim();

const config = getNavnodeConfig();

config.extend[command]();
} catch (err) {
console.log(colors.bgRed(err));
}
return;
if (!env) {
console.log(colors.red("Missing environment"));
return;
}

try {
const environmentConfig = getConfig(env);
const { user, server, path: remotePath } = environmentConfig;
const url = `${user}@${server}`;

const commands = getTask(task);

commands.forEach((command) => {
if (command.includes("--navnode_extend")) {
try {
command = command.replace("--navnode_extend", "").trim();
const config = getNavnodeConfig();
config.extend[command]();
} catch (err) {
handleError(err);
}
const sshCommand = `cd ${remotePath} && ${command}`;
const finalCommand = `ssh ${url} "${sshCommand}"`;
const response = execSync(finalCommand).toString();

console.log("----------------------------------");
console.log(colors.green(response));
});
} catch (err) {
console.log(colors.bgRed(err));
}
return;
}

const sshCommand = `cd ${remotePath} && ${command}`;
const finalCommand = `ssh ${url} "${sshCommand}"`;
const response = execSync(finalCommand).toString();

console.log("----------------------------------");
console.log(colors.green(response));
});
} catch (err) {
handleError(err);
}
};

/**
* Pulls code from a specific environment using rsync.
* Syncs files from a specific environment using rsync.
* @param {string} env - The name of the environment to pull from.
* @param {string} task - The name of the task associated with the pull operation.
*/
exports.rsyncPull = function (env, task) {
if (env) {
try {
const environmentConfig = getConfig(env);
const user = environmentConfig.user;
const url = `${user}@${environmentConfig.server}`;
const remotePath = environmentConfig.path;
function rsync(env, task, isPull) {
if (!env) {
console.log(colors.red("Missing environment"));
return;
}

let rsyncCommand = `rsync -rltDvhzP ${url}:${remotePath}${task} ${task}`;
try {
const environmentConfig = getConfig(env);
const { user, server, path: remotePath } = environmentConfig;
const url = `${user}@${server}`;

console.log("----------------------------------");
execSync(rsyncCommand, { stdio: "inherit" });
console.log("----------------------------------");
} catch (err) {
console.log(colors.bgRed(err));
}
const rsyncCommand = isPull
? `rsync -rltDvhzP ${url}:${remotePath}${task} ${task}`
: `rsync -rltDvhzP ${task} ${url}:${remotePath}${task}`;

console.log("----------------------------------");
execSync(rsyncCommand, { stdio: "inherit" });
console.log("----------------------------------");
} catch (err) {
handleError(err);
}
}

/**
* Pulls code from a specific environment using rsync.
* @param {string} env - The name of the environment to pull from.
* @param {string} task - The name of the task associated with the pull operation.
*/
exports.rsyncPull = function (env, task) {
rsync(env, task, true);
};

/**
Expand All @@ -117,31 +136,14 @@ exports.rsyncPull = function (env, task) {
* @param {string} task - The name of the task associated with the push operation.
*/
exports.rsyncPush = function (env, task) {
if (env) {
try {
const environmentConfig = getConfig(env);
const user = environmentConfig.user;
const url = `${user}@${environmentConfig.server}`;
const remotePath = environmentConfig.path;

let rsyncCommand = `rsync -rltDvhzP ${task} ${url}:${remotePath}${task}`;

console.log("----------------------------------");
execSync(rsyncCommand, { stdio: "inherit" });
console.log("----------------------------------");
} catch (err) {
console.log(colors.bgRed(err));
}
}
rsync(env, task, false);
};

/**
* Initializes the navnode-deployment module by creating a configuration file.
* This function writes a JavaScript file named 'navnode-deployment.js' with
* predefined environment and task configurations.
*
* @function
* @memberof module:navnode-deployment
* @throws {Error} If there is an issue writing the configuration file.
* @returns {void}
*/
Expand All @@ -167,5 +169,9 @@ exports.environments = environments;
exports.tasks = tasks;
`;

fs.writeFileSync("navnode-deployment.js", content);
try {
fs.writeFileSync("navnode-deployment.js", content);
} catch (err) {
handleError(err);
}
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1f61935

Please sign in to comment.