Skip to content

Commit

Permalink
clean up of routing pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
machellerogden committed Mar 15, 2017
1 parent 67f7656 commit 2d9cfc3
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 178 deletions.
132 changes: 0 additions & 132 deletions cli.js

This file was deleted.

4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
/* istanbul ignore next */
(function() {

const cli = require('./cli');
const router = require('./router');
const argv = require('./argv');
const setTarget = require('./lib/setTarget');

cli(argv)
router(argv)
.then(setTarget)
.catch((err) => {
console.log(err.message);
Expand Down
31 changes: 31 additions & 0 deletions lib/getSearchTerm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';
const _ = require('lodash');
const path = require('path');

module.exports = getSearchTerm;

function getSearchTerm(options) {
let searchTerm;
let defaultToCurrent = _.pick(options, [
'open',
'edit'
]);
let defaultToWild = _.pick(options, [
'completions',
'clone-all',
'force-latest',
'pull-all'
]);
if (options._ && options._[0]) {
searchTerm = options._[0];
} else if (_.keys(defaultToCurrent).length) {
searchTerm = _.reduce(_.values(defaultToCurrent), (acc, value) => {
return _.isString(value) ? value : acc;
}, '^' + _.takeRight(process.cwd().split(path.sep), 3).join(path.sep) + '$');
} else if (_.keys(defaultToWild).length) {
searchTerm = _.reduce(_.values(defaultToWild), (acc, value) => {
return _.isString(value) ? value : acc;
}, '.*');
}
return searchTerm;
}
15 changes: 0 additions & 15 deletions lib/index.js

This file was deleted.

32 changes: 32 additions & 0 deletions lib/mapToHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';
const _ = require('lodash');
const Promise = require('bluebird');
const getSourceResult = require('./getSourceResult');
const output = require('./output');
const inquirer = require('inquirer');
const workspaceDir = require('./workspaceDir');

module.exports = mapToHandler;

function mapToHandler(options) {
let { results, confirmMessage, errorMessage, handler } = options;
return Promise.all(_.map(results, getSourceResult)).then((repos) => {
_.each(repos, (r) => output.log(r.name));
return inquirer.prompt([
{
type: 'confirm',
name: 'confirm',
message: confirmMessage,
default: false
}
]).then((answers) => {
if (answers.confirm) {
return Promise.resolve(repos).mapSeries(handler).then(() => {
return { repoDir: workspaceDir };
});
} else {
throw new Error(errorMessage);
}
});
});
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"changelog": "./node_modules/.bin/github-changes -o carsdotcom -r bitcar",
"generate-and-stage-changelog": "npm run changelog && git add -A",
"coverage": "rm -rf coverage && node node_modules/.bin/istanbul --include-all-sources cover -x \"test/**/*.spec.js\" --dir=\"coverage\" --root=\"./\" node_modules/.bin/_mocha -- \"test/**/*.spec.js\"",
"check-coverage": "npm run coverage && ./node_modules/.bin/istanbul check-coverage --root=\"./coverage\" --statement 65 --function 68 --branch 50"
"check-coverage": "npm run coverage && ./node_modules/.bin/istanbul check-coverage --root=\"./coverage\" --statement 60 --function 60 --branch 50"
},
"contributors": [
{
Expand Down
92 changes: 92 additions & 0 deletions router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
'use strict';
const _ = require('lodash');
const inquirer = require('inquirer');
const path = require('path');
const Promise = require('bluebird');
const workspaceDir = require('./lib/workspaceDir');
const output = require('./lib/output');
const getSearchTerm = require('./lib/getSearchTerm');
const mapToHandler = require('./lib/mapToHandler');
const syncExisting = require('./lib/syncExisting');
const getSourceData = require('./lib/getSourceData');
const getSourceResult = require('./lib/getSourceResult');
const getPaths = require('./lib/getPaths');
const maybeClone = require('./lib/maybeClone');
const maybePull = require('./lib/maybePull');
const forceLatest = require('./lib/forceLatest');
const openInBrowser = require('./lib/openInBrowser');

module.exports = router;

function router(options) {
let searchTerm = getSearchTerm(options);

if (options.version) {
output.log(require('./package.json').version);
return Promise.resolve();
}

if (options.setup) {
return require('./setup')();
}

if (options['sync-existing']) {
return syncExisting();
}

if (options.refresh) {
return getSourceData(true);
} else {
const sourceDataPromise = getSourceData();
const pathsPromise = sourceDataPromise.then(getPaths).filter((v) => (new RegExp(searchTerm)).test(v));
if (options.completions) {
return pathsPromise.map((result) => _.tail(result.split(path.sep)).join(path.sep))
.each(_.ary(output.log, 1));
} else {
return pathsPromise.then((results) => {
let resultPromise;
if (results.length && options['clone-all']) {
return mapToHandler({
results,
confirmMessage: 'Are you sure you want clone all of the above?',
errorMessage: 'Clone all aborted',
handler: maybeClone
});
} else if (results.length && options['pull-all']) {
return mapToHandler({
results,
confirmMessage: 'Are you sure you want pull all of the above?',
errorMessage: 'Pull all aborted',
handler: (r) => maybeClone(r).then(maybePull)
});
} else if (results.length && options['force-latest']) {
return mapToHandler({
results,
confirmMessage: 'Are you sure you want clean and force a hard reset to all of the above?',
errorMessage: 'Force latest aborted',
handler: forceLatest
});
} else if (results.length > 1) {
resultPromise = inquirer.prompt([
{
type: 'list',
name: 'result',
message: 'search results',
choices: results
}
]).then((answers) => getSourceResult(answers.result));
} else if (results.length) {
resultPromise = getSourceResult(results[0]);
} else {
throw new Error('No results.');
}
if (options.open) {
resultPromise = resultPromise.then(openInBrowser).then(maybeClone);
} else {
resultPromise = resultPromise.then(maybeClone);
}
return resultPromise;
});
}
}
}
Loading

0 comments on commit 2d9cfc3

Please sign in to comment.