1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 | const echo = console.log;
|
7 |
|
8 | const os = require('os');
|
9 | const path = require('path');
|
10 | const readPkgUp = require('read-pkg-up');
|
11 | const chalk = require('chalk');
|
12 | const pad = require('pad');
|
13 |
|
14 |
|
15 |
|
16 | delete require.cache[__filename];
|
17 | const pkgPath = path.dirname(module.parent.filename);
|
18 | const { pkg } = readPkgUp.sync({ cwd:pkgPath });
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | const STATIC = global.___AbsolunetCli___ ? global.___AbsolunetCli___ : global.___AbsolunetCli___ = {
|
27 | commands: {},
|
28 | taskWidth: {},
|
29 | fullUsage: {},
|
30 | baseWidth: 0
|
31 | };
|
32 |
|
33 |
|
34 |
|
35 | const printCmd = (cmd, width = STATIC.baseWidth, spacer = 1) => {
|
36 | const [task, subtask] = cmd.split(' ');
|
37 | const [name, desc, delta = 0] = subtask ? STATIC.commands[task][subtask] : STATIC.commands[task];
|
38 |
|
39 | return `${pad(chalk.yellow(name), width + delta)}${' '.repeat(spacer)}${desc}`;
|
40 | };
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | module.exports = class Cli {
|
49 |
|
50 |
|
51 | static get PLACEHOLDER() {
|
52 | return 10;
|
53 | }
|
54 |
|
55 | static get OPTIONALPLACEHOLDER() {
|
56 | return 26;
|
57 | }
|
58 |
|
59 | static get placeholder() {
|
60 | return chalk.green;
|
61 | }
|
62 |
|
63 | static optionalPlaceholder(name) {
|
64 | return `${chalk.reset('[')}${this.placeholder(name)}${chalk.reset(']')}`;
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 | static setUsageTasks(commands, taskWidth) {
|
70 | STATIC.commands = commands;
|
71 | STATIC.taskWidth = taskWidth;
|
72 | }
|
73 |
|
74 |
|
75 |
|
76 | static setFullUsage(fullUsage, baseWidth) {
|
77 | STATIC.fullUsage = fullUsage;
|
78 | STATIC.baseWidth = baseWidth;
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 | static get fullUsage() {
|
84 | let usage = `Usage: ${chalk.yellow(pkg.name)} ${chalk.cyan('<command>')}\n`;
|
85 |
|
86 | Object.keys(STATIC.fullUsage).forEach((group) => {
|
87 | usage += `\n${chalk.underline(group)}\n`;
|
88 |
|
89 | STATIC.fullUsage[group].forEach((task) => {
|
90 | usage += `${printCmd(task)}\n`;
|
91 | });
|
92 | });
|
93 |
|
94 | usage += `\n${pkg.name}@${pkg.version} ${pkgPath}`;
|
95 |
|
96 | return usage;
|
97 | }
|
98 |
|
99 |
|
100 |
|
101 | static getTaskUsage(task) {
|
102 | const subs = !Array.isArray(STATIC.commands[task]);
|
103 |
|
104 | let usage = ` ${chalk.underline('Usage:')}\n`;
|
105 | if (subs) {
|
106 | Object.values(STATIC.commands[task]).forEach((item) => {
|
107 | usage += ` ${chalk.yellow(`${pkg.name} ${task}`)} ${printCmd(`${task} ${item[0]}`, STATIC.taskWidth[task])}\n`;
|
108 | });
|
109 | } else {
|
110 | usage += ` ${chalk.yellow(pkg.name)} ${printCmd(task, 0, 3)}\n`;
|
111 | }
|
112 |
|
113 | return usage;
|
114 | }
|
115 |
|
116 |
|
117 |
|
118 | static showTaskUsage(meowCli) {
|
119 | echo(`\n${this.getTaskUsage(meowCli.input[0])}`);
|
120 | process.exit(2);
|
121 | }
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | static get rawArguments() {
|
130 | const args = process.argv.slice(2);
|
131 | args.shift();
|
132 |
|
133 | return args.join(' ');
|
134 | }
|
135 |
|
136 |
|
137 | static refuseFlags(meowCli) {
|
138 | if (Object.keys(meowCli.flags).length) {
|
139 | this.showTaskUsage(meowCli);
|
140 | }
|
141 | }
|
142 |
|
143 |
|
144 | static refuseFlagsAndArguments(meowCli) {
|
145 | if (meowCli.input.length > 1 || Object.keys(meowCli.flags).length) {
|
146 | this.showTaskUsage(meowCli);
|
147 | }
|
148 | }
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | static isRoot() {
|
157 | const user = os.userInfo();
|
158 |
|
159 | return user.uid === 0 || user.gid === 0 || user.username === 'root';
|
160 | }
|
161 |
|
162 | };
|