1 | import {Command} from "../interfaces/command";
|
2 | import {CommandLine, ConsoleEx} from "../interfaces/command-line";
|
3 | import {injectable, inject} from 'inversify';
|
4 | import {NestedYargs, NestedYargsCategory} from "../interfaces/nested-yargs-wrapper";
|
5 | declare let console: ConsoleEx;
|
6 |
|
7 |
|
8 | @injectable()
|
9 | export class CommandLineImpl implements CommandLine {
|
10 | private nestedYargsApp: NestedYargsCategory;
|
11 |
|
12 | constructor(@inject('NestedYargs') private nestedYargs: NestedYargs) {
|
13 | this.addEasyTableToConsole();
|
14 | }
|
15 |
|
16 | init(options: any = {}) {
|
17 | this.nestedYargsApp = this.nestedYargs.createApp(options);
|
18 | }
|
19 |
|
20 | addCommand(cmd: Command) {
|
21 | cmd.aliases.forEach(alias => {
|
22 | let category = this.nestedYargs.createCategory(alias, cmd.commandDesc);
|
23 | cmd.subCommands.forEach(subCommand => {
|
24 | subCommand.aliases.forEach(alias => {
|
25 | let catCmd = this.nestedYargs.createCommand(
|
26 | alias,
|
27 | subCommand.commandDesc,
|
28 | subCommand
|
29 | );
|
30 | category.command(catCmd);
|
31 | });
|
32 | });
|
33 | this.nestedYargsApp.command(category);
|
34 | });
|
35 | }
|
36 |
|
37 | printTable(rows: any[]) {
|
38 | console.table(rows);
|
39 | }
|
40 |
|
41 | exec(unitTestArgs: string[] = []) {
|
42 | this.nestedYargs.run(this.nestedYargsApp, unitTestArgs);
|
43 | }
|
44 |
|
45 | private addEasyTableToConsole() {
|
46 | if (typeof console === 'undefined') {
|
47 | throw new Error('Weird, console object is undefined');
|
48 | }
|
49 | if (typeof console.table === 'function') {
|
50 | return;
|
51 | }
|
52 | let Table = require('easy-table');
|
53 |
|
54 | function arrayToString(arr) {
|
55 | let t = new Table();
|
56 | arr.forEach(record => {
|
57 | if (typeof record === 'string' ||
|
58 | typeof record === 'number') {
|
59 | t.cell('item', record);
|
60 | } else {
|
61 |
|
62 | Object.keys(record).forEach(property => {
|
63 | t.cell(property, record[property]);
|
64 | });
|
65 | }
|
66 | t.newRow();
|
67 | });
|
68 | return t.toString();
|
69 | }
|
70 |
|
71 | function printTitleTable(title, arr) {
|
72 | let str = arrayToString(arr);
|
73 | let rowLength = str.indexOf('\n');
|
74 | if (rowLength > 0) {
|
75 | if (title.length > rowLength) {
|
76 | rowLength = title.length;
|
77 | }
|
78 | console.log(title);
|
79 | let sep = '-', k, line = '';
|
80 | for (k = 0; k < rowLength; k += 1) {
|
81 | line += sep;
|
82 | }
|
83 | console.log(line);
|
84 | }
|
85 | console.log(str);
|
86 | }
|
87 |
|
88 | function objectToArray(obj) {
|
89 | let keys = Object.keys(obj);
|
90 | return keys.map(key => {
|
91 | return {
|
92 | key: key,
|
93 | value: obj[key]
|
94 | };
|
95 | });
|
96 | }
|
97 |
|
98 | function objectToString(obj) {
|
99 | return arrayToString(objectToArray(obj));
|
100 | }
|
101 |
|
102 | console.table = function () {
|
103 | console.log('');
|
104 | let args = Array.prototype.slice.call(arguments);
|
105 | if (args.length === 2 &&
|
106 | typeof args[0] === 'string' &&
|
107 | Array.isArray(args[1])) {
|
108 | return printTitleTable(args[0], args[1]);
|
109 | }
|
110 | args.forEach(k => {
|
111 | if (typeof k === 'string') {
|
112 | return console.log(k);
|
113 | } else if (Array.isArray(k)) {
|
114 | console.log(arrayToString(k));
|
115 | } else if (typeof k === 'object') {
|
116 | console.log(objectToString(k));
|
117 | }
|
118 | });
|
119 | };
|
120 | }
|
121 | }
|
122 |
|