lamed_learn
Version:
Learning through code templates
131 lines (117 loc) • 5.18 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 _test = require('lamed_test')
const { Ok, notOk, notOk_Then, Equal, notEqual, con, testAND } = _test // eslint-disable-line
// con.traceSet(0)
// 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('lodash')
const _filter = require('lodash.filter')
const path = require('path')
const _setupDir = path.join(__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 () { // eslint-disable-line
con.log(_command.help())
}
// con.log(_command);
function help_documentation () { // eslint-disable-line
con.logLine()
con.logGreen('Simple search:')
con.logGreen('---------------')
con.log('>ll key1 (Searches for key1)\n')
con.log('Advanced search:')
con.log('>ll key1, key2, key3, /key4, /key5 (Searches for key1..3 and not for key4,5)\n\\n')
con.logGreen('TEMPLATE HELP:')
con.logGreen('---------------')
con.log('- Add template files with extentions (.bat .txt .sql .js .java .npm)')
con.log('- If template ends with .bat it will be excuted')
con.log(`- Save template files in '${_setupDir}'`)
con.log('- Up to two parameters can be added to a template.')
con.log('- To describe a folder add file and end it with (.readme) extension.')
con.log('- Start line comments with "/?"')
con.logGreen('\nParameters:')
con.logGreen('---------------')
con.log('- $Parameter_name$ (start and end with "$" and use "_" for spaces)')
con.log('- $DATE$ (Replace with current date)')
con.log('- $USER$ (Replace with current user)')
con.log('- $Parm&&$ (&& - if user enter "&&" then use clipboard as input)')
con.logGreen('\nContents:')
con.log('- To open a folder, add "explorer.exe " and the folder to the .bat template')
con.log('- To open a web site; just add the url starting with http.')
con.logGreen('\nGlobal:')
con.log('- Some templates need to be global accessible (like git commands). Put in _global folder.')
con.log()
con.logLine()
}
/* Check the parameters */
function checkParm () {
return new Promise((resolve, reject) => {
// con.log({_command})
// ll cobus -/old -/oli
// Need to filter for arguments containing -/
// @formatter:off
if (_test.Ok(_command.about)) resolve({ topic: 'about', keyword: ['startnewapp'], description: "'How to start a new app'" })
else if (_test.Ok(_command.setup)) resolve({ topic: 'setup', keyword: [_command.setup], description: _command.description('setup') })
else if (_test.Ok(_command.create)) resolve({ topic: 'create', keyword: [_command.create], description: _command.description('create') })
else if (_test.Ok(_command.templates)) resolve({ topic: 'templates', keyword: [''], description: 'Searching history templates' })
else if (_test.Ok(_command.edit)) resolve({ topic: 'edit', keyword: [''], description: 'Edit template' })
else if (_test.Ok(_command.folder)) resolve({ topic: 'folder', keyword: [''], description: 'Folder of template' })
else if (_test.Ok(_command.howto)) {
help_documentation()
help_parameters()
} else {
const keys = _command.args.toLowerCase()
/* Filter for parameters that does not include '/' */
const keys1 = _filter(keys, (item) => {
if (item.includes('/') === false) return item
})
/* Filter for '/*' parameters */
const keys2 = _filter(keys, (item) => {
if (item.includes('/') === true) return item
}).replaceAll('/', '')
// con.log({keys});
resolve({ topic: 'all', keyword: keys1, remove: keys2, description: 'Searching in all templates' })
}
// @formatter:on
})
}
/* Get comment line starting with /? */
function Comment (text) {
let prt1 = text.split('/?')
if (prt1.length > 1) {
prt1 = prt1[1] // comment with \n
const comment = prt1.split('\n')
// con.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
}