-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #806 from plckr/master
feature: add pull-languages command
- Loading branch information
Showing
7 changed files
with
169 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |