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

Commit 86b6a9a

Browse files
author
Thiago Saife
authored
Merge pull request #806 from plckr/master
feature: add pull-languages command
2 parents bd16770 + 88fd1a4 commit 86b6a9a

7 files changed

Lines changed: 169 additions & 1 deletion

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ Usage to kickstart a boilerplate, fieldtype or theme
2828
$ storyblok select
2929
```
3030

31+
### pull-languages
32+
33+
Download your space's languages schema as json. This command will download 1 file.
34+
35+
```sh
36+
$ storyblok pull-languages --space <SPACE_ID>
37+
```
38+
39+
#### Options
40+
41+
* `space`: your space id
42+
3143
### pull-components
3244

3345
Download your space's components schema as json. This command will download 2 files: 1 for the components and 1 for the presets.

src/cli.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,31 @@ program
6969
}
7070
})
7171

72+
// pull-languages
73+
program
74+
.command('pull-languages')
75+
.description("Download your space's languages schema as json")
76+
.action(async () => {
77+
console.log(`${chalk.blue('-')} Executing pull-languages task`)
78+
const space = program.space
79+
if (!space) {
80+
console.log(chalk.red('X') + ' Please provide the space as argument --space YOUR_SPACE_ID.')
81+
process.exit(0)
82+
}
83+
84+
try {
85+
if (!api.isAuthorized()) {
86+
await api.processLogin()
87+
}
88+
89+
api.setSpaceId(space)
90+
await tasks.pullLanguages(api, { space })
91+
} catch (e) {
92+
console.log(chalk.red('X') + ' An error occurred when executing the pull-languages task: ' + e.message)
93+
process.exit(1)
94+
}
95+
})
96+
7297
// pull-components
7398
program
7499
.command(COMMANDS.PULL_COMPONENTS)

src/tasks/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = {
33
scaffold: require('./scaffold'),
44
quickstart: require('./quickstart'),
55
pullComponents: require('./pull-components'),
6+
pullLanguages: require('./pull-languages'),
67
pushComponents: require('./push-components'),
78
generateMigration: require('./migrations/generate'),
89
runMigration: require('./migrations/run'),

src/tasks/pull-languages.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const fs = require('fs')
2+
const chalk = require('chalk')
3+
4+
/**
5+
* @method pullLanguages
6+
* @param {Object} api
7+
* @param {Object} options { space: Number }
8+
* @return {Promise<Object>}
9+
*/
10+
const pullLanguages = async (api, options) => {
11+
const { space } = options
12+
13+
try {
14+
const options = await api.getSpaceOptions()
15+
const languages = {
16+
default_lang_name: options.default_lang_name,
17+
languages: options.languages
18+
}
19+
20+
const file = `languages.${space}.json`
21+
const data = JSON.stringify(languages, null, 2)
22+
23+
console.log(`${chalk.green('✓')} We've saved your languages in the file: ${file}`)
24+
25+
fs.writeFile(`./${file}`, data, (err) => {
26+
if (err) {
27+
Promise.reject(err)
28+
return
29+
}
30+
31+
Promise.resolve(file)
32+
})
33+
} catch (e) {
34+
console.error(`${chalk.red('X')} An error ocurred in pull-languages task when load components data`)
35+
return Promise.reject(new Error(e))
36+
}
37+
}
38+
39+
module.exports = pullLanguages

src/utils/api.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ module.exports = {
156156
.catch(err => Promise.reject(err))
157157
},
158158

159+
getSpaceOptions () {
160+
const client = this.getClient()
161+
162+
return client
163+
.get(this.getPath(''))
164+
.then((data) => data.data.space.options || {})
165+
.catch((err) => Promise.reject(err))
166+
},
167+
159168
getComponents () {
160169
const client = this.getClient()
161170

tests/constants.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,31 @@ const FAKE_SPACES = () => [
229229
}
230230
]
231231

232+
const FAKE_SPACE_OPTIONS = () => ({
233+
languages: [
234+
{
235+
code: 'pt',
236+
name: 'Português'
237+
},
238+
{
239+
code: 'nl-be',
240+
name: 'Dutch (Belgian)'
241+
}
242+
],
243+
hosted_backup: false,
244+
onboarding_step: '3',
245+
default_lang_name: 'English',
246+
rev_share_enabled: true,
247+
required_assest_fields: [],
248+
use_translated_stories: false
249+
})
250+
232251
module.exports = {
233252
EMAIL_TEST,
234253
TOKEN_TEST,
235254
FAKE_STORIES,
236255
PASSWORD_TEST,
237256
FAKE_COMPONENTS,
238-
FAKE_SPACES
257+
FAKE_SPACES,
258+
FAKE_SPACE_OPTIONS
239259
}

tests/units/pull-languages.spec.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const fs = require('fs')
2+
const pullLanguages = require('../../src/tasks/pull-languages')
3+
const { FAKE_SPACE_OPTIONS } = require('../constants')
4+
5+
jest.mock('fs')
6+
7+
describe('testing pullLanguages', () => {
8+
afterEach(() => {
9+
jest.clearAllMocks()
10+
})
11+
12+
it('api.getSpaceOptions() should be called once time', () => {
13+
const api = {
14+
getSpaceOptions: jest.fn(() => Promise.resolve(FAKE_SPACE_OPTIONS()))
15+
}
16+
17+
return pullLanguages(api, {})
18+
.then(() => {
19+
expect(api.getSpaceOptions.mock.calls.length).toBe(1)
20+
})
21+
})
22+
23+
it('api.getSpaceOptions() should be call fs.writeFile correctly', async () => {
24+
const SPACE = 12345
25+
const BODY = FAKE_SPACE_OPTIONS()
26+
27+
const api = {
28+
getSpaceOptions () {
29+
return Promise.resolve(BODY)
30+
}
31+
}
32+
33+
const options = {
34+
space: SPACE
35+
}
36+
37+
const expectFileName = `languages.${SPACE}.json`
38+
const expectData = {
39+
default_lang_name: BODY.default_lang_name,
40+
languages: BODY.languages
41+
}
42+
43+
return pullLanguages(api, options)
44+
.then(_ => {
45+
const [path, data] = fs.writeFile.mock.calls[0]
46+
47+
expect(fs.writeFile.mock.calls.length).toBe(1)
48+
expect(path).toBe(`./${expectFileName}`)
49+
expect(JSON.parse(data)).toEqual(expectData)
50+
})
51+
})
52+
53+
it('api.getSpaceOptions() when a error ocurred, catch the body response', async () => {
54+
const _api = {
55+
getSpaceOptions (_, fn) {
56+
return Promise.reject(new Error('Failed'))
57+
}
58+
}
59+
60+
await expect(pullLanguages(_api, {})).rejects.toThrow('Error: Failed')
61+
})
62+
})

0 commit comments

Comments
 (0)