Skip to content

Commit

Permalink
Merge pull request #806 from plckr/master
Browse files Browse the repository at this point in the history
feature: add pull-languages command
  • Loading branch information
Thiago Saife authored Nov 30, 2022
2 parents bd16770 + 88fd1a4 commit 86b6a9a
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ Usage to kickstart a boilerplate, fieldtype or theme
$ storyblok select
```

### pull-languages

Download your space's languages schema as json. This command will download 1 file.

```sh
$ storyblok pull-languages --space <SPACE_ID>
```

#### Options

* `space`: your space id

### pull-components

Download your space's components schema as json. This command will download 2 files: 1 for the components and 1 for the presets.
Expand Down
25 changes: 25 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,31 @@ program
}
})

// pull-languages
program
.command('pull-languages')
.description("Download your space's languages schema as json")
.action(async () => {
console.log(`${chalk.blue('-')} Executing pull-languages task`)
const space = program.space
if (!space) {
console.log(chalk.red('X') + ' Please provide the space as argument --space YOUR_SPACE_ID.')
process.exit(0)
}

try {
if (!api.isAuthorized()) {
await api.processLogin()
}

api.setSpaceId(space)
await tasks.pullLanguages(api, { space })
} catch (e) {
console.log(chalk.red('X') + ' An error occurred when executing the pull-languages task: ' + e.message)
process.exit(1)
}
})

// pull-components
program
.command(COMMANDS.PULL_COMPONENTS)
Expand Down
1 change: 1 addition & 0 deletions src/tasks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
scaffold: require('./scaffold'),
quickstart: require('./quickstart'),
pullComponents: require('./pull-components'),
pullLanguages: require('./pull-languages'),
pushComponents: require('./push-components'),
generateMigration: require('./migrations/generate'),
runMigration: require('./migrations/run'),
Expand Down
39 changes: 39 additions & 0 deletions src/tasks/pull-languages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const fs = require('fs')
const chalk = require('chalk')

/**
* @method pullLanguages
* @param {Object} api
* @param {Object} options { space: Number }
* @return {Promise<Object>}
*/
const pullLanguages = async (api, options) => {
const { space } = options

try {
const options = await api.getSpaceOptions()
const languages = {
default_lang_name: options.default_lang_name,
languages: options.languages
}

const file = `languages.${space}.json`
const data = JSON.stringify(languages, null, 2)

console.log(`${chalk.green('✓')} We've saved your languages in the file: ${file}`)

fs.writeFile(`./${file}`, data, (err) => {
if (err) {
Promise.reject(err)
return
}

Promise.resolve(file)
})
} catch (e) {
console.error(`${chalk.red('X')} An error ocurred in pull-languages task when load components data`)
return Promise.reject(new Error(e))
}
}

module.exports = pullLanguages
9 changes: 9 additions & 0 deletions src/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ module.exports = {
.catch(err => Promise.reject(err))
},

getSpaceOptions () {
const client = this.getClient()

return client
.get(this.getPath(''))
.then((data) => data.data.space.options || {})
.catch((err) => Promise.reject(err))
},

getComponents () {
const client = this.getClient()

Expand Down
22 changes: 21 additions & 1 deletion tests/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,31 @@ const FAKE_SPACES = () => [
}
]

const FAKE_SPACE_OPTIONS = () => ({
languages: [
{
code: 'pt',
name: 'Português'
},
{
code: 'nl-be',
name: 'Dutch (Belgian)'
}
],
hosted_backup: false,
onboarding_step: '3',
default_lang_name: 'English',
rev_share_enabled: true,
required_assest_fields: [],
use_translated_stories: false
})

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

jest.mock('fs')

describe('testing pullLanguages', () => {
afterEach(() => {
jest.clearAllMocks()
})

it('api.getSpaceOptions() should be called once time', () => {
const api = {
getSpaceOptions: jest.fn(() => Promise.resolve(FAKE_SPACE_OPTIONS()))
}

return pullLanguages(api, {})
.then(() => {
expect(api.getSpaceOptions.mock.calls.length).toBe(1)
})
})

it('api.getSpaceOptions() should be call fs.writeFile correctly', async () => {
const SPACE = 12345
const BODY = FAKE_SPACE_OPTIONS()

const api = {
getSpaceOptions () {
return Promise.resolve(BODY)
}
}

const options = {
space: SPACE
}

const expectFileName = `languages.${SPACE}.json`
const expectData = {
default_lang_name: BODY.default_lang_name,
languages: BODY.languages
}

return pullLanguages(api, options)
.then(_ => {
const [path, data] = fs.writeFile.mock.calls[0]

expect(fs.writeFile.mock.calls.length).toBe(1)
expect(path).toBe(`./${expectFileName}`)
expect(JSON.parse(data)).toEqual(expectData)
})
})

it('api.getSpaceOptions() when a error ocurred, catch the body response', async () => {
const _api = {
getSpaceOptions (_, fn) {
return Promise.reject(new Error('Failed'))
}
}

await expect(pullLanguages(_api, {})).rejects.toThrow('Error: Failed')
})
})

0 comments on commit 86b6a9a

Please sign in to comment.