import * as yargs from 'yargs'; import * as fs from 'fs-extra'; import { merge } from 'lodash'; import * as chalk from 'chalk'; import { dirname, join, basename, extname } from 'path'; import { inspect, getCallerFile } from '@radic/util'; import { paths } from './paths'; export interface ICli { start(); } export class Cli implements ICli { argv: yargs.Argv; protected static _instance: Cli; protected started: boolean = false; public pkg: any; protected constructor() { this.pkg = require(join(__dirname, '..', '..', 'package.json')); fs.ensureDirSync(paths.userData); } static get instance(): Cli { if ( !this._instance ) { this._instance = new Cli; } return this._instance; } start() { if ( this.started === true ) return; this.started = true; var year = (new Date(Date.now())).getFullYear(); let _global = ( opts: yargs.Options ): yargs.Options => merge({ global : true, type : 'boolean' }, opts); var argv = yargs .merge({ cli : this }) .strict() // Format usage output to wrap at columns many columns. Use .wrap(null) to specify no column limit. Use .wrap(yargs.terminalWidth()) to maximize the width of yargs' usage instructions. .wrap(yargs.terminalWidth() - (yargs.terminalWidth() / 100 * 10)) .epilog(`Copyright ${year} Robin Radic`) .usage(`------------------ | ${chalk.green.bold('Radic Commando')} | ------------------ ${chalk.blue.bold('Usage:')} r [-V|--version] [-D|--debug] [-N|--no-interaction] [-h|--help] []`) .showHelpOnFail(false) .options({ N : _global({ alias : 'no-interaction', describe : 'Disables interactions' }), v : _global({ alias : 'verbose', describe : 'Increase verbosity by 1 (WARN, INFO, DEBUG)', count : true, }), D : _global({ alias : 'dump', describe : 'Dump parsed argv' }), h : _global({ alias : 'help' }), V : _global({ alias : 'version' }) }) .version('V', 'Show version', this.pkg.version) .help('h') .updateStrings({ 'Options:' : 'Global options:' }) //chalk.bold('Global options') }) // Register commands .commandDir('./../commands', { include: /[a-z]*.js$/, exclude: /_/ }) .completion('completion') .fail(function ( msg, err ) { if ( err ) throw err // preserve stack console.error(msg) process.exit(1) }) .argv; let a = 'a'; if ( argv._.length === 0 ) { yargs.showHelp(); } if ( argv.debug ) { inspect(argv) } // require all command files so the decorator can fill the commands array //globule.find(path.join(paths.src, 'commands', '*.js')).forEach((filePath) => require('./../commands/' + path.basename(filePath, '.js'))); } static commandDirBuilder( _yargs: yargs.Argv, name?: string, opts:any = {}) { let file = getCallerFile(); name = name || basename(file, extname(file)); var argv = _yargs .usage(`Usage: r ${name} `) .commandDir(dirname(file), merge({ include: RegExp(name + '_.*') }, opts)) .fail(( msg, err ) => { var _argv = _yargs.parsed ? _yargs.parsed['argv'] : {}; if ( _argv && _argv._ && _argv._.length === 0 ) { _yargs.showHelp(); process.exit(0); } if ( err ) throw err; console.log(msg); process.exit(1); }).argv; } static handleGlobals( argv: any ): boolean { if ( argv.dump ) { inspect(argv); } // not needed, staying here as example if ( argv.help || argv.version ) { return true; } return false; } }