From 9797cb1211d7a82c19b6cc5e0c959b3afbb44591 Mon Sep 17 00:00:00 2001 From: Ricardo Reis Date: Thu, 2 Jun 2022 12:07:20 +0100 Subject: [PATCH 1/3] feat: add pull languages --- src/cli.js | 25 ++++++++++++++++++++++++ src/tasks/index.js | 1 + src/tasks/pull-languages.js | 39 +++++++++++++++++++++++++++++++++++++ src/utils/api.js | 9 +++++++++ 4 files changed, 74 insertions(+) create mode 100644 src/tasks/pull-languages.js diff --git a/src/cli.js b/src/cli.js index b6bafdd..ada70a8 100755 --- a/src/cli.js +++ b/src/cli.js @@ -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('pull-components') diff --git a/src/tasks/index.js b/src/tasks/index.js index 5058f0e..102e365 100644 --- a/src/tasks/index.js +++ b/src/tasks/index.js @@ -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'), diff --git a/src/tasks/pull-languages.js b/src/tasks/pull-languages.js new file mode 100644 index 0000000..7dead35 --- /dev/null +++ b/src/tasks/pull-languages.js @@ -0,0 +1,39 @@ +const fs = require('fs') +const chalk = require('chalk') + +/** + * @method pullLanguages + * @param {Object} api + * @param {Object} options { space: Number } + * @return {Promise} + */ +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 diff --git a/src/utils/api.js b/src/utils/api.js index 07e1158..5859eeb 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -147,6 +147,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() From 3cbfdbfb7e52ddea885012a64c98ebc6330b59ec Mon Sep 17 00:00:00 2001 From: Ricardo Reis Date: Thu, 2 Jun 2022 12:32:51 +0100 Subject: [PATCH 2/3] test(pull-languages): add tests --- tests/constants.js | 22 ++++++++++- tests/units/pull-languages.spec.js | 62 ++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 tests/units/pull-languages.spec.js diff --git a/tests/constants.js b/tests/constants.js index a9d42cd..38666b0 100644 --- a/tests/constants.js +++ b/tests/constants.js @@ -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 } diff --git a/tests/units/pull-languages.spec.js b/tests/units/pull-languages.spec.js new file mode 100644 index 0000000..6cf44f8 --- /dev/null +++ b/tests/units/pull-languages.spec.js @@ -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') + }) +}) From 88fd1a411751aff0d7a47769d6f7d422eb796e15 Mon Sep 17 00:00:00 2001 From: Ricardo Reis Date: Thu, 2 Jun 2022 18:28:37 +0100 Subject: [PATCH 3/3] =?UTF-8?q?docs(pull-languages):=20=F0=9F=93=9D=20add?= =?UTF-8?q?=20documentation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index bdba82c..c0fa1bd 100644 --- a/README.md +++ b/README.md @@ -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 +``` + +#### 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.