1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
16 | return new (P || (P = Promise))(function (resolve, reject) {
|
17 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
18 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
19 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
20 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
21 | });
|
22 | };
|
23 | Object.defineProperty(exports, "__esModule", { value: true });
|
24 |
|
25 |
|
26 | const commandLineArgs = require("command-line-args");
|
27 | const path_1 = require("path");
|
28 | const logging = require("plylog");
|
29 | const polymer_project_config_1 = require("polymer-project-config");
|
30 | const args_1 = require("./args");
|
31 | const analyze_1 = require("./commands/analyze");
|
32 | const build_1 = require("./commands/build");
|
33 | const help_1 = require("./commands/help");
|
34 | const init_1 = require("./commands/init");
|
35 | const install_1 = require("./commands/install");
|
36 | const lint_1 = require("./commands/lint");
|
37 | const serve_1 = require("./commands/serve");
|
38 | const test_1 = require("./commands/test");
|
39 | const util_1 = require("./util");
|
40 | const commandLineCommands = require("command-line-commands");
|
41 | const logger = logging.getLogger('cli.main');
|
42 | process.on('uncaughtException', (error) => {
|
43 | logger.error(`Uncaught exception: ${error}`);
|
44 | if (error && error.stack)
|
45 | logger.error(error.stack);
|
46 | process.exit(1);
|
47 | });
|
48 | process.on('unhandledRejection', (error) => {
|
49 | logger.error(`Promise rejection: ${error}`);
|
50 | if (error && error.stack)
|
51 | logger.error(error.stack);
|
52 | process.exit(1);
|
53 | });
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | function parseCLIArgs(commandOptions) {
|
62 | commandOptions = commandOptions && commandOptions['_all'];
|
63 | const parsedOptions = Object.assign({}, commandOptions);
|
64 | if (commandOptions['extra-dependencies']) {
|
65 | parsedOptions.extraDependencies = commandOptions['extra-dependencies'];
|
66 | }
|
67 | if (commandOptions.fragment) {
|
68 | parsedOptions.fragments = commandOptions.fragment;
|
69 | }
|
70 | return parsedOptions;
|
71 | }
|
72 |
|
73 |
|
74 |
|
75 | function objectDashToCamelCase(input) {
|
76 | const output = {};
|
77 | for (const key of Object.keys(input)) {
|
78 | output[util_1.dashToCamelCase(key)] = input[key];
|
79 | }
|
80 | return output;
|
81 | }
|
82 | class PolymerCli {
|
83 | constructor(args, configOptions) {
|
84 | this.commands = new Map();
|
85 |
|
86 |
|
87 | if (args.indexOf('--quiet') > -1 || args.indexOf('-q') > -1) {
|
88 | logging.setQuiet();
|
89 | }
|
90 |
|
91 |
|
92 | if (args.indexOf('--verbose') > -1 || args.indexOf('-v') > -1) {
|
93 | logging.setVerbose();
|
94 | }
|
95 | this.args = args;
|
96 | logger.debug('got args:', { args: args });
|
97 | if (typeof configOptions !== 'undefined') {
|
98 | this.defaultConfigOptions = configOptions;
|
99 | logger.debug('got default config from constructor argument:', { config: this.defaultConfigOptions });
|
100 | }
|
101 | else {
|
102 | this.defaultConfigOptions =
|
103 | polymer_project_config_1.ProjectConfig.loadOptionsFromFile('polymer.json');
|
104 | if (this.defaultConfigOptions) {
|
105 | logger.debug('got default config from polymer.json file:', { config: this.defaultConfigOptions });
|
106 | }
|
107 | else {
|
108 | logger.debug('no polymer.json file found, no config loaded');
|
109 | }
|
110 | }
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | this.defaultConfigOptions = this.defaultConfigOptions || {};
|
117 | this.defaultConfigOptions.extraDependencies =
|
118 | this.defaultConfigOptions.extraDependencies || [];
|
119 | this.defaultConfigOptions.extraDependencies.unshift(`bower_components${path_1.sep}webcomponentsjs${path_1.sep}*.js`);
|
120 | this.addCommand(new analyze_1.AnalyzeCommand());
|
121 | this.addCommand(new build_1.BuildCommand());
|
122 | this.addCommand(new help_1.HelpCommand(this.commands));
|
123 | this.addCommand(new init_1.InitCommand());
|
124 | this.addCommand(new install_1.InstallCommand());
|
125 | this.addCommand(new lint_1.LintCommand());
|
126 | this.addCommand(new serve_1.ServeCommand());
|
127 | this.addCommand(new test_1.TestCommand());
|
128 | }
|
129 | addCommand(command) {
|
130 | logger.debug('adding command', command.name);
|
131 | this.commands.set(command.name, command);
|
132 | command.aliases.forEach((alias) => {
|
133 | logger.debug('adding alias', alias);
|
134 | this.commands.set(alias, command);
|
135 | });
|
136 | }
|
137 | run() {
|
138 | return __awaiter(this, void 0, void 0, function* () {
|
139 | const helpCommand = this.commands.get('help');
|
140 | const commandNames = Array.from(this.commands.keys());
|
141 | let parsedArgs;
|
142 | logger.debug('running...');
|
143 |
|
144 |
|
145 | if (this.args.indexOf('--version') > -1) {
|
146 | console.log(require('../package.json').version);
|
147 | return Promise.resolve();
|
148 | }
|
149 | try {
|
150 | parsedArgs = commandLineCommands(commandNames, this.args);
|
151 | }
|
152 | catch (error) {
|
153 |
|
154 |
|
155 |
|
156 | if (error.name === 'INVALID_COMMAND') {
|
157 | if (error.command) {
|
158 | logger.warn(`'${error.command}' is not an available command.`);
|
159 | }
|
160 | return helpCommand.run({ command: error.command }, new polymer_project_config_1.ProjectConfig(this.defaultConfigOptions));
|
161 | }
|
162 |
|
163 | throw error;
|
164 | }
|
165 | const commandName = parsedArgs.command;
|
166 | const commandArgs = parsedArgs.argv;
|
167 | const command = this.commands.get(commandName);
|
168 | if (command == null)
|
169 | throw new TypeError('command is null');
|
170 | logger.debug(`command '${commandName}' found, parsing command args:`, { args: commandArgs });
|
171 | const commandDefinitions = args_1.mergeArguments([command.args, args_1.globalArguments]);
|
172 | const commandOptionsRaw = commandLineArgs(commandDefinitions, { argv: commandArgs });
|
173 | const commandOptions = parseCLIArgs(commandOptionsRaw);
|
174 | logger.debug(`command options parsed from args:`, commandOptions);
|
175 | const mergedConfigOptions = Object.assign({}, this.defaultConfigOptions, objectDashToCamelCase(commandOptions));
|
176 | logger.debug(`final config options:`, mergedConfigOptions);
|
177 | const config = new polymer_project_config_1.ProjectConfig(mergedConfigOptions);
|
178 | logger.debug(`final project configuration generated:`, config);
|
179 |
|
180 |
|
181 |
|
182 | if (commandOptions['help']) {
|
183 | logger.debug(`'--help' option found, running 'help' for given command...`);
|
184 | return helpCommand.run({ command: commandName }, config);
|
185 | }
|
186 | logger.debug('Running command...');
|
187 | return command.run(commandOptions, config);
|
188 | });
|
189 | }
|
190 | }
|
191 | exports.PolymerCli = PolymerCli;
|
192 |
|
\ | No newline at end of file |