diff --git a/lib/actions/create-issue.js b/lib/actions/create-issue.js index 2b16036..176a2f7 100644 --- a/lib/actions/create-issue.js +++ b/lib/actions/create-issue.js @@ -1,16 +1,15 @@ module.exports = { description: 'Create new issue', props: [], - run () { + async run () { const IssuesDAO = require('../dao/issues-dao'); const ProjectsInquirer = require('../inquirer/projects-inquirer'); const IssuesInquirer = require('../inquirer/issues-inquirer'); const PrintUtils = require('../utils/print-utils'); - return ProjectsInquirer - .chooseProject() - .then(IssuesInquirer.getIssueParameters) - .then(IssuesDAO.createIssue) - .then(({ key }) => PrintUtils.printLine(`Issue '${key}' successfully created`, 'green')); + const projectId = await ProjectsInquirer.chooseProject(); + const parameters = await IssuesInquirer.getIssueParameters(projectId); + const issue = await IssuesDAO.createIssue(parameters); + PrintUtils.printLine(`Issue '${issue.key}' successfully created`, 'green') } }; diff --git a/lib/actions/issue-status.js b/lib/actions/issue-status.js index 5c01fc6..e0f0cb3 100644 --- a/lib/actions/issue-status.js +++ b/lib/actions/issue-status.js @@ -1,12 +1,11 @@ module.exports = { description: 'Show issue status', props: ['issueName'], - run (issueName) { + async run (issueName) { const IssuesDAO = require('../dao/issues-dao'); const IssuesPrinter = require('../printer/issues-printer'); - return IssuesDAO - .findIssueByName(issueName) - .then(IssuesPrinter.printIssueStatus); + const issue = await IssuesDAO.findIssueByName(issueName); + IssuesPrinter.printIssueStatus(issue); } }; diff --git a/lib/actions/issue.js b/lib/actions/issue.js index cae5335..d800be9 100644 --- a/lib/actions/issue.js +++ b/lib/actions/issue.js @@ -1,12 +1,11 @@ module.exports = { description: 'Show issue details', props: ['issueName'], - run (issueName) { + async run (issueName) { const IssuesDAO = require('../dao/issues-dao'); const IssuesPrinter = require('../printer/issues-printer'); - return IssuesDAO - .findIssueByName(issueName) - .then(IssuesPrinter.printIssueDetails); + const issue = await IssuesDAO.findIssueByName(issueName); + IssuesPrinter.printIssueDetails(issue); } }; diff --git a/lib/actions/jql.js b/lib/actions/jql.js index 6413d32..367649f 100644 --- a/lib/actions/jql.js +++ b/lib/actions/jql.js @@ -1,12 +1,11 @@ module.exports = { description: 'Show issues using JQL', props: ['jql'], - run (jql) { + async run (jql) { const IssuesDAO = require('../dao/issues-dao'); const IssuesPrinter = require('../printer/issues-printer'); - return IssuesDAO - .fetchIssuesUsingJQL(jql) - .then(IssuesPrinter.printIssues) + const issues = await IssuesDAO.fetchIssuesUsingJQL(jql); + IssuesPrinter.printIssues(issues) } }; diff --git a/lib/dao/issue-types-dao.js b/lib/dao/issue-types-dao.js index b1d7e7a..862b2ae 100644 --- a/lib/dao/issue-types-dao.js +++ b/lib/dao/issue-types-dao.js @@ -2,10 +2,11 @@ const DAOUtils = require('../utils/dao-utils'); const IssueTypesMapper = require('../mappers/issue-types-mapper'); module.exports = { - getIssueTypesExcludingSubtasks (projectId) { - return DAOUtils - .fetch(`/project/${projectId}`) - .then(({ issueTypes }) => issueTypes.filter(issue => !issue.subtask)) - .then(IssueTypesMapper.mapIssueTypes); + async getIssueTypesExcludingSubtasks (projectId) { + const { issueTypes } = await DAOUtils.fetch(`/project/${projectId}`); + + return issueTypes + .filter(issue => !issue.subtask) + .map(IssueTypesMapper.mapIssueType) } }; diff --git a/lib/dao/issues-dao.js b/lib/dao/issues-dao.js index ae10627..383be4e 100644 --- a/lib/dao/issues-dao.js +++ b/lib/dao/issues-dao.js @@ -2,13 +2,12 @@ const DAOUtils = require('../utils/dao-utils'); const IssuesMapper = require('../mappers/issues-mapper'); module.exports = { - findIssueByName (issueName) { - return DAOUtils - .fetch(`/issue/${issueName}`) - .then(IssuesMapper.mapIssueDetails) + async findIssueByName (issueName) { + const issue = await DAOUtils.fetch(`/issue/${issueName}`); + return IssuesMapper.mapIssueDetails(issue); }, - createIssue ({ summary, description, issueTypeId, projectId }) { + async createIssue ({ summary, description, issueTypeId, projectId }) { return DAOUtils.post('/issue', { summary, description, @@ -17,9 +16,8 @@ module.exports = { }) }, - fetchIssuesUsingJQL (jql) { - return DAOUtils - .fetch(`/search?jql=${jql}`) - .then(IssuesMapper.mapIssues); + async fetchIssuesUsingJQL (jql) { + const { issues } = await DAOUtils.fetch(`/search?jql=${jql}`); + return issues.map(IssuesMapper.mapIssue); } }; diff --git a/lib/dao/projects-dao.js b/lib/dao/projects-dao.js index 493cd7f..bccc26f 100644 --- a/lib/dao/projects-dao.js +++ b/lib/dao/projects-dao.js @@ -2,13 +2,12 @@ const DAOUtils = require('../utils/dao-utils'); const ProjectsMapper = require('../mappers/projects-mapper'); module.exports = { - getProjects () { - return DAOUtils - .fetch(`/project`) - .then(ProjectsMapper.mapProjects); + async getProjects () { + const projects = await DAOUtils.fetch(`/project`); + return projects.map(ProjectsMapper.mapProject); }, - getProject (projectId) { + async getProject (projectId) { return DAOUtils.fetch(`/project/${projectId}`); } }; diff --git a/lib/inquirer/issues-inquirer.js b/lib/inquirer/issues-inquirer.js index 214abf4..35d8e21 100644 --- a/lib/inquirer/issues-inquirer.js +++ b/lib/inquirer/issues-inquirer.js @@ -2,24 +2,25 @@ const prompt = require('inquirer').prompt; const IssueTypesDAO = require('../dao/issue-types-dao'); module.exports = { - getIssueParameters (projectId) { - return IssueTypesDAO.getIssueTypesExcludingSubtasks(projectId) - .then(issueTypes => prompt([ - { - type: 'list', - name: 'issueTypeId', - message: 'Type: ', - choices: issueTypes.map(issueType => ({ value: issueType.id, name: issueType.name })) - },{ - type: 'input', - name: 'summary', - message: 'Name: ' - },{ - type: 'input', - name: 'description', - message: 'Description: ' - } - ])) - .then(answers => Object.assign(answers, { projectId })); + async getIssueParameters (projectId) { + const issueTypes = await IssueTypesDAO.getIssueTypesExcludingSubtasks(projectId); + const answers = await prompt([ + { + type: 'list', + name: 'issueTypeId', + message: 'Type: ', + choices: issueTypes.map(issueType => ({ value: issueType.id, name: issueType.name })) + },{ + type: 'input', + name: 'summary', + message: 'Name: ' + },{ + type: 'input', + name: 'description', + message: 'Description: ' + } + ]); + + return Object.assign(answers, { projectId }); } }; diff --git a/lib/inquirer/projects-inquirer.js b/lib/inquirer/projects-inquirer.js index 89cdd11..4c64b64 100644 --- a/lib/inquirer/projects-inquirer.js +++ b/lib/inquirer/projects-inquirer.js @@ -2,14 +2,16 @@ const prompt = require('inquirer').prompt; const ProjectsDAO = require('../dao/projects-dao'); module.exports = { - chooseProject () { - return ProjectsDAO.getProjects() - .then(projects => prompt({ - type: 'list', - name: 'projectId', - message: 'Choose project: ', - choices: projects.map(project => ({ value: project.id, name: project.name })) - })) - .then(({ projectId }) => projectId) + async chooseProject () { + const projects = await ProjectsDAO.getProjects(); + + const { projectId } = await prompt({ + type: 'list', + name: 'projectId', + message: 'Choose project: ', + choices: projects.map(project => ({ value: project.id, name: project.name })) + }); + + return projectId; } }; diff --git a/lib/mappers/issue-types-mapper.js b/lib/mappers/issue-types-mapper.js index 83db482..c8cc9db 100644 --- a/lib/mappers/issue-types-mapper.js +++ b/lib/mappers/issue-types-mapper.js @@ -6,10 +6,6 @@ const IssueTypeMapper = { }; return issueType; - }, - - mapIssueTypes (issueTypes) { - return issueTypes.map(IssueTypeMapper.mapIssueType); } }; diff --git a/lib/mappers/issues-mapper.js b/lib/mappers/issues-mapper.js index 93a006b..d712e8c 100644 --- a/lib/mappers/issues-mapper.js +++ b/lib/mappers/issues-mapper.js @@ -24,10 +24,6 @@ const IssueMapper = { issue.commentsCount = serverIssue.fields.comment.total; issue.comments = (serverIssue.fields.comment.comments || []).map(CommentsMapper.mapComment); return issue; - }, - - mapIssues ({ issues }) { - return issues.map(IssueMapper.mapIssue); } }; diff --git a/lib/utils/configuration-utils.js b/lib/utils/configuration-utils.js index 96d9e56..4352555 100644 --- a/lib/utils/configuration-utils.js +++ b/lib/utils/configuration-utils.js @@ -14,10 +14,6 @@ function saveConfiguration ({ host, username, password }) { }) } -function printWhereConfigurationStored () { - PrintUtils.printLine(`Configuration JSON stored in '${CONFIGURATION_FILE_PATH}' file`, 'yellow') -} - const ConfigurationUtils = { CONFIGURATION_FILE_PATH, @@ -30,11 +26,11 @@ const ConfigurationUtils = { PrintUtils.printNewLine(); }, - setupConfigurationFile () { - return ConfigurationInquirer.getConfigurationParameters() - .then(saveConfiguration) - .then(ConfigurationUtils.printConfigurationFile) - .then(printWhereConfigurationStored) + async setupConfigurationFile () { + const parameters = await ConfigurationInquirer.getConfigurationParameters(); + saveConfiguration(parameters); + ConfigurationUtils.printConfigurationFile(); + PrintUtils.printLine(`Configuration JSON stored in '${CONFIGURATION_FILE_PATH}' file`, 'yellow') } }; diff --git a/lib/utils/dao-utils.js b/lib/utils/dao-utils.js index 589a00c..707a81e 100644 --- a/lib/utils/dao-utils.js +++ b/lib/utils/dao-utils.js @@ -14,18 +14,28 @@ function readDataFromResponse (response) { } const DAOUtils = { - fetch (url) { - return require('axios') - .get(buildUrl(url), buildRequestParams()) - .then(readDataFromResponse) - .catch(require('./error-handle-utils').handleResponseError); + async fetch (url) { + const axios = require('axios'); + const ErrorHandleUtils = require('./error-handle-utils'); + + try { + const response = await axios.get(buildUrl(url), buildRequestParams()); + return readDataFromResponse(response); + } catch (error) { + ErrorHandleUtils.handleResponseError(error); + } }, - post (url, fields) { - return require('axios') - .post(buildUrl(url), { fields }, buildRequestParams()) - .then(readDataFromResponse) - .catch(require('./error-handle-utils').handleResponseError); + async post (url, fields) { + const axios = require('axios'); + const ErrorHandleUtils = require('./error-handle-utils'); + + try { + const response = await axios.post(buildUrl(url), { fields }, buildRequestParams()); + return readDataFromResponse(response); + } catch (error) { + ErrorHandleUtils.handleResponseError(error); + } } };