UNPKG

p7s-info

Version:
79 lines (70 loc) 1.82 kB
// Import. const fs = require('fs'); const path = require('path'); const CColor = require('ccolor'); // Constants. const HELP_FILE_PATH = path.join(__dirname, '..', 'data', 'help.txt'); const HELP_FILE_ENCODING = 'utf8'; const WRONG_PARAMS_FILE_PATH = path.join(__dirname, '..', 'data', 'wrong_params.txt'); const WRONG_PARAMS_FILE_ENCODING = 'utf8'; const PACKAGE_JSON_FILE_PATH = path.join(__dirname, '..', 'package.json'); const PACKAGE_JSON_FILE_ENCODING = 'utf8'; const VERSION_ANCHOR = '<version>'; const VERSION_TEMPLATE = `Version: ${VERSION_ANCHOR}.`; /** * Help. */ class Help { /** * Show. */ static show() { // Define and show help text. const helpText = getHelpText(); console.log(CColor.yellow(helpText)); } /** * Version. */ static version() { // Define and show version. const version = getVersion(); const versionText = VERSION_TEMPLATE.replace(VERSION_ANCHOR, version); console.log(CColor.yellow(versionText)); } /** * Wrong params. */ static wrongParams() { // Inform about wrong params. const wrongParamsText = getWrongParamsText(); console.log(CColor.red(wrongParamsText)); } } /** * Get help text. * @return {string} */ function getHelpText() { const helpText = fs.readFileSync(HELP_FILE_PATH, HELP_FILE_ENCODING); return helpText; } /** * Get version. * @return {string} */ function getVersion() { const packageJsonContent = fs.readFileSync(PACKAGE_JSON_FILE_PATH, PACKAGE_JSON_FILE_ENCODING); const version = JSON.parse(packageJsonContent).version; return version; } /** * Get wrong params text. * @return {string} */ function getWrongParamsText() { const helpText = fs.readFileSync(WRONG_PARAMS_FILE_PATH, WRONG_PARAMS_FILE_ENCODING); return helpText; } // Export. module.exports = Help;