-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
executable file
·135 lines (110 loc) · 3.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env node
/*
Awesome isomorphic NodeJS skeleton for structured applications.
Just have a look at the "bundles" that make up an EdenJS application.
*/
/*
███████╗██████╗ ███████╗███╗ ██╗ ██╗███████╗
██╔════╝██╔══██╗██╔════╝████╗ ██║ ██║██╔════╝
█████╗ ██║ ██║█████╗ ██╔██╗ ██║ ██║███████╗
██╔══╝ ██║ ██║██╔══╝ ██║╚██╗██║██ ██║╚════██║
███████╗██████╔╝███████╗██║ ╚████║╚█████╔╝███████║
╚══════╝╚═════╝ ╚══════╝╚═╝ ╚═══╝ ╚════╝ ╚══════╝
*/
// Require environment
require('./lib/env');
// Require dependencies
const glob = require('@edenjs/glob');
const chalk = require('chalk');
const yargs = require('yargs');
const winston = require('winston');
const { EventEmitter } = require('events');
// Require internal utils
const log = require('lib/utilities/log');
const loader = require('lib/loader');
// setup globals
global.isCLI = true;
/**
* create eden CLI
*/
class EdenCLI extends EventEmitter {
/**
* construct eden CLI
*/
constructor() {
// run super
super();
// set building promise
this.building = this.build();
}
/**
* build as async
*/
async build() {
// locations
const cliLocations = loader.getFiles('cli/**/*.js', global.bundleLocations);
// set cli commands
let cliCommands = [];
// create logger
const logger = this.logger();
// glob import locations
for (const cliPath of await glob(cliLocations)) {
// eslint-disable-next-line global-require, import/no-dynamic-require
const cliModule = require(cliPath);
// init
if (cliModule.build) {
// push commands from init function
cliCommands.push(...(await cliModule.build(yargs, logger, this)));
}
}
// Iterate with non-command modules first
cliCommands = cliCommands.sort((a, b) => {
// return priorities
const aP = a.priority || 10;
const bP = b.priority || 10;
// return commands
if (aP > bP) return -1;
if (bP > aP) return 1;
return 0;
});
// yargs
const { argv } = yargs;
// get base command
const [baseCommandName] = argv._;
// log running
this._logger.log('info', `[${chalk.green(baseCommandName)}] Running`);
// find command
const command = cliCommands.find(c => (c.command || '').split(' ')[0] === baseCommandName);
// run command
try {
// run command
await command.run(Object.assign({}, argv));
} catch (e) {
global.printError(e);
process.exit(1);
}
// exit
process.exit(0);
}
/**
* Builds logger
*/
logger() {
// Set logger
this._logger = winston.createLogger({
level : 'info',
format : log,
transports : [
new winston.transports.Console(),
],
});
// return logger
return this._logger;
}
}
/**
* export default edenJS
*
* @type {EdenJS}
*/
new EdenCLI(); // eslint-disable-line no-new