llearn
Version:
Bad-ass developers create awesome apps
162 lines (143 loc) • 6.04 kB
JavaScript
// console.log("Starting helper.js...");
// helper.js
// Purpose: The purpose of this....
// Date Created: 6/1/2018
// Created by : Perez Lamed van Niekerk
// ------------------------------------------------------
/*jshint esversion: 6 */
const _log = console.log;
const _JSONstr = (object) => JSON.stringify(object, undefined, 2);
const _logJSON = (object) => console.log(_JSONstr(object));
const _logLine = () => _log('----------------------------------------------------------');
// ------------------------------------------------------
const _clipboard = require('copy-paste'); // npm i copy-paste -g
const _command = require('commander'); // npm i commander -s
const _package = require('../package.json');
const _$ = require('larray'); // npm i lamed_array -s
const _ = require('lodash');
const _setupDir = __dirname + '\\templates\\';
/* Get the application version */
function Version () {
return _package.version;
}
/* Parse the parameters */
_command.version(Version())
.option('-a, --about', "Show about 'New app development'")
.option('-e, --edit', "Edit the last selected template'")
.option('-f, --folder', "Show the folder of the last selected template'")
// .option('-c, --create "[file]"', "Create the file and open in editor")
.option('-t, --templates ', "Show history of templates used.")
.option('-?, --howto ', "Show how to create templates.")
.option('-s, --setup', "Setup the environment")
.parse(process.argv);
function help_parameters() {
_log(_command.help());
}
//_log(_command);
function help_documentation() {
_logLine();
_log('Simple search:');
_log('>ll key1 (Searches for key1)\n');
_log('Advanced search:');
_log('>ll key1, key2, key3, /key4, /key5 (Searches for key1..3 and not for key4,5)\n\\n')
_log('TEMPLATE HELP:');
_log('---------------');
_log('- Add template files with extentions (.bat .txt .sql .js .java .npm)');
_log('- If template ends with .bat it will be excuted');
_log(`- Save template files in '${_setupDir}'`);
_log('- Up to two parameters can be added to a template.');
_log('- To describe a folder add file and end it with (.readme) extension.');
_log('- Start line comments with "/?"');
_log('\nParameters:');
_log('- $Parameter_name$ (start and end with "$" and use "_" for spaces)');
_log('- $DATE$ (Replace with current date)');
_log('- $USER$ (Replace with current user)');
_log('- $Parm&&$ (&& - if user enter "&&" then use clipboard as input)');
_log('\nContents:');
_log('- To open a folder, add "explorer.exe " and the folder to the .bat template');
_log('- To open a web site; just add the url starting with http.');
_log('\nGlobal:');
_log('- Some templates need to be global accessible (like git commands). Put in _global folder.');
_log();
_logLine();
}
/* Check the parameters */
function checkParm() {
return new Promise((resolve, reject) => {
// _log({_command});
// ll cobus -/old -/oli
// Need to filter for arguments containing -/
// @formatter:off
if (_$.Ok(_command.about)) resolve({topic: 'about', keyword: ['startnewapp'], description: "'How to start a new app'"});
else if (_$.Ok(_command.setup)) resolve({topic: 'setup', keyword: [_command.setup], description: _command.description("setup")});
else if (_$.Ok(_command.templates)) resolve({topic: 'templates', keyword: [''], description: "Searching history templates"});
else if (_$.Ok(_command.edit)) resolve({topic: 'edit', keyword: [''], description: "Edit template"});
else if (_$.Ok(_command.folder)) resolve({topic: 'folder', keyword: [''], description: "Folder of template"});
else if (_$.Ok(_command.howto)) {
help_documentation();
help_parameters();
} else {
let keys = _command.args.toLowerCase();
/* Filter for parameters that does not include '/' */
keys1 = _.filter(keys, (item) => {
if (item.includes('/') === false) return item;
});
/* Filter for '/*' parameters */
let keys2 = _.filter(keys, (item) => {
if (item.includes('/') === true) return item;
}).replaceAll('/', '');
// _log({keys});
resolve({topic: 'all', keyword: keys1, remove: keys2, description: "Searching in all templates"});
}
// @formatter:on
});
}
var Clipboard = () => {
// CopyTo() --------------------
var CopyTo = (text) => {
return new Promise((resolve, reject) => {
_clipboard.copy(text, () => {
resolve();
});
})
};
// PasteFrom() --------------------
var PasteFrom = () => {
return new Promise((resolve, reject) => {
_clipboard.paste((err, text) => {
resolve(text);
});
})
};
return {CopyTo, PasteFrom}
};
/* Add new date 2 str method */
Date.prototype.toStr = function (seperator = '/') {
var mm = this.getMonth() + 1; // getMonth() is zero-based
var dd = this.getDate();
return [this.getFullYear(),
(mm > 9 ? '' : '0') + mm,
(dd > 9 ? '' : '0') + dd
].join(seperator);
};
/* Get comment line starting with /? */
function Comment(text) {
let prt1 = text.split('/?');
if (prt1.length > 1) {
prt1 = prt1[1]; // comment with \n
let comment = prt1.split('\n');
// _log({comment});
return comment[0];
}
}
// Exports --------------------------
module.exports = {
IO: require('./helper_io.js').IO,
parametersGet: require('./helper_parameter.js').parametersGet,
Clipboard,
Version,
help_documentation,
help_parameters,
checkParm,
Comment
};