UNPKG

3.29 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3/*
4 Awesome isomorphic NodeJS skeleton for structured applications.
5 Just have a look at the "bundles" that make up an EdenJS application.
6*/
7
8/*
9 ███████╗██████╗ ███████╗███╗ ██╗ ██╗███████╗
10 ██╔════╝██╔══██╗██╔════╝████╗ ██║ ██║██╔════╝
11 █████╗ ██║ ██║█████╗ ██╔██╗ ██║ ██║███████╗
12 ██╔══╝ ██║ ██║██╔══╝ ██║╚██╗██║██ ██║╚════██║
13 ███████╗██████╔╝███████╗██║ ╚████║╚█████╔╝███████║
14 ╚══════╝╚═════╝ ╚══════╝╚═╝ ╚═══╝ ╚════╝ ╚══════╝
15*/
16
17// Require environment
18require('./lib/env');
19
20// Require dependencies
21const chalk = require('chalk');
22const { EventEmitter } = require('events');
23const winston = require('winston');
24const glob = require('@edenjs/glob');
25const yargs = require('yargs');
26
27// Require internal utils
28const loader = require('lib/loader');
29const log = require('lib/utilities/log');
30
31// setup globals
32global.isCLI = true;
33
34class EdenCLI extends EventEmitter {
35 constructor() {
36 // run super
37 super();
38
39 this.building = this.build();
40 }
41
42 async build() {
43 const cliLocations = loader.getFiles('cli/**/*.js', global.bundleLocations);
44 const cliCommands = [];
45
46 for (const cliPath of await glob(cliLocations)) {
47 // eslint-disable-next-line global-require, import/no-dynamic-require
48 const cliModuleCommands = require(cliPath);
49
50 cliCommands.push(...cliModuleCommands);
51 }
52
53 let yy = yargs;
54
55 // Iterate with non-command modules first
56 const sortedCommands = cliCommands.sort((a, b) => {
57 return (a.command === null && b.command !== null ? -1 : 1);
58 });
59
60 for (const command of sortedCommands) {
61 if (command.command === null) {
62 yy = command.fn(yy);
63 } else {
64 yy = yy.command(command.command, command.description, command.fn.bind(this));
65 }
66 }
67
68 this._args = yy.argv;
69
70 // create logger
71 this.logger();
72
73 // get function
74 const [baseCommandName] = this._args._;
75
76 this._logger.log('info', `[${chalk.green(baseCommandName)}] Running`);
77
78 const callCommand = async (commandName, args = {}) => {
79 await cliCommands.find(c => (c.command || '').split(' ')[0] === commandName).handler.bind(this)(Object.assign({}, this._args, args), callCommand);
80 };
81
82 try {
83 await callCommand(baseCommandName);
84 } catch (err) {
85 global.printError(err);
86 process.exit(1);
87 }
88
89 process.exit(0);
90 }
91
92 /**
93 * Builds logger
94 */
95 logger() {
96 // Set logger
97 this._logger = winston.createLogger({
98 level : 'info',
99 format : log,
100 transports : [
101 new winston.transports.Console(),
102 ],
103 });
104 }
105}
106
107/**
108 * export default edenJS
109 *
110 * @type {EdenJS}
111 */
112new EdenCLI(); // eslint-disable-line no-new