1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | require('@babel/polyfill');
|
6 |
|
7 | const path = require('path');
|
8 |
|
9 | const buntstift = require('buntstift'),
|
10 | commandLineArgs = require('command-line-args'),
|
11 | commandLineCommands = require('command-line-commands'),
|
12 | findSuggestions = require('findsuggestions'),
|
13 | updateNotifier = require('update-notifier');
|
14 |
|
15 | const commands = require('../cli/commands'),
|
16 | errors = require('../errors'),
|
17 | globalOptionDefinitions = require('../cli/globalOptionDefinitions'),
|
18 | packageJson = require('../../package.json'),
|
19 | shell = require('../shell/'),
|
20 | ui = require('../cli/ui/');
|
21 |
|
22 | const defaultCommand = 'qa';
|
23 |
|
24 | updateNotifier({ pkg: packageJson }).notify();
|
25 |
|
26 | const exit = function (ex) {
|
27 | buntstift.line();
|
28 |
|
29 | if (!ex) {
|
30 | buntstift.exit(0);
|
31 |
|
32 | return;
|
33 | }
|
34 |
|
35 | if (ex.message) {
|
36 | buntstift.verbose(ex.message);
|
37 | }
|
38 |
|
39 | if (ex.stack) {
|
40 | buntstift.verbose(ex.stack);
|
41 | }
|
42 |
|
43 | buntstift.exit(1);
|
44 | };
|
45 |
|
46 | (async function () {
|
47 | ui.progress.show();
|
48 |
|
49 | const validCommands = Object.keys(commands);
|
50 |
|
51 | let packageJsonOfModule,
|
52 | parsed;
|
53 |
|
54 | const cwd = process.cwd(),
|
55 | packageJsonPath = path.join(cwd, 'package.json');
|
56 |
|
57 | try {
|
58 |
|
59 | packageJsonOfModule = require(packageJsonPath);
|
60 |
|
61 | } catch {
|
62 |
|
63 | }
|
64 |
|
65 | try {
|
66 | parsed = commandLineCommands([ null, ...validCommands ]);
|
67 | } catch (ex) {
|
68 | if (packageJsonOfModule && packageJsonOfModule.scripts && packageJsonOfModule.scripts[ex.command]) {
|
69 | const args = process.argv.splice(3);
|
70 |
|
71 | try {
|
72 | await shell.execLive(`npm`, { args: [ 'run', ex.command, ...args ], cwd });
|
73 | } catch (exShellExecLive) {
|
74 | exit(exShellExecLive);
|
75 | }
|
76 |
|
77 | exit();
|
78 | }
|
79 |
|
80 | const suggestions = findSuggestions({ for: ex.command, in: validCommands });
|
81 |
|
82 | buntstift.error(`Unknown command '${ex.command}', did you mean '${suggestions[0].suggestion}'?`);
|
83 | buntstift.exit(1);
|
84 | }
|
85 |
|
86 | if (!parsed.command) {
|
87 | parsed.command = defaultCommand;
|
88 |
|
89 | if (parsed.argv.length > 0) {
|
90 | if (parsed.argv.includes('--version') || parsed.argv.includes('-v')) {
|
91 | buntstift.info(packageJson.version);
|
92 | buntstift.exit(0);
|
93 | }
|
94 |
|
95 | if (parsed.argv.includes('--help') || parsed.argv.includes('-h')) {
|
96 | parsed.command = 'help';
|
97 | }
|
98 | }
|
99 | }
|
100 |
|
101 | const command = commands[parsed.command];
|
102 | const validOptionDefinitions = [ ...globalOptionDefinitions, ...await command.getOptionDefinitions() ];
|
103 |
|
104 | const args = commandLineArgs(validOptionDefinitions, { argv: parsed.argv, partial: true });
|
105 |
|
106 |
|
107 | if (args._unknown && args._unknown.length > 0) {
|
108 | buntstift.error(`Unknown argument '${args._unknown[0]}'.`);
|
109 | buntstift.exit(1);
|
110 | }
|
111 |
|
112 |
|
113 | try {
|
114 | if (!packageJsonOfModule && parsed.command !== 'help') {
|
115 | throw new errors.PackageJsonMissing();
|
116 | }
|
117 |
|
118 | await command.run(args);
|
119 | } catch (ex) {
|
120 | switch (ex.code) {
|
121 | case 'EPACKAGEJSONMISSING':
|
122 | ui.error('package.json is missing.');
|
123 | break;
|
124 | default:
|
125 | }
|
126 |
|
127 | exit(ex);
|
128 | }
|
129 |
|
130 | exit();
|
131 | })();
|