Skip to content

Commit

Permalink
Add Husky and simplify prompt (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv authored Nov 5, 2017
1 parent 2612f78 commit d033f5c
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 69 deletions.
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
"license": "MIT",
"scripts": {
"test": "jest",
"lint": "eslint ./src"
"testWithCoverage": "jest --coverage",
"format": "prettier \"./src/**/*.js\" --write",
"lint": "eslint ./src",
"prepush": "npm run lint",
"precommit": "lint-staged"
},
"lint-staged": {
"*.js": ["prettier --write", "git add"]
},
"repository": {
"type": "git",
Expand All @@ -30,7 +37,9 @@
"eslint-config-prettier": "^2.4.0",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-prettier": "^2.2.0",
"husky": "^0.14.3",
"jest": "^21.2.1",
"lint-staged": "^4.3.0",
"prettier": "^1.7.4"
}
}
94 changes: 37 additions & 57 deletions src/prompts.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,56 @@
const inquirer = require('inquirer');

function listFullRepoName(repoNames) {
function prompt(options) {
return inquirer
.prompt([
{
type: 'list',
name: 'fullRepoName',
message: 'Select repository',
choices: repoNames
}
])
.then(({ fullRepoName }) => fullRepoName);
.prompt([Object.assign({}, options, { name: 'promptResult' })])
.then(({ promptResult }) => promptResult);
}

function listFullRepoName(repoNames) {
return prompt({
type: 'list',
message: 'Select repository',
choices: repoNames
});
}

function listCommits(commits) {
const pageSize = Math.min(10, commits.length);

return inquirer
.prompt([
{
pageSize,
type: 'list',
name: 'commit',
message: 'Select commit to backport',
choices: commits
.map(commit => ({
name: commit.message,
value: commit,
short: commit.message
}))
.concat(commits.length > pageSize ? new inquirer.Separator() : [])
}
])
.then(({ commit }) => commit);
return prompt({
pageSize,
type: 'list',
message: 'Select commit to backport',
choices: commits
.map(commit => ({
name: commit.message,
value: commit,
short: commit.message
}))
.concat(commits.length > pageSize ? new inquirer.Separator() : [])
});
}

function listVersions(versions) {
return inquirer
.prompt([
{
type: 'list',
name: 'version',
message: 'Select version to backport to',
choices: versions
}
])
.then(({ version }) => [version]);
return prompt({
type: 'list',
message: 'Select version to backport to',
choices: versions
}).then(version => [version]);
}

function checkboxVersions(versions) {
return inquirer
.prompt([
{
type: 'checkbox',
name: 'versions',
message: 'Select version to backport to',
choices: versions
}
])
.then(({ versions }) => versions);
return prompt({
type: 'checkbox',
message: 'Select version to backport to',
choices: versions
});
}

function confirmConflictResolved() {
return inquirer
.prompt([
{
type: 'confirm',
name: 'isConflictResolved',
message: 'Have you solved the merge conflict?'
}
])
.then(({ isConflictResolved }) => isConflictResolved);
return prompt({
type: 'confirm',
message: 'Have you solved the merge conflict?'
});
}

module.exports = {
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/cli.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Array [
"elastic/backport-cli-test",
],
"message": "Select repository",
"name": "fullRepoName",
"name": "promptResult",
"type": "list",
},
],
Expand Down Expand Up @@ -88,7 +88,7 @@ Array [
},
],
"message": "Select commit to backport",
"name": "commit",
"name": "promptResult",
"pageSize": 3,
"type": "list",
},
Expand All @@ -105,7 +105,7 @@ Array [
"5.4",
],
"message": "Select version to backport to",
"name": "version",
"name": "promptResult",
"type": "list",
},
],
Expand Down
6 changes: 3 additions & 3 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ function setup() {

inquirer.prompt = jest
.fn()
.mockReturnValueOnce(Promise.resolve({ fullRepoName }))
.mockReturnValueOnce(Promise.resolve({ promptResult: fullRepoName }))
.mockReturnValueOnce(
Promise.resolve({
commit: {
promptResult: {
message: 'myCommitMessage',
sha: 'mySha'
}
})
)
.mockReturnValueOnce(
Promise.resolve({
version: '6.2'
promptResult: '6.2'
})
);

Expand Down
2 changes: 1 addition & 1 deletion test/cliService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe('promptRepoInfo', () => {
function mockPrompt() {
inquirer.prompt = jest
.fn()
.mockReturnValueOnce(Promise.resolve({ fullRepoName: 'elastic/kibana' }));
.mockReturnValueOnce(Promise.resolve({ promptResult: 'elastic/kibana' }));
}

describe('without matching cwd', () => {
Expand Down
Loading

0 comments on commit d033f5c

Please sign in to comment.