1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | require('./lib/env');
|
19 |
|
20 |
|
21 | const chalk = require('chalk');
|
22 | const { EventEmitter } = require('events');
|
23 | const winston = require('winston');
|
24 | const glob = require('@edenjs/glob');
|
25 | const yargs = require('yargs');
|
26 |
|
27 |
|
28 | const loader = require('lib/loader');
|
29 | const log = require('lib/utilities/log');
|
30 |
|
31 |
|
32 | global.isCLI = true;
|
33 |
|
34 | class EdenCLI extends EventEmitter {
|
35 | constructor() {
|
36 |
|
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 |
|
48 | const cliModuleCommands = require(cliPath);
|
49 |
|
50 | cliCommands.push(...cliModuleCommands);
|
51 | }
|
52 |
|
53 | let yy = yargs;
|
54 |
|
55 |
|
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 |
|
71 | this.logger();
|
72 |
|
73 |
|
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 |
|
94 |
|
95 | logger() {
|
96 |
|
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 |
|
109 |
|
110 |
|
111 |
|
112 | new EdenCLI();
|