Skip to content
This repository was archived by the owner on Jan 19, 2026. It is now read-only.

Commit 8b20416

Browse files
Merge pull request #365 from ademarCardoso/feature/list-spaces
Feature/list spaces
2 parents 9e8d7e2 + 6c2426d commit 8b20416

7 files changed

Lines changed: 116 additions & 2 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ $ storyblok run-migration --space <SPACE_ID> --component <COMPONENT_NAME> --fiel
145145
* `field`: name of field
146146
* `dryrun`: when passed as an argument, does not perform the migration
147147

148+
### spaces
149+
150+
List all spaces of the logged account
151+
152+
```sh
153+
$ storyblok spaces
154+
```
155+
148156
### Help
149157

150158
For global help

src/cli.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,23 @@ program
283283
}
284284
})
285285

286+
// list spaces
287+
program
288+
.command('spaces')
289+
.description('List all spaces of the logged account')
290+
.action(async () => {
291+
try {
292+
if (!api.isAuthorized()) {
293+
await api.processLogin()
294+
}
295+
296+
await tasks.listSpaces(api)
297+
} catch (e) {
298+
console.log(chalk.red('X') + ' An error ocurred to listing sapces : ' + e.message)
299+
process.exit(1)
300+
}
301+
})
302+
286303
program.parse(process.argv)
287304

288305
if (program.rawArgs.length <= 2) {

src/tasks/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ module.exports = {
55
pullComponents: require('./pull-components'),
66
pushComponents: require('./push-components'),
77
generateMigration: require('./migrations/generate'),
8-
runMigration: require('./migrations/run')
8+
runMigration: require('./migrations/run'),
9+
listSpaces: require('./list-spaces')
910
}

src/tasks/list-spaces.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const chalk = require('chalk')
2+
3+
/**
4+
* @method listSpaces
5+
* @param api - Pass the api instance as a parameter
6+
* @return {Promise}
7+
*/
8+
9+
const listSpaces = async (api) => {
10+
console.log()
11+
console.log(chalk.green('✓') + ' Loading spaces...')
12+
console.log()
13+
14+
if (!api) {
15+
console.log(chalk.red('X') + 'Api instance is required to make the request')
16+
return []
17+
}
18+
19+
const spaces = await api.getAllSpaces()
20+
.then(res => res)
21+
.catch(err => Promise.reject(err))
22+
23+
if (!spaces) {
24+
console.log(chalk.red('X') + ' No spaces were found for this user ')
25+
return []
26+
}
27+
28+
spaces.map(space => {
29+
console.log(`${space.name} (id: ${space.id})`)
30+
})
31+
32+
return spaces
33+
}
34+
35+
module.exports = listSpaces

src/utils/api.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,12 @@ module.exports = {
189189
const _path = this.getPath(path)
190190

191191
return client[method](_path, props)
192+
},
193+
194+
async getAllSpaces () {
195+
return await this.getClient()
196+
.get('spaces/', {})
197+
.then(res => res.data.spaces || [])
198+
.catch(err => Promise.reject(err))
192199
}
193200
}

tests/constants.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,34 @@ const FAKE_STORIES = () => [
126126
}
127127
]
128128

129+
const FAKE_SPACES = () => [
130+
{
131+
name: 'Example Space',
132+
domain: 'https://example.storyblok.com',
133+
uniq_domain: null,
134+
plan: 'starter',
135+
plan_level: 0,
136+
limits: {},
137+
created_at: '2018-11-10T15:33:18.402Z',
138+
id: 0
139+
},
140+
{
141+
name: 'Example Space Two',
142+
domain: 'https://example-two.storyblok.com',
143+
uniq_domain: null,
144+
plan: 'starter',
145+
plan_level: 0,
146+
limits: {},
147+
created_at: '2018-11-10T15:33:18.402Z',
148+
id: 1
149+
}
150+
]
151+
129152
module.exports = {
130153
EMAIL_TEST,
131154
TOKEN_TEST,
132155
FAKE_STORIES,
133156
PASSWORD_TEST,
134-
FAKE_COMPONENTS
157+
FAKE_COMPONENTS,
158+
FAKE_SPACES
135159
}

tests/units/list-spaces.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const { listSpaces } = require('../../src/tasks/')
2+
const { FAKE_SPACES } = require('../constants')
3+
4+
describe('Test spaces method', () => {
5+
it('Testing list-spaces funtion without api instance', async () => {
6+
try {
7+
const spaces = await listSpaces()
8+
expect(spaces).toStrictEqual([])
9+
} catch (e) {
10+
console.error(e)
11+
}
12+
})
13+
it('Testing list-spaces funtion with api instance', async () => {
14+
const FAKE_API = {
15+
getAllSpaces: jest.fn(() => Promise.resolve(FAKE_SPACES()))
16+
}
17+
expect(
18+
await listSpaces(FAKE_API)
19+
).toEqual(FAKE_SPACES())
20+
expect(FAKE_API.getAllSpaces).toHaveBeenCalled()
21+
})
22+
})

0 commit comments

Comments
 (0)