UNPKG

3.37 kBJavaScriptView Raw
1//--------------------------------------------------------
2//-- CLI utilities
3//--------------------------------------------------------
4'use strict';
5
6const echo = console.log; // eslint-disable-line no-console
7
8const os = require('os');
9const path = require('path');
10const readPkgUp = require('read-pkg-up');
11const chalk = require('chalk');
12const pad = require('pad');
13// const widestLine = require('widest-line');
14// const stringWidth = require('string-width');
15
16delete require.cache[__filename];
17const pkgPath = path.dirname(module.parent.filename);
18const { pkg } = readPkgUp.sync({ cwd:pkgPath });
19
20
21
22
23
24
25//-- Static properties
26const STATIC = global.___AbsolunetCli___ ? global.___AbsolunetCli___ : global.___AbsolunetCli___ = {
27 commands: {},
28 taskWidth: {},
29 fullUsage: {},
30 baseWidth: 0
31};
32
33
34//-- Command usage
35const 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
48module.exports = class Cli {
49
50 //-- Usager helpers
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 //-- Set tasks
69 static setUsageTasks(commands, taskWidth) {
70 STATIC.commands = commands;
71 STATIC.taskWidth = taskWidth;
72 }
73
74
75 //-- Set full usage
76 static setFullUsage(fullUsage, baseWidth) {
77 STATIC.fullUsage = fullUsage;
78 STATIC.baseWidth = baseWidth;
79 }
80
81
82 //-- Get full usage
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 //-- Get task usage
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 //-- Show task usage and die
118 static showTaskUsage(meowCli) {
119 echo(`\n${this.getTaskUsage(meowCli.input[0])}`);
120 process.exit(2); // eslint-disable-line no-process-exit
121 }
122
123
124
125
126
127
128 //-- Raw arguments
129 static get rawArguments() {
130 const args = process.argv.slice(2);
131 args.shift();
132
133 return args.join(' ');
134 }
135
136 //-- Refuse flags
137 static refuseFlags(meowCli) {
138 if (Object.keys(meowCli.flags).length) {
139 this.showTaskUsage(meowCli);
140 }
141 }
142
143 //-- Refuse flags & arguments
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 //-- Is root
156 static isRoot() {
157 const user = os.userInfo();
158
159 return user.uid === 0 || user.gid === 0 || user.username === 'root';
160 }
161
162};