-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve code quality for overal package
- Loading branch information
1 parent
4dbe2e7
commit 1f61935
Showing
3 changed files
with
141 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.