forked from strongloop/loopback-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloopback-cli.js
More file actions
executable file
·88 lines (72 loc) · 2.71 KB
/
loopback-cli.js
File metadata and controls
executable file
·88 lines (72 loc) · 2.71 KB
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
#!/usr/bin/env node
// Copyright IBM Corp. 2016. 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('debug')('loopback:cli');
const minimist = require('minimist');
const path = require('path');
const opts = minimist(process.argv.slice(2), {
alias: {
help: 'h',
version: 'v',
commands: 'l',
},
});
if (opts.version) {
const ourVersion = require('../package.json').version;
const generatorVersion = require('generator-loopback/package.json').version;
const workspaceVersion = require('generator-loopback').workspaceVersion;
console.log('%s (generator-loopback@%s loopback-workspace@%s)',
ourVersion, generatorVersion, workspaceVersion);
return;
}
// Tell the generator to replace "yo loopback:" with "lb"
process.env.SLC_COMMAND = 'loopback-cli';
// NOTE(bajtos) Loading generator-loopback takes about a second,
// therefore I am intentionally loading it only after we have
// handled the "--version" case which becomes much faster as the result.
const lbGenerator = require('generator-loopback');
const yeoman = lbGenerator._yeoman; // generator-loopback should export _yeoman
assert(yeoman, 'generator-loopback should export _yeoman');
const env = yeoman();
// Change the working directory to the generator-loopback module so that
// yeoman can discover the generators
const root = path.dirname(require.resolve('generator-loopback/package.json'));
const cwd = process.cwd();
debug('changing directory to %s', root);
process.chdir(root);
// lookup for every namespaces, within the environments.paths and lookups
env.lookup();
debug('changing directory back to %s', cwd);
process.chdir(cwd); // Switch back
// list generators
if (opts.commands) {
console.log('Available commands: ');
var list = Object.keys(env.getGeneratorsMeta())
.filter(name => /^loopback:/.test(name))
.map(name => name.replace(/^loopback:/, ' lb '));
console.log(list.join('\n'));
return;
}
const args = opts._;
const originalCommand = args.shift();
let command = 'loopback:' + (originalCommand || 'app');
const supportedCommands = env.getGeneratorsMeta();
if (!(command in supportedCommands)) {
command = 'loopback: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);
const generator = env.create(command, {args});
debug('env.run %j %j', args, options);
env.run(args, options);