Skip to content

Commit

Permalink
feat(content-type): enhance list command functionality (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
makinwab authored Mar 14, 2019
1 parent f76d168 commit 564d648
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 11 deletions.
10 changes: 6 additions & 4 deletions docs/content-type/list/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ Prints the Content Types in a space
Usage: contentful content-type list [options]
Options:
--space-id Space id [string]
--space-id, -s Space id [string]
--management-token, -mt Management token [string]
--environment-id, -e Environment id [string]
```

### Example

```shell
contentful content-type list --space-id xxx
contentful content-type list --space-id xxx -e master

Environment: "master"
┌───────────────────┬────────────────────────┐
│ Content Type Name │ Content Type ID │
├───────────────────┼────────────────────────┤
│ Test │ test
└───────────────────┴────────────────────────┘
```


26 changes: 21 additions & 5 deletions lib/cmds/content-type_cmds/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,52 @@ import { assertLoggedIn, assertSpaceIdProvided } from '../../utils/assertions'
import { handleAsyncError as handle } from '../../utils/async'
import { log } from '../../utils/log'
import paginate from '../../utils/pagination'
import { highlightStyle } from '../../utils/styles'

export const command = 'list'

export const desc = 'List your content types'

export const aliases = ['ls']

export const builder = (yargs) => {
export const builder = yargs => {
return yargs
.option('space-id', { alias: 's', type: 'string', describe: 'Space id' })
.option('management-token', { alias: 'mt', type: 'string', describe: 'Contentful management API token' })
.option('management-token', {
alias: 'mt',
type: 'string',
describe: 'Contentful management API token'
})
.option('environment-id', {
alias: 'e',
type: 'string',
describe:
'Environment ID you want to interact with. Defaults to the current active environment.'
})
.epilog('Copyright 2018 Contentful, this is a BETA release')
}

async function ctList (argv) {
await assertLoggedIn(argv)
await assertSpaceIdProvided(argv)

const { cmaToken, activeSpaceId } = await getContext()
const { cmaToken, activeSpaceId, activeEnvironmentId } = await getContext()
const managementToken = argv.managementToken || cmaToken
const spaceId = argv.spaceId || activeSpaceId
const environmentId = argv.environmentId || activeEnvironmentId || 'master'

const client = await createManagementClient({
accessToken: managementToken,
feature: 'content_type-list'
})

const space = await client.getSpace(spaceId)
const environment = await space.getEnvironment(environmentId)

log(highlightStyle(`Environment: "${environmentId}"`))

const result = await paginate({
client: space,
client: environment,
method: 'getContentTypes',
query: {
order: 'name,sys.id'
Expand All @@ -46,7 +62,7 @@ async function ctList (argv) {
head: ['Content Type Name', 'Content Type ID']
})

result.items.forEach((contentType) => {
result.items.forEach(contentType => {
table.push([contentType.name, contentType.sys.id])
})

Expand Down
17 changes: 15 additions & 2 deletions lib/cmds/space_cmds/environment_cmds/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { handleAsyncError as handle } from '../../../utils/async'
import { createManagementClient } from '../../../utils/contentful-clients'
import { log } from '../../../utils/log'
import paginate from '../../../utils/pagination'
import { highlightStyle } from '../../../utils/styles'

export const command = 'list'

Expand Down Expand Up @@ -37,7 +38,7 @@ export async function environmentList (argv) {
await assertLoggedIn(argv)
await assertSpaceIdProvided(argv)

const { cmaToken, activeSpaceId } = await getContext()
const { cmaToken, activeSpaceId, activeEnvironmentId } = await getContext()
const spaceId = argv.spaceId || activeSpaceId
const managementToken = argv.managementToken || cmaToken

Expand All @@ -57,7 +58,19 @@ export async function environmentList (argv) {
})

environments.forEach((environment) => {
table.push([environment.name, environment.sys.id, environment.sys.status.sys.id])
if (activeEnvironmentId === environment.sys.id) {
table.push([
highlightStyle(`${environment.name} [active]`),
highlightStyle(environment.sys.id),
highlightStyle(environment.sys.status.sys.id)
])
} else {
table.push([
environment.name,
environment.sys.id,
environment.sys.status.sys.id
])
}
})

log(table.toString())
Expand Down
138 changes: 138 additions & 0 deletions test/unit/cmds/content-type_cmds/list.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { handler } from '../../../../lib/cmds/content-type_cmds/list'
import { getContext } from '../../../../lib/context'
import { log } from '../../../../lib/utils/log'
import { createManagementClient } from '../../../../lib/utils/contentful-clients'

jest.mock('../../../../lib/context')
jest.mock('../../../../lib/utils/log')
jest.mock('../../../../lib/utils/contentful-clients')

const mockContentTypes = [
{
name: 'content type name',
sys: {
id: 'mockedMasterCT',
environment: {
sys: {
id: 'master'
}
}
}
},
{
name: 'content type name',
sys: {
id: 'mockedDevelopCT',
environment: {
sys: {
id: 'develop'
}
}
}
},
{
name: 'content type name',
sys: {
id: 'mockedTestCT',
environment: {
sys: {
id: 'test'
}
}
}
}
]

const getContentTypesSub = jest.fn().mockResolvedValue({
items: mockContentTypes
})

const fakeClient = {
getSpace: async () => ({
getEnvironment: async () => ({
getContentTypes: getContentTypesSub
})
})
}
createManagementClient.mockResolvedValue(fakeClient)

getContext.mockResolvedValue({
cmaToken: 'mockedToken',
activeSpaceId: 'someSpaceId'
})

afterEach(() => {
createManagementClient.mockClear()
getContentTypesSub.mockClear()
log.mockClear()
})

test('List content types from default environment, "master"', async () => {
await handler({})

expect(createManagementClient).toHaveBeenCalledTimes(1)
expect(getContentTypesSub).toHaveBeenCalledTimes(1)

expect(log.mock.calls[0][0]).toContain(
mockContentTypes[0].sys.environment.sys.id
)
expect(log.mock.calls[0][0]).not.toContain(
mockContentTypes[1].sys.environment.sys.id
)
expect(log.mock.calls[0][0]).not.toContain(
mockContentTypes[2].sys.environment.sys.id
)

expect(log.mock.calls[1][0]).toContain(mockContentTypes[0].name)
expect(log.mock.calls[1][0]).toContain(mockContentTypes[0].sys.id)
})

test('List content types based on active environment if available', async () => {
getContext.mockResolvedValue({
cmaToken: 'mockedToken',
activeSpaceId: 'someSpaceId',
activeEnvironmentId: 'develop'
})

await handler({})

expect(createManagementClient).toHaveBeenCalledTimes(1)
expect(getContentTypesSub).toHaveBeenCalledTimes(1)

expect(log.mock.calls[0][0]).not.toContain(
mockContentTypes[0].sys.environment.sys.id
)
expect(log.mock.calls[0][0]).toContain(
mockContentTypes[1].sys.environment.sys.id
)
expect(log.mock.calls[0][0]).not.toContain(
mockContentTypes[2].sys.environment.sys.id
)

expect(log.mock.calls[1][0]).toContain(mockContentTypes[1].name)
expect(log.mock.calls[1][0]).toContain(mockContentTypes[1].sys.id)
})

test('List content types based on environment passed if --environment-id option is used', async () => {
const stubArgv = {
environmentId: 'test'
}

await handler(stubArgv)

expect(createManagementClient).toHaveBeenCalledTimes(1)
expect(getContentTypesSub).toHaveBeenCalledTimes(1)

expect(log.mock.calls[0][0]).not.toContain(
mockContentTypes[0].sys.environment.sys.id
)
expect(log.mock.calls[0][0]).not.toContain(
mockContentTypes[1].sys.environment.sys.id
)
expect(log.mock.calls[0][0]).toContain(
mockContentTypes[2].sys.environment.sys.id
)

expect(log.mock.calls[1][0]).toContain(mockContentTypes[2].name)
expect(log.mock.calls[1][0]).toContain(mockContentTypes[2].sys.id)
})

0 comments on commit 564d648

Please sign in to comment.