-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathcli.js
executable file
·71 lines (59 loc) · 1.8 KB
/
cli.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
#!/usr/bin/env node
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: @loopback/cli
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
const assert = require('assert');
const camelCaseKeys = require('camelcase-keys');
const debug = require('../lib/debug')();
const minimist = require('minimist');
const path = require('path');
const yeoman = require('yeoman-environment');
const opts = minimist(process.argv.slice(2), {
alias: {
version: 'v',
commands: 'l',
},
});
if (opts.version) {
const ver = require('../package.json').version;
console.log('Version: %s', ver);
return;
}
var env = yeoman.createEnv();
env.register(path.join(__dirname, '../generators/app'), 'loopback4:app');
env.register(
path.join(__dirname, '../generators/extension'),
'loopback4:extension'
);
env.register(
path.join(__dirname, '../generators/controller'),
'loopback4:controller'
);
// list generators
if (opts.commands) {
console.log('Available commands: ');
var list = Object.keys(env.getGeneratorsMeta())
.filter(name => /^loopback4:/.test(name))
.map(name => name.replace(/^loopback4:/, ' lb4 '));
console.log(list.join('\n'));
return;
}
const args = opts._;
const originalCommand = args.shift();
let command = 'loopback4:' + (originalCommand || 'app');
const supportedCommands = env.getGeneratorsMeta();
if (!(command in supportedCommands)) {
command = 'loopback4:app';
args.unshift(originalCommand);
args.unshift(command);
} else {
args.unshift(command);
}
debug('invoking generator', args);
// `yo` is adding flags converted to CamelCase
const options = camelCaseKeys(opts, {exclude: ['--', /^\w$/, 'argv']});
Object.assign(options, opts);
debug('env.run %j %j', args, options);
env.run(args, options);