UNPKG

16 kBJavaScriptView Raw
1#!/usr/bin/env node
2'use strict';
3
4function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
5
6var parser = _interopDefault(require('yargs-parser'));
7var Menehra = require('menhera');
8var Menehra__default = _interopDefault(Menehra);
9var chalk = _interopDefault(require('chalk'));
10var dowloadRepo = _interopDefault(require('download-git-repo'));
11var fs = _interopDefault(require('fs-extra'));
12var cliSpinner = _interopDefault(require('cli-spinners'));
13var ora = _interopDefault(require('ora'));
14var path = _interopDefault(require('path'));
15var Message = _interopDefault(require('menhera-message'));
16
17function _defineProperty(obj, key, value) {
18 if (key in obj) {
19 Object.defineProperty(obj, key, {
20 value: value,
21 enumerable: true,
22 configurable: true,
23 writable: true
24 });
25 } else {
26 obj[key] = value;
27 }
28
29 return obj;
30}
31
32function _objectWithoutProperties(source, excluded) {
33 if (source == null) return {};
34 var target = {};
35 var sourceKeys = Object.keys(source);
36 var key, i;
37
38 for (i = 0; i < sourceKeys.length; i++) {
39 key = sourceKeys[i];
40 if (excluded.indexOf(key) >= 0) continue;
41 target[key] = source[key];
42 }
43
44 if (Object.getOwnPropertySymbols) {
45 var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
46
47 for (i = 0; i < sourceSymbolKeys.length; i++) {
48 key = sourceSymbolKeys[i];
49 if (excluded.indexOf(key) >= 0) continue;
50 if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
51 target[key] = source[key];
52 }
53 }
54
55 return target;
56}
57
58function _toArray(arr) {
59 return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest();
60}
61
62function _arrayWithHoles(arr) {
63 if (Array.isArray(arr)) return arr;
64}
65
66function _iterableToArray(iter) {
67 if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
68}
69
70function _nonIterableRest() {
71 throw new TypeError("Invalid attempt to destructure non-iterable instance");
72}
73
74var space = function space(num) {
75 return Array.from(new Array(num)).join(" ");
76};
77var fillSpace = function fillSpace(size, current) {
78 return space(size - current);
79};
80var genOutput = function genOutput(_ref) {
81 var input = _ref.input,
82 chalkFn = _ref.chalkFn,
83 length = _ref.length,
84 _ref$left = _ref.left,
85 left = _ref$left === void 0 ? 0 : _ref$left,
86 _ref$right = _ref.right,
87 right = _ref$right === void 0 ? 0 : _ref$right;
88 var inputLenght = input.length;
89 var output = chalkFn(input);
90 (!length || inputLenght > length) && (length = inputLenght);
91 return "".concat(space(left)).concat(output || []).concat(fillSpace(length, inputLenght)).concat(space(right));
92};
93var ENV = function ENV(_ref2) {
94 var _args = _ref2._args,
95 args = _ref2.args,
96 options = _ref2.options;
97 var env = {};
98 env.NONE_ARGS = args.length === 0;
99 env.NONE_OPTIONS = Object.keys(options).length === 0;
100 env.NONE_INPUTS = env.NONE_ARGS && env.NONE_OPTIONS;
101 env.NONE_FULL_ARGS = _args.length !== args.length;
102 return env;
103};
104
105var commands = {
106 $: function $(_ref) {
107 var _this = this;
108
109 var _ = _ref._,
110 _key = _ref._key,
111 _val = _ref._val,
112 cp = _ref.cp;
113 var commandAlias = this.alias.commands,
114 rootAlias = this.config.rootAlias;
115 var name = _val.name,
116 _val$args = _val.args,
117 args = _val$args === void 0 ? [] : _val$args,
118 alias = _val.alias,
119 _val$examples = _val.examples,
120 examples = _val$examples === void 0 ? {} : _val$examples,
121 _val$desc = _val.desc,
122 cDesc = _val$desc === void 0 ? "" : _val$desc,
123 _val$options = _val.options,
124 options = _val$options === void 0 ? {} : _val$options;
125 var key = name || _key;
126 this.commands[key] = _val;
127
128 if (alias) {
129 this.alias.commands[key] = alias;
130 this.alias.commands[alias] = key;
131 }
132
133 examples && Menehra.$set(this.examples, examples);
134 var commandOutput = [];
135 args = args.map(function (argv) {
136 return "[".concat(argv, "]");
137 });
138
139 if (key !== rootAlias) {
140 commandOutput.push("".concat(genOutput({
141 input: "".concat(key),
142 chalkFn: chalk.magenta,
143 length: 12,
144 left: 2
145 })).concat(genOutput({
146 input: "".concat(alias || ""),
147 chalkFn: chalk.magenta,
148 length: 6
149 })).concat(genOutput({
150 input: "".concat(args.join(" ")),
151 chalkFn: chalk.grey,
152 length: 50
153 })).concat(genOutput({
154 input: "".concat(cDesc),
155 chalkFn: chalk.grey,
156 length: 20
157 }), "\n"));
158 }
159
160 var examplesOutput = [];
161
162 Menehra.$(examples, function (key, val) {
163 examplesOutput.push("".concat(genOutput({
164 input: "".concat(key),
165 chalkFn: chalk.magenta,
166 length: 20,
167 left: 2
168 })).concat(genOutput({
169 input: "".concat(val),
170 chalkFn: chalk.grey,
171 length: 50
172 }), "\n"));
173 });
174
175 var optionOutput = [];
176
177 Menehra.$(options, function (key, val) {
178 var alias = val.alias,
179 oDesc = val.desc;
180 _this.alias.options[key] = alias;
181 _this.alias.options[alias] = key;
182 optionOutput.push("".concat(genOutput({
183 input: "-".concat(alias),
184 chalkFn: chalk.grey,
185 length: 6,
186 left: 12
187 })).concat(genOutput({
188 input: "--".concat(key),
189 chalkFn: chalk.grey,
190 length: 50
191 })).concat(genOutput({
192 input: oDesc,
193 chalkFn: chalk.grey,
194 length: 10
195 }), "\n"));
196 });
197
198 Menehra.$set(this.helper, _defineProperty({}, key, {
199 examplesOutput: examplesOutput,
200 optionOutput: optionOutput,
201 commandOutput: commandOutput
202 }));
203 }
204};
205var config = {
206 _: function _(_ref2) {
207 var _2 = _ref2._,
208 _val = _ref2._val;
209 Menehra.$set(this.config, _val);
210 },
211 start: function start(_ref3) {
212 var _this2 = this;
213
214 var _ = _ref3._,
215 _val = _ref3._val;
216 var _this$alias = this.alias,
217 optionAlias = _this$alias.options,
218 commandAlias = _this$alias.commands,
219 _this$config = this.config,
220 name = _this$config.name,
221 target = _this$config.target,
222 rootAlias = _this$config.rootAlias,
223 version = _this$config.version;
224
225 var _parser = parser(target || process.argv.slice(2)),
226 __ = _parser._,
227 options = _objectWithoutProperties(_parser, ["_"]);
228
229 var _3 = _toArray(__),
230 _$2 = _3[0],
231 _key = _$2 === void 0 ? rootAlias : _$2,
232 args = _3.slice(1);
233
234 var command = this.commands[_key] || this.commands[commandAlias[_key]] || {};
235
236 var _command$args = command.args,
237 _args = _command$args === void 0 ? [] : _command$args,
238 _command$exec = command.exec,
239 exec = _command$exec === void 0 ? function () {} : _command$exec,
240 _command$options = command.options,
241 _options = _command$options === void 0 ? {} : _command$options;
242
243 Menehra.$(_options, function (key, val) {
244 var _default = _options[key].default;
245 _this2.args[key] = _default;
246 var alias = optionAlias[key];
247 alias && (_this2.args[alias] = _default);
248 });
249
250 Menehra.$(options, function (key, val) {
251 _this2.args[key] = val;
252 var alias = optionAlias[key];
253 alias && (_this2.args[alias] = val);
254 });
255
256 Menehra.$(_args, function (i, argv) {
257 _this2.args[argv] = args[i];
258 });
259
260 Menehra.$(args, function (i) {
261 _this2.args["$".concat(i)] = args[i];
262 });
263
264 var val = Object.assign({
265 _: _,
266 CLI: this
267 }, this.args, {
268 _key: _key,
269 options: options,
270 args: args,
271 _args: _args
272 });
273 val.env = ENV(val);
274 var help = val.help,
275 v = val.v;
276
277 if (help) {
278 _.$use(_defineProperty({}, "".concat(name, ".help"), _key));
279
280 return;
281 }
282
283 if (v) {
284 console.log(version);
285 return;
286 }
287
288 exec(val);
289 }
290};
291function help(_ref4) {
292 var _val = _ref4._val;
293 var help;
294 var commandAlias = this.alias.commands,
295 rootAlias = this.config.rootAlias;
296
297 if (_val !== rootAlias) {
298 help = this.helper[_val] || this.helper[commandAlias[_val]];
299 } else {
300 help = {
301 commandOutput: Object.values(this.helper).map(function (help) {
302 return help.commandOutput;
303 }),
304 optionOutput: this.helper[rootAlias].optionOutput
305 };
306 }
307
308 if (!help) return;
309 console.log("\n\n".concat(chalk.grey("Commands:"), "\n\n").concat(help.commandOutput.join(""), "\n\n").concat(chalk.grey("Options:"), "\n\n").concat(help.optionOutput.join(""), "\n "));
310}
311function usage(_ref5) {
312 var _val = _ref5._val;
313 var commandAlias = this.alias.commands;
314 var help = this.helper[_val] || this.helper[commandAlias[_val]];
315 help && console.log(" \n \n".concat(chalk.grey("Usage:"), "\n \n").concat(help.commandOutput.join(""), "\n "));
316}
317function examples(_ref6) {
318 var _val = _ref6._val;
319 var rootAlias = this.config.rootAlias;
320 help = {
321 examplesOutput: Object.values(this.helper).map(function (help) {
322 return help.examplesOutput.join("");
323 })
324 };
325 console.log("\n\n".concat(chalk.grey("Examples:"), "\n \n").concat(help.examplesOutput.join(""), "\n "));
326}
327
328var CLI = /*#__PURE__*/Object.freeze({
329 commands: commands,
330 config: config,
331 get help () { return help; },
332 usage: usage,
333 examples: examples
334});
335
336var examples$1 = {
337 desc: "Examples",
338 alias: "e",
339 args: ["example"],
340 exec: function exec(data) {
341 return new Promise(function ($return, $error) {
342 var _ = data._,
343 env = data.env,
344 example = data.example,
345 CLI = data.CLI;
346 var _CLI$config = CLI.config,
347 name = _CLI$config.name,
348 rootAlias = _CLI$config.rootAlias,
349 examples = CLI.examples;
350
351 if (env.NONE_INPUTS) {
352 _.$use(_defineProperty({}, "".concat(name, ".examples"), rootAlias));
353
354 return $return();
355 }
356
357 var val = examples[example];
358 var target = val.split(" ");
359
360 _.$use({
361 CLI: {
362 config: {
363 target: target,
364 start: true
365 }
366 }
367 });
368
369 return $return();
370 });
371 }
372};
373
374var commands$1 = /*#__PURE__*/Object.freeze({
375 examples: examples$1
376});
377
378var CLI$1 = (function (_ref) {
379 var name = _ref.name,
380 version = _ref.version;
381 return _defineProperty({
382 name: name,
383 alias: {
384 options: {
385 help: "h",
386 h: "help",
387 version: "v",
388 v: "version"
389 },
390 commands: {
391 examples: "e",
392 e: "examples"
393 }
394 },
395 args: {},
396 commands: {},
397 examples: {},
398 helper: {},
399 _hooks: _defineProperty({}, name, CLI),
400 config: {
401 name: name,
402 version: version,
403 rootAlias: "_"
404 }
405 }, name, {
406 commands: commands$1
407 });
408});
409
410var download = function download(repo, desc, options) {
411 return new Promise(function (res, rej) {
412 dowloadRepo(repo, desc, options, function (err) {
413 err ? rej(err) : res(err);
414 });
415 });
416};
417var forceDownload = function forceDownload(repo, desc, options) {
418 return new Promise(function ($return, $error) {
419 var exists;
420 return Promise.resolve(fs.exists(desc)).then(function ($await_2) {
421 try {
422 exists = $await_2;
423
424 if (exists) {
425 return Promise.resolve(fs.remove(desc)).then(function ($await_3) {
426 try {
427 return $If_1.call(this);
428 } catch ($boundEx) {
429 return $error($boundEx);
430 }
431 }.bind(this), $error);
432 }
433
434 function $If_1() {
435 return Promise.resolve(download(repo, desc, options)).then($return, $error);
436 }
437
438 return $If_1.call(this);
439 } catch ($boundEx) {
440 return $error($boundEx);
441 }
442 }.bind(this), $error);
443 });
444};
445var RandomKeyFromObject = function RandomKeyFromObject(object) {
446 return object[RandomKeyFromArray(Object.keys(object))];
447};
448var RandomKeyFromArray = function RandomKeyFromArray(array) {
449 return array[Math.floor(Math.random() * array.length)];
450};
451
452var download$1 = {
453 fail: function fail() {
454 return console.log("\n\nsorry, download failed. \n ");
455 },
456 success: function success(_ref) {
457 var _ref$_val = _ref._val,
458 projectName = _ref$_val.projectName,
459 templateName = _ref$_val.templateName;
460 return console.log(chalk.green("\nSuccess! Created <".concat(projectName, "> with template <").concat(templateName, ">\n\nWe suggest that you begin by typing:\n\n $ cd ").concat(projectName, "\n $ yarn install\n\n Happy hacking!\n")));
461 }
462};
463var colors = ["black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "gray"];
464var _spinner = null;
465var spinner = function spinner(_ref2) {
466 var _key = _ref2._key,
467 _val = _ref2._val;
468 var mode = _val.mode,
469 type = _val.type,
470 text = _val.text,
471 color = _val.color,
472 status = _val.status;
473
474 if (status == "stop") {
475 if (_spinner) {
476 _spinner.stop();
477 }
478 }
479
480 if (status == "start") {
481 var args = {};
482
483 if (mode === "random") {
484 var randomSpinner = RandomKeyFromObject(cliSpinner);
485
486 var _color = RandomKeyFromArray(colors);
487
488 args.spinner = randomSpinner;
489 args.color = _color;
490 }
491
492 color && (args.color = color);
493 text && (args.text = text);
494 cliSpinner[type] && (args.spinner = cliSpinner[type]);
495 _spinner = new ora(args);
496
497 _spinner.start();
498 }
499};
500
501var messages = /*#__PURE__*/Object.freeze({
502 download: download$1,
503 spinner: spinner
504});
505
506var _ = {
507 exec: function exec(_ref) {
508 var _ = _ref._,
509 _key = _ref._key,
510 env = _ref.env;
511
512 if (env.NONE_INPUTS) {
513 _.$use({
514 Mhr: {
515 help: _key
516 }
517 });
518 }
519 }
520};
521var init = {
522 desc: "Init Project",
523 alias: "i",
524 args: ["templateName", "projectName"],
525 examples: {
526 "init:cli": "init uu-z/template-cli mycli",
527 "init:module": "init uu-z/template-module mymodule"
528 },
529 options: {
530 clone: {
531 alias: "c",
532 desc: "clone repo",
533 default: false
534 }
535 },
536 exec: function exec(data) {
537 return new Promise(function ($return, $error) {
538 var _, _key, templateName, projectName, clone, h, env, desc, download$$1;
539
540 _ = data._, _key = data._key, templateName = data.templateName, projectName = data.projectName, clone = data.clone, h = data.h, env = data.env;
541
542 if (env.NONE_INPUTS) {
543 _.$use({
544 Mhr: {
545 help: _key
546 }
547 });
548
549 return $return();
550 }
551
552 if (env.NONE_FULL_ARGS) {
553 _.$use({
554 Mhr: {
555 usage: _key
556 }
557 });
558
559 return $return();
560 }
561
562 desc = path.join(process.cwd(), projectName);
563
564 _.$use({
565 Message: {
566 spinner: {
567 mode: "random",
568 text: "downloading...",
569 status: "start"
570 }
571 }
572 });
573
574 download$$1 = {};
575 return Promise.resolve(forceDownload(templateName, desc, {
576 clone: clone
577 })).then(function ($await_1) {
578 try {
579 $await_1 ? download$$1.fail = true : download$$1.success = {
580 templateName: templateName,
581 projectName: projectName
582 };
583
584 _.$use({
585 Message: {
586 download: download$$1,
587 spinner: {
588 status: "stop"
589 }
590 }
591 });
592
593 return $return();
594 } catch ($boundEx) {
595 return $error($boundEx);
596 }
597 }, $error);
598 });
599 }
600};
601
602var commands$2 = /*#__PURE__*/Object.freeze({
603 _: _,
604 init: init
605});
606
607var Mhr = CLI$1({
608 name: "Mhr",
609 version: "0.0.1"
610});
611var cli = Menehra__default.$use({
612 _mount: {
613 Mhr: Mhr,
614 Message: Message
615 },
616 messages: messages,
617 Mhr: {
618 commands: commands$2,
619 config: {
620 start: true
621 }
622 }
623});
624
625module.exports = cli;