1 | ;
|
2 | /*
|
3 | * @adonisjs/ace
|
4 | *
|
5 | * (c) Harminder Virk <virk@adonisjs.com>
|
6 | *
|
7 | * For the full copyright and license information, please view the LICENSE
|
8 | * file that was distributed with this source code.
|
9 | */
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | exports.sortAndGroupCommands = void 0;
|
12 | /**
|
13 | * Loops over the commands and converts them to an array of sorted groups with
|
14 | * nested commands inside them. The grouping is done using the command
|
15 | * namespace seperated with `:`. Example: `make:controller`
|
16 | */
|
17 | function sortAndGroupCommands(commands) {
|
18 | /**
|
19 | * Create a group of commands using it's namespace
|
20 | */
|
21 | const groupsLiteral = commands.reduce((result, command) => {
|
22 | const tokens = command.commandName.split(':');
|
23 | /**
|
24 | * Use the command namespace or move it inside the `root` group when
|
25 | * it is not namespaced.
|
26 | */
|
27 | const group = tokens.length > 1 ? tokens.shift() : 'root';
|
28 | result[group] = result[group] || [];
|
29 | result[group].push(command);
|
30 | return result;
|
31 | }, {});
|
32 | /**
|
33 | * Convert the object literal groups and it's command to an
|
34 | * array of sorted groups and commands
|
35 | */
|
36 | return Object.keys(groupsLiteral)
|
37 | .sort((prev, curr) => {
|
38 | if (prev === 'root') {
|
39 | return -1;
|
40 | }
|
41 | if (curr === 'root') {
|
42 | return 1;
|
43 | }
|
44 | if (curr > prev) {
|
45 | return -1;
|
46 | }
|
47 | if (curr < prev) {
|
48 | return 1;
|
49 | }
|
50 | return 0;
|
51 | })
|
52 | .map((name) => {
|
53 | return {
|
54 | group: name,
|
55 | commands: groupsLiteral[name].sort((prev, curr) => {
|
56 | if (curr.commandName > prev.commandName) {
|
57 | return -1;
|
58 | }
|
59 | if (curr.commandName < prev.commandName) {
|
60 | return 1;
|
61 | }
|
62 | return 0;
|
63 | }),
|
64 | };
|
65 | });
|
66 | }
|
67 | exports.sortAndGroupCommands = sortAndGroupCommands;
|