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