UNPKG

2.29 kBJavaScriptView Raw
1require('lazy-ass');
2var check = require('check-more-types');
3var utils = require('./utils');
4var _ = require('lodash');
5var Promise = require('bluebird');
6var log = console.log.bind(console);
7var reposApi = utils.github.repos;
8var gTags = Promise.promisify(reposApi.getTags);
9
10function nameAndSha(tags) {
11 if (check.array(tags)) {
12 return _.map(tags, nameAndSha);
13 }
14 return {
15 name: tags.name,
16 sha: tags.commit.sha
17 };
18}
19
20function trimVersions(tags) {
21 return tags.map(function (tag) {
22 tag.name = utils.trimVersion(tag.name);
23 return tag;
24 });
25}
26
27function getTags(options) {
28 la(check.object(options), 'missing options', options);
29 utils.verifyRepoOptions(options);
30
31 return gTags(options)
32 .tap(check.array)
33 .then(nameAndSha)
34 .then(trimVersions);
35}
36
37function getFromToTags(question) {
38 var tagSchema = {
39 from: check.unemptyString,
40 to: check.unemptyString
41 };
42 la(check.schema(tagSchema, question), question);
43
44 return getTags(_.pick(question, 'user', 'repo'))
45 .then(function (allTags) {
46 la(check.array(allTags), 'missing tags', allTags);
47 var fromTag = _.find(allTags, 'name', question.from);
48 la(fromTag, 'cannot find tag', question.from, 'all tags', allTags);
49 var toTag = _.find(allTags, 'name', question.to);
50 la(toTag, 'cannot to tag', question.to, 'all tags', allTags);
51
52 return {
53 fromTag: fromTag,
54 toTag: toTag,
55 allTags: allTags
56 };
57 });
58}
59
60module.exports = getFromToTags;
61
62if (!module.parent) {
63
64 (function examples() {
65
66 /* eslint no-unused-vars:0 */
67 function nextUpdateExample() {
68 var question = {
69 user: 'bahmutov',
70 repo: 'next-update',
71 from: '0.8.0',
72 to: '0.8.2' // or 'latest?'
73 };
74
75 log('Getting commit SHA for the given tags');
76 log(question);
77 getFromToTags(question)
78 .then(log);
79 }
80
81 function chalkExample() {
82 var question = {
83 user: 'chalk',
84 repo: 'chalk',
85 from: '0.5.1',
86 to: '0.3.0'
87 };
88
89 log('Getting commit SHA for the given tags');
90 log('%s / %s from %s to %s',
91 question.user, question.repo, question.from, question.to);
92 getFromToTags(question)
93 .then(log);
94 }
95
96 chalkExample();
97 }());
98}