Skip to content

Commit

Permalink
feat(ace): add command sorting and grouping helper
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Apr 8, 2019
1 parent 81877de commit 07f662f
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
77 changes: 77 additions & 0 deletions packages/ace/src/utils/sortAndGroupCommands.ts
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
}),
}
})
}
72 changes: 72 additions & 0 deletions packages/ace/test/sort-commands.spec.ts
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] },
])
})
})

0 comments on commit 07f662f

Please sign in to comment.