Instantly share code, notes, and snippets.

Embed
What would you like to do?
Javascript modulated setup for node ES6
/***************************************************************************************************************************************************************
*
* Application framework and settings
*
* [Description of application]
*
* @license [url] [description]
* @author [author] [@email]
* @repository [url]
*
**************************************************************************************************************************************************************/
'use strict';
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Dependencies
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
const CFonts = require(`cfonts`);
const Chalk = require(`chalk`);
const Fs = require(`fs`);
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Constructor
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
const App = (() => { //constructor factory
return {
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// settings
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
DEBUG: true, //Debug settings
DEBUGLEVEL: 2, //Debug level setting
SETTING1: `string`, //Setting as a string
SETTING2: false, //Setting as boolean
SETTING3: 1, //Setting as a initeger
SETTING4: {}, //Setting as an object
SETTING5: [], //Setting as an array
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Debugging prettiness
//
// debugging, Print debug message that will be logged to console.
//
// @method headline Return a headline preferably at the beginning of your app
// @param [text] {string} The sting you want to log
// @param [level] {integer} (optional) The debug level. Show equal and greater levels. Default: 99
// @return [ansi] {output}
//
// @method report Return a message to report starting a process
// @param [text] {string} The sting you want to log
// @param [level] {integer} (optional) The debug level. Show equal and greater levels. Default: 99
// @return [ansi] {output}
//
// @method error Return a message to report an error
// @param [text] {string} The sting you want to log
// @param [level] {integer} (optional) The debug level. Show equal and greater levels. Default: 99
// @return [ansi] {output}
//
// @method interaction Return a message to report an interaction
// @param [text] {string} The sting you want to log
// @param [level] {integer} (optional) The debug level. Show equal and greater levels. Default: 99
// @return [ansi] {output}
//
// @method send Return a message to report data has been sent
// @param [text] {string} The sting you want to log
// @param [level] {integer} (optional) The debug level. Show equal and greater levels. Default: 99
// @return [ansi] {output}
//
// @method received Return a message to report data has been received
// @param [text] {string} The sting you want to log
// @param [level] {integer} (optional) The debug level. Show equal and greater levels. Default: 99
// @return [ansi] {output}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
debugging: {
headline: ( text ) => {
if( App.DEBUG ) {
CFonts.say(text, {
'font': 'chrome',
'align': 'center',
'colors': ['cyan', 'gray'],
'maxLength': 12,
});
}
},
report: ( text, level = 99 ) => {
if( App.DEBUG && level >= App.DEBUGLEVEL ) {
console.log(Chalk.bgWhite(`\n${Chalk.bold.green(' \u2611 ')} ${Chalk.black(`${text} `)}`));
}
},
error: ( text, level = 99 ) => {
if( App.DEBUG && level >= App.DEBUGLEVEL ) {
console.log(Chalk.bgWhite(`\n${Chalk.red(' \u2612 ')} ${Chalk.black(`${text} `)}`));
}
},
interaction: ( text, level = 99 ) => {
if( App.DEBUG && level >= App.DEBUGLEVEL ) {
console.log(Chalk.bgWhite(`\n${Chalk.blue(' \u261C ')} ${Chalk.black(`${text} `)}`));
}
},
send: ( text, level = 99 ) => {
if( App.DEBUG && level >= App.DEBUGLEVEL ) {
console.log(Chalk.bgWhite(`\n${Chalk.bold.cyan(' \u219D ')} ${Chalk.black(`${text} `)}`));
}
},
received: ( text, level = 99 ) => {
if( App.DEBUG && level >= App.DEBUGLEVEL ) {
console.log(Chalk.bgWhite(`\n${Chalk.bold.cyan(' \u219C ')} ${Chalk.black(`${text} `)}`));
}
}
},
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Log to console.log
//
// Log to console and in extension save in log file regardless of debug mode
//
// @method info Log info to console.log and in extension to node log file
// @param [text] {string} The sting you want to log
// @return [ansi] output
//
// @method error Log error to console.log and in extension to node log file
// @param [text] {string} The sting you want to log
// @return [ansi] output
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
log: {
info: ( text ) => {
console.log(`${Chalk.bold.gray(`Info `)} ${new Date().toString()}: ${text}`);
},
error: ( text ) => {
console.log(`${Chalk.bold.red(`ERROR`)} ${new Date().toString()}: ${text}`);
},
}
}
})();
/***************************************************************************************************************************************************************
*
* Module 1
*
* [Description of module]
*
**************************************************************************************************************************************************************/
App.module1 = (() => {
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Private function
// converting, [add string to text]
//
// @param [input] {string} The sting you want to convert
//
// @return [text] + suffix
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
const converting = ( text ) => {
App.debugging.report(`Running converting with "${text}"`, 1);
return `${text} converted!`;
}
return {
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Public function
// uppercase, [convert lower to upper case]
//
// @param [input] {string} The sting you want to convert
//
// @return Uppercase [text]
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
uppercase: ( input ) => {
App.debugging.report(`Running uppercase with "${input}"`, 1);
let variable = converting( input ); //calling private method
variable = variable.toUpperCase();
App.SETTING1 = `UPDATED GLOBAL SETTINGS`; //changing global settings
//some code
return variable;
},
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Public function
// [convert upper to lower case and add SETTING1]
//
// @param [input] {string} The sting you want to convert
//
// @return Lowercase [text]
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
lowercase: ( input ) => {
App.debugging.report(`Running lowercase with "${input}"`, 1);
let variable = `${input.toLowerCase()} - ${App.SETTING1}`; //accessing global settings
//some code
return variable;
},
}
})();
/***************************************************************************************************************************************************************
*
* Application initialization
*
* [We start the server and do all the listening stuff]
*
**************************************************************************************************************************************************************/
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
// Initiate application
//
// [This will start the server and listen to port 1337]
//--------------------------------------------------------------------------------------------------------------------------------------------------------------
App.init = () => {
App.debugging.headline(`DEBUG|INFO`, 2);
const text1 = App.module1.uppercase(`this text was lowercase`);
const text2 = App.module1.lowercase(`THIS TEXT WAS UPPERCASE`);
App.log.info(`${text1} - ${text2}`);
};
App.init();
{
"name": "[FILLME]",
"description": "[FILLME]",
"version": "0.1.0",
"homepage": "https://github.com/[FILLME]",
"author": {
"name": "[FILLME]",
"email": "[FILLME]",
"url": "[FILLME]"
},
"contributors": {
"name": "[FILLME]",
"email": "[FILLME]",
"url": "[FILLME]"
},
"repository": {
"type": "git",
"url": "git://github.com/[FILLME].git"
},
"bugs": {
"url": "https://github.com/[FILLME]/issues"
},
"licenses": [
{
"type": "[FILLME]",
"url": "https://github.com/[FILLME]/blob/master/LICENSE"
}
],
"engines": {
"node": ">=6.0.0"
},
"devDependencies": {},
"peerDependencies": {},
"dependencies": {
"chalk": "^1.0.0",
"cfonts": "^1.0.1"
},
"keywords": [
"[FILLME]"
],
"main": "index.js",
"bin": {
"[FILLME]": "./bin/[FILLME].js"
},
"license": "[FILLME]"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment