UNPKG

3.33 kBJavaScriptView Raw
1const fs = require('fs');
2const path = require('path');
3const yargs = require('yargs');
4const yeoman = require('yeoman-environment');
5
6const { executeInScope } = require('./execution-scope');
7
8exports.bootstrap = (eg, adapter) => {
9 const env = yeoman.createEnv();
10
11 if (executeInScope(env)) {
12 return;
13 }
14
15 env.eg = eg;
16
17 if (adapter) {
18 env.adapter = adapter;
19 }
20
21 const program = yargs;
22
23 const generatorsPath = path.join(__dirname, 'generators');
24
25 const prefix = 'express-gateway';
26
27 const commands = [];
28 const subCommands = {};
29
30 const dirs = fs
31 .readdirSync(generatorsPath)
32 .filter(dir => {
33 if (dir[0] === '.') {
34 return false;
35 }
36
37 const stat = fs.statSync(path.join(generatorsPath, dir));
38 return stat.isDirectory();
39 });
40
41 dirs.forEach(dir => {
42 const directoryPath = path.join(generatorsPath, dir);
43
44 const files = fs
45 .readdirSync(directoryPath)
46 .filter(file => {
47 if (file[0] === '.') {
48 return false;
49 }
50
51 const stat = fs.statSync(path.join(directoryPath, file));
52 return stat.isFile();
53 });
54
55 files.forEach(file => {
56 if (file === 'index.js') {
57 const namespace = `${prefix}:${dir}`;
58 commands.push({ namespace: namespace, path: directoryPath });
59 env.register(directoryPath, namespace);
60 return;
61 }
62
63 const filePath = path.join(directoryPath, file);
64 const namespace = `${prefix}:${dir}:${file.slice(0, -3)}`;
65
66 if (!subCommands.hasOwnProperty(dir)) {
67 subCommands[dir] = [];
68 }
69
70 subCommands[dir].push({
71 namespace: namespace,
72 path: filePath
73 });
74
75 env.register(filePath, namespace);
76 });
77 });
78
79 const commandAliases = {};
80 // Ex: {
81 // 'user': 'users',
82 // 'users': 'users'
83 // }
84 commands.forEach(command => {
85 const generator = env.create(command.namespace);
86
87 let aliases = generator._configuration.command;
88 if (!Array.isArray(aliases)) {
89 aliases = [aliases];
90 }
91
92 aliases = aliases.map(alias => {
93 return alias.split(/\s/)[0];
94 });
95
96 const commandName = command.namespace.split(':')[1];
97
98 aliases.forEach(a => {
99 commandAliases[a] = commandName;
100 });
101
102 program.command(generator._configuration);
103 });
104
105 const subCommandAliases = {};
106 // Ex: {
107 // 'users': {
108 // 'rm': 'remove',
109 // 'remove: 'remove'
110 // }
111 // }
112 Object.keys(subCommands).forEach(key => {
113 const subCommandArray = subCommands[key];
114
115 subCommandAliases[key] = {};
116
117 subCommandArray.forEach(s => {
118 const generator = env.create(s.namespace);
119
120 let aliases = generator._configuration.command;
121
122 if (!Array.isArray(aliases)) {
123 aliases = [aliases];
124 }
125
126 aliases = aliases.map(alias => {
127 return alias.split(/\s/)[0];
128 });
129
130 const commandName = s.namespace.split(':')[2];
131
132 aliases.forEach(a => {
133 subCommandAliases[key][a] = commandName;
134 });
135 });
136 });
137
138 env.commandAliases = [commandAliases, subCommandAliases];
139
140 program
141 .usage('Usage: $0 <command> [options]')
142 .demandCommand()
143 .recommendCommands()
144 .strict()
145 .alias('h', 'help')
146 .wrap(Math.min(90, yargs.terminalWidth()));
147
148 return { program, env };
149};