Skip to content

Commit

Permalink
Separate lib and cli
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed Nov 5, 2017
1 parent d033f5c commit 897a430
Show file tree
Hide file tree
Showing 21 changed files with 285 additions and 293 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "backport",
"version": "1.1.1",
"main": "./src/index.js",
"main": "./src/cli/index.js",
"bin": {
"backport": "./src/index.js"
"backport": "./src/cli/index.js"
},
"license": "MIT",
"scripts": {
Expand Down
139 changes: 65 additions & 74 deletions src/cliService.js → src/cli/cliService.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const ora = require('ora');
const path = require('path');
const prompts = require('./prompts');
const { withSpinner } = require('./utils');
const github = require('./github');
const constants = require('./constants');
const { getConfigFilePath, getRepoPath } = require('./env');
const prompts = require('../lib/prompts');
const github = require('../lib/github');
const constants = require('../lib/constants');
const { getRepoPath } = require('../lib/env');

const {
resetAndPullMaster,
Expand All @@ -13,45 +12,42 @@ const {
push,
repoExists,
setupRepo
} = require('./git');
} = require('../lib/git');

const service = {};
service.doBackportVersions = ({
function doBackportVersions({
owner,
repoName,
commit,
reference,
versions,
username,
labels
}) => {
}) {
return sequentially(versions, version => {
return service
.doBackportVersion({
owner,
repoName,
commit,
reference,
version,
username,
labels
})
return doBackportVersion({
owner,
repoName,
commit,
reference,
version,
username,
labels
})
.then(res => console.log(`View pull request: ${res.data.html_url}\n`))
.catch(service.handleErrors);
.catch(handleErrors);
});
};
}

service.doBackportVersion = ({
function doBackportVersion({
owner,
repoName,
commit,
reference,
version,
username,
labels = []
}) => {
}) {
const backportBranchName = getBackportBranchName(version, reference);

console.log(`Backporting ${getReferenceLong(reference)} to ${version}`);

return withSpinner(
Expand Down Expand Up @@ -86,9 +82,9 @@ service.doBackportVersion = ({
'Creating pull request'
);
});
};
}

service.getReference = (owner, repoName, commitSha) => {
function getReference(owner, repoName, commitSha) {
return github
.getPullRequestByCommit(owner, repoName, commitSha)
.then(pullRequest => {
Expand All @@ -98,9 +94,9 @@ service.getReference = (owner, repoName, commitSha) => {

return { type: 'commit', value: commitSha.slice(0, 7) };
});
};
}

service.promptRepoInfo = (repositories, cwd) => {
function promptRepoInfo(repositories, cwd) {
return Promise.resolve()
.then(() => {
const fullRepoNames = repositories.map(repo => repo.name);
Expand All @@ -115,9 +111,9 @@ service.promptRepoInfo = (repositories, cwd) => {
const [owner, repoName] = fullRepoName.split('/');
return { owner, repoName };
});
};
}

service.maybeSetupRepo = (owner, repoName, username) => {
function maybeSetupRepo(owner, repoName, username) {
return repoExists(owner, repoName).then(exists => {
if (!exists) {
return withSpinner(
Expand All @@ -126,9 +122,9 @@ service.maybeSetupRepo = (owner, repoName, username) => {
);
}
});
};
}

service.promptCommit = (owner, repoName, username) => {
function promptCommit(owner, repoName, username) {
const spinner = ora('Loading commits...').start();
return github
.getCommits(owner, repoName, username)
Expand All @@ -140,62 +136,33 @@ service.promptCommit = (owner, repoName, username) => {
spinner.stop();
return prompts.listCommits(commits);
});
};
}

service.promptVersions = (versions, multipleChoice = false) => {
function promptVersions(versions, multipleChoice = false) {
return multipleChoice
? prompts.checkboxVersions(versions)
: prompts.listVersions(versions);
};
}

service.handleErrors = e => {
switch (e.message) {
function handleErrors(e) {
switch (e.code) {
case constants.INVALID_CONFIG:
switch (e.details) {
case 'username_and_access_token':
console.log(
`Welcome to the Backport tool. Please add your Github username, and a Github access token to the config: ${getConfigFilePath()}`
);
break;

case 'username':
console.log(
`Please add your username to the config: ${getConfigFilePath()}`
);
break;

case 'access_token':
console.log(
`Please add a Github access token to the config: ${getConfigFilePath()}`
);
break;

case 'repositories':
console.log(
`You must add at least 1 repository: ${getConfigFilePath()}`
);
break;

default:
console.log(
`There seems to be an issue with your config file. Please fix it: ${getConfigFilePath()}`
);
}

console.log(e.message);
break;

case constants.GITHUB_ERROR:
console.error(JSON.stringify(e.details, null, 4));
break;

case constants.CHERRYPICK_CONFLICT_NOT_HANDLED:
console.error('Merge conflict was not resolved', e.details);
console.error('Merge conflict was not resolved', e.message);
break;

default:
console.log(e.message);
console.error(e);
}
};
}

function sequentially(items, handler) {
return items.reduce(
Expand Down Expand Up @@ -239,9 +206,8 @@ function cherrypickAndPrompt(owner, repoName, sha) {

return prompts.confirmConflictResolved().then(isConflictResolved => {
if (!isConflictResolved) {
const error = new Error(constants.CHERRYPICK_CONFLICT_NOT_HANDLED);
error.details = e.message;
throw error;
e.code = constants.CHERRYPICK_CONFLICT_NOT_HANDLED;
throw e;
}
});
});
Expand Down Expand Up @@ -269,4 +235,29 @@ function getPullRequestPayload(commitMessage, version, reference, username) {
};
}

module.exports = service;
function withSpinner(promise, text, errorText) {
const spinner = ora(text).start();
return promise
.then(res => {
spinner.succeed();
return res;
})
.catch(e => {
if (errorText) {
spinner.text = errorText;
}
spinner.fail();
throw e;
});
}

module.exports = {
doBackportVersions,
handleErrors,
doBackportVersion,
getReference,
promptRepoInfo,
maybeSetupRepo,
promptCommit,
promptVersions
};
18 changes: 4 additions & 14 deletions src/index.js → src/cli/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
#!/usr/bin/env node

const yargs = require('yargs');
const { init } = require('./cli');
const { getConfig } = require('./configs');
const { CONFIG_FILE_PERMISSION_ERROR } = require('./constants');

let config;
try {
config = getConfig();
} catch (error) {
if (error.code === CONFIG_FILE_PERMISSION_ERROR) {
console.log(error.message);
process.exit(1);
}
}
const initSteps = require('./steps');
const { getConfig } = require('../lib/configs');

const config = getConfig();
const isBool = value => typeof value === 'boolean';
const args = yargs
.usage('$0 [args]')
Expand Down Expand Up @@ -43,4 +33,4 @@ if (args.config) {

const options = Object.assign({}, args, { cwd: process.cwd() });

init(config, options);
initSteps(config, options);
10 changes: 4 additions & 6 deletions src/cli.js → src/cli/steps.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const github = require('./github');
const github = require('../lib/github');
const {
ensureConfigAndFoldersExists,
validateConfig,
getRepoConfig
} = require('./configs');
} = require('../lib/configs');
const {
promptRepoInfo,
promptCommit,
Expand All @@ -14,7 +14,7 @@ const {
maybeSetupRepo
} = require('./cliService');

function init(config, options) {
function initSteps(config, options) {
let commit, versions, reference, owner, repoName, repoConfig;

return ensureConfigAndFoldersExists()
Expand Down Expand Up @@ -49,6 +49,4 @@ function init(config, options) {
.catch(handleErrors);
}

module.exports = {
init
};
module.exports = initSteps;
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,25 @@ Array [
Array [
"git reset --hard && git checkout master && git pull origin master",
Object {
"cwd": "homefolder/.backport/repositories/elastic/kibana",
"cwd": "/homefolder/.backport/repositories/elastic/kibana",
},
],
Array [
"git fetch origin 6.x && git branch backport/6.x/commit-myCommitSha origin/6.x --force && git checkout backport/6.x/commit-myCommitSha ",
Object {
"cwd": "homefolder/.backport/repositories/elastic/kibana",
"cwd": "/homefolder/.backport/repositories/elastic/kibana",
},
],
Array [
"git cherry-pick undefined",
Object {
"cwd": "homefolder/.backport/repositories/elastic/kibana",
"cwd": "/homefolder/.backport/repositories/elastic/kibana",
},
],
Array [
"git push sqren backport/6.x/commit-myCommitSha --force",
Object {
"cwd": "homefolder/.backport/repositories/elastic/kibana",
"cwd": "/homefolder/.backport/repositories/elastic/kibana",
},
],
]
Expand Down Expand Up @@ -88,25 +88,25 @@ Array [
Array [
"git reset --hard && git checkout master && git pull origin master",
Object {
"cwd": "homefolder/.backport/repositories/elastic/kibana",
"cwd": "/homefolder/.backport/repositories/elastic/kibana",
},
],
Array [
"git fetch origin 6.x && git branch backport/6.x/pr-myPullRequest origin/6.x --force && git checkout backport/6.x/pr-myPullRequest ",
Object {
"cwd": "homefolder/.backport/repositories/elastic/kibana",
"cwd": "/homefolder/.backport/repositories/elastic/kibana",
},
],
Array [
"git cherry-pick undefined",
Object {
"cwd": "homefolder/.backport/repositories/elastic/kibana",
"cwd": "/homefolder/.backport/repositories/elastic/kibana",
},
],
Array [
"git push sqren backport/6.x/pr-myPullRequest --force",
Object {
"cwd": "homefolder/.backport/repositories/elastic/kibana",
"cwd": "/homefolder/.backport/repositories/elastic/kibana",
},
],
]
Expand Down
Loading

0 comments on commit 897a430

Please sign in to comment.