Skip to content

Commit

Permalink
Merge pull request #365 from ademarCardoso/feature/list-spaces
Browse files Browse the repository at this point in the history
Feature/list spaces
  • Loading branch information
emanuelgsouza authored Jul 10, 2020
2 parents 9e8d7e2 + 6c2426d commit 8b20416
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ $ storyblok run-migration --space <SPACE_ID> --component <COMPONENT_NAME> --fiel
* `field`: name of field
* `dryrun`: when passed as an argument, does not perform the migration

### spaces

List all spaces of the logged account

```sh
$ storyblok spaces
```

### Help

For global help
Expand Down
17 changes: 17 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,23 @@ program
}
})

// list spaces
program
.command('spaces')
.description('List all spaces of the logged account')
.action(async () => {
try {
if (!api.isAuthorized()) {
await api.processLogin()
}

await tasks.listSpaces(api)
} catch (e) {
console.log(chalk.red('X') + ' An error ocurred to listing sapces : ' + e.message)
process.exit(1)
}
})

program.parse(process.argv)

if (program.rawArgs.length <= 2) {
Expand Down
3 changes: 2 additions & 1 deletion src/tasks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ module.exports = {
pullComponents: require('./pull-components'),
pushComponents: require('./push-components'),
generateMigration: require('./migrations/generate'),
runMigration: require('./migrations/run')
runMigration: require('./migrations/run'),
listSpaces: require('./list-spaces')
}
35 changes: 35 additions & 0 deletions src/tasks/list-spaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const chalk = require('chalk')

/**
* @method listSpaces
* @param api - Pass the api instance as a parameter
* @return {Promise}
*/

const listSpaces = async (api) => {
console.log()
console.log(chalk.green('✓') + ' Loading spaces...')
console.log()

if (!api) {
console.log(chalk.red('X') + 'Api instance is required to make the request')
return []
}

const spaces = await api.getAllSpaces()
.then(res => res)
.catch(err => Promise.reject(err))

if (!spaces) {
console.log(chalk.red('X') + ' No spaces were found for this user ')
return []
}

spaces.map(space => {
console.log(`${space.name} (id: ${space.id})`)
})

return spaces
}

module.exports = listSpaces
7 changes: 7 additions & 0 deletions src/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,12 @@ module.exports = {
const _path = this.getPath(path)

return client[method](_path, props)
},

async getAllSpaces () {
return await this.getClient()
.get('spaces/', {})
.then(res => res.data.spaces || [])
.catch(err => Promise.reject(err))
}
}
26 changes: 25 additions & 1 deletion tests/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,34 @@ const FAKE_STORIES = () => [
}
]

const FAKE_SPACES = () => [
{
name: 'Example Space',
domain: 'https://example.storyblok.com',
uniq_domain: null,
plan: 'starter',
plan_level: 0,
limits: {},
created_at: '2018-11-10T15:33:18.402Z',
id: 0
},
{
name: 'Example Space Two',
domain: 'https://example-two.storyblok.com',
uniq_domain: null,
plan: 'starter',
plan_level: 0,
limits: {},
created_at: '2018-11-10T15:33:18.402Z',
id: 1
}
]

module.exports = {
EMAIL_TEST,
TOKEN_TEST,
FAKE_STORIES,
PASSWORD_TEST,
FAKE_COMPONENTS
FAKE_COMPONENTS,
FAKE_SPACES
}
22 changes: 22 additions & 0 deletions tests/units/list-spaces.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { listSpaces } = require('../../src/tasks/')
const { FAKE_SPACES } = require('../constants')

describe('Test spaces method', () => {
it('Testing list-spaces funtion without api instance', async () => {
try {
const spaces = await listSpaces()
expect(spaces).toStrictEqual([])
} catch (e) {
console.error(e)
}
})
it('Testing list-spaces funtion with api instance', async () => {
const FAKE_API = {
getAllSpaces: jest.fn(() => Promise.resolve(FAKE_SPACES()))
}
expect(
await listSpaces(FAKE_API)
).toEqual(FAKE_SPACES())
expect(FAKE_API.getAllSpaces).toHaveBeenCalled()
})
})

0 comments on commit 8b20416

Please sign in to comment.