-
Notifications
You must be signed in to change notification settings - Fork 7
/
router.js
95 lines (88 loc) · 3.6 KB
/
router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'use strict';
const _ = require('lodash');
const inquirer = require('inquirer');
const path = require('path');
const Promise = require('bluebird');
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 createRepo = require('./lib/createRepo');
const openInBrowser = require('./lib/openInBrowser');
const status = require('./lib/status');
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['create']) {
return createRepo(searchTerm);
}
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['status-all']) {
return mapToHandler({
results,
confirmMessage: 'Are you sure you want check status on all of the above?',
errorMessage: 'Status all aborted',
handler: status
});
} 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);
}
resultPromise = resultPromise.then(maybeClone);
return resultPromise;
});
}
}
}