UNPKG

3.48 kBJavaScriptView Raw
1require('lazy-ass');
2var check = require('check-more-types');
3var packageRepo = require('./package-repo');
4
5var log = console.log.bind(console);
6var debug = require('debug')('main');
7var _ = require('lodash');
8var utils = require('./utils');
9var Promise = require('bluebird');
10
11function findCommitIds(options, repoInfo) {
12 la(check.object(repoInfo), 'missing repo info', repoInfo);
13
14 var tagsToCommits = require('./get-commits-from-tags');
15 return tagsToCommits({
16 user: repoInfo.user,
17 repo: repoInfo.repo,
18 from: options.from,
19 to: options.to
20 }).tap(debug)
21 .then(function mergeCommitInfo(tagsInfo) {
22 return _.extend({}, repoInfo, options, tagsInfo);
23 });
24}
25
26function findCommentsBetweenTags(options) {
27 la(check.object(options), 'missing options', options);
28 la(check.object(options.fromTag), 'missing fromTag', options);
29 la(check.object(options.toTag), 'missing toTag', options);
30
31 var findCommits = require('./get-comments-between-commits');
32 return findCommits({
33 user: options.user,
34 repo: options.repo,
35 from: options.fromTag.sha,
36 to: options.toTag.sha
37 }).then(function (report) {
38 report.options.name = options.name;
39 report.options.from = options.from;
40 report.options.to = options.to;
41 return report;
42 });
43}
44
45function askGithubUsernameAndPassword() {
46 var inquirer = require('inquirer');
47
48 var username = {
49 type: 'input',
50 name: 'username',
51 message: 'github username'
52 };
53 var password = {
54 type: 'password',
55 name: 'password',
56 message: 'github password (not stored locally)'
57 };
58
59 return new Promise(function (resolve) {
60 inquirer.prompt([username, password], function (answers) {
61 la(check.unemptyString(answers.username), 'missing username');
62 la(check.unemptyString(answers.password), 'missing password');
63 resolve({
64 username: answers.username,
65 password: answers.password
66 });
67 });
68 });
69}
70
71function githubLogin() {
72 return askGithubUsernameAndPassword()
73 .then(function (info) {
74 log('trying to login to github %s', info.username);
75 la(check.unemptyString(info.password), 'empty password for', info.username);
76
77 utils.github.authenticate({
78 type: 'basic',
79 username: info.username,
80 password: info.password
81 });
82 });
83}
84
85
86function changedLogReport(options, reportOptions) {
87 // TODO validate options
88 options = options || {};
89 reportOptions = reportOptions || {};
90
91 var allTags;
92
93 return packageRepo(options.name)
94 .tap(debug)
95 .then(_.partial(findCommitIds, options))
96 .tap(function (result) {
97 la(check.array(result.allTags), 'expected all tags in', result);
98 allTags = result.allTags;
99 })
100 .then(findCommentsBetweenTags)
101 .then(function mergeCommitsAndTags(report) {
102 report.allTags = allTags;
103 return report;
104 })
105 .tap(debug)
106 .tap(function (report) {
107 la(check.object(report), 'missing report', report);
108 la(check.fn(report.print), 'cannot print report', report);
109 report.print(reportOptions);
110 });
111}
112
113function changedLog(options, reportOptions) {
114 // TODO validate options
115 options = options || {};
116 reportOptions = reportOptions || {};
117
118 if (options.auth) {
119 log('Please login to github to increase the API rate limit');
120 return githubLogin()
121 .then(_.partial(changedLogReport, options, reportOptions));
122 }
123
124 return changedLogReport(options, reportOptions);
125}
126
127module.exports = changedLog;