-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ace): add command sorting and grouping helper
- Loading branch information
1 parent
81877de
commit 07f662f
Showing
2 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* @adonisjs/ace | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { CommandConstructorContract } from '../Contracts' | ||
|
||
/** | ||
* Shape of groups and their commands | ||
*/ | ||
type Grouped = { | ||
group: string, | ||
commands: CommandConstructorContract[], | ||
}[] | ||
|
||
/** | ||
* Loops over the commands and converts them to an array of sorted groups with | ||
* nested commands inside them. The grouping is done using the command | ||
* namespace seperated with `:`. Example: `make:controller` | ||
*/ | ||
export function sortAndGroupCommands (commands: CommandConstructorContract[]): Grouped { | ||
/** | ||
* Create a group of commands using it's namespace | ||
*/ | ||
const groupsLiteral = commands.reduce((result, command) => { | ||
const tokens = command.commandName.split(':') | ||
const group = tokens.length > 1 ? tokens.shift()! : 'root' | ||
result[group] = result[group] || [] | ||
result[group].push(command) | ||
return result | ||
}, {} as { [key: string]: CommandConstructorContract[] }) | ||
|
||
/** | ||
* Convert the object literal groups and it's command to an | ||
* array of sorted groups and commands | ||
*/ | ||
return Object.keys(groupsLiteral) | ||
.sort((prev, curr) => { | ||
if (prev === 'root') { | ||
return -1 | ||
} | ||
|
||
if (curr === 'root') { | ||
return 1 | ||
} | ||
|
||
if (curr > prev) { | ||
return -1 | ||
} | ||
|
||
if (curr < prev) { | ||
return 1 | ||
} | ||
|
||
return 0 | ||
}) | ||
.map((name) => { | ||
return { | ||
group: name, | ||
commands: groupsLiteral[name].sort((prev, curr) => { | ||
if (curr.commandName > prev.commandName) { | ||
return -1 | ||
} | ||
|
||
if (curr.commandName < prev.commandName) { | ||
return 1 | ||
} | ||
|
||
return 0 | ||
}), | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* @adonisjs/ace | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import * as test from 'japa' | ||
import { sortAndGroupCommands } from '../src/utils/sortAndGroupCommands' | ||
import { BaseCommand } from '../src/BaseCommand' | ||
|
||
test.group('utils | sortAndGroupCommands', () => { | ||
test('sort commands in alphabetical order', (assert) => { | ||
class Greet extends BaseCommand { | ||
public static commandName = 'greet' | ||
} | ||
|
||
class Run extends BaseCommand { | ||
public static commandName = 'run' | ||
} | ||
|
||
const output = sortAndGroupCommands([Run, Greet]) | ||
assert.deepEqual(output, [{ group: 'root', commands: [Greet, Run] }]) | ||
}) | ||
|
||
test('sort and group commands in alphabetical order', (assert) => { | ||
class MakeController extends BaseCommand { | ||
public static commandName = 'make:controller' | ||
} | ||
|
||
class MakeModel extends BaseCommand { | ||
public static commandName = 'make:model' | ||
} | ||
|
||
class Run extends BaseCommand { | ||
public static commandName = 'run' | ||
} | ||
|
||
const output = sortAndGroupCommands([MakeController, MakeModel, Run]) | ||
assert.deepEqual(output, [ | ||
{ group: 'root', commands: [Run] }, | ||
{ group: 'make', commands: [MakeController, MakeModel] }, | ||
]) | ||
}) | ||
|
||
test('sort groups in alphabetical order too', (assert) => { | ||
class MakeController extends BaseCommand { | ||
public static commandName = 'make:controller' | ||
} | ||
|
||
class MakeModel extends BaseCommand { | ||
public static commandName = 'make:model' | ||
} | ||
|
||
class AuthScaffold extends BaseCommand { | ||
public static commandName = 'auth:scaffold' | ||
} | ||
|
||
class Run extends BaseCommand { | ||
public static commandName = 'run' | ||
} | ||
|
||
const output = sortAndGroupCommands([MakeController, MakeModel, Run, AuthScaffold]) | ||
assert.deepEqual(output, [ | ||
{ group: 'root', commands: [Run] }, | ||
{ group: 'auth', commands: [AuthScaffold] }, | ||
{ group: 'make', commands: [MakeController, MakeModel] }, | ||
]) | ||
}) | ||
}) |