UNPKG

2.69 kBJavaScriptView Raw
1require('lazy-ass');
2var check = require('check-more-types');
3var Promise = require('bluebird');
4var R = require('ramda');
5var _ = require('lodash');
6var packageJson = Promise.promisify(require('package-json'));
7var packageField = Promise.promisify(require('package-json').field);
8var GitHubApi = require('github');
9
10var github = new GitHubApi({
11 // required
12 version: '3.0.0',
13 // optional
14 debug: false
15});
16
17var log = console.log.bind(console);
18var debug = require('debug')('load');
19
20function compareVersionPackages(from, to) {
21 la(check.object(from) && check.object(to),
22 'invalid from and to package json', from, to);
23 debug('package from %s to %s', from.version, to.version);
24}
25
26var question = {
27 user: 'bahmutov',
28 name: 'next-update',
29 repo: 'next-update',
30 from: '0.8.0',
31 to: '0.8.2' // or 'latest'
32};
33
34function diffVersions() {
35 Promise.all([
36 packageJson(question.name, question.from),
37 packageJson(question.name, question.to)
38 ]).spread(compareVersionPackages);
39}
40
41function packageRepo() {
42 function verifyGithub(repo) {
43 la(check.object(repo) &&
44 repo.type === 'git' &&
45 check.unemptyString(repo.url) &&
46 /github\.com/.test(repo.url),
47 'not a github repo', repo);
48 }
49
50 return packageField(question.name, 'repository')
51 .tap(verifyGithub)
52 .then(R.prop('url'));
53}
54
55// packageRepo()
56// .then(console.log.bind(console));
57
58
59function getTags(options) {
60 var repoSchema = {
61 user: check.unemptyString,
62 repo: check.unemptyString
63 };
64 la(check.object(options), 'missing options', options);
65 la(check.schema(repoSchema, options), options);
66
67 var gTags = Promise.promisify(github.repos.getTags);
68 return gTags({
69 user: 'bahmutov',
70 repo: 'next-update'
71 }).then(function (tags) {
72 la(check.array(tags), 'expected tags to be an array', tags);
73 console.log('received %d tags', tags.length);
74 return _.map(tags, function (tag) {
75 return {
76 name: tag.name,
77 sha: tag.commit.sha
78 };
79 });
80 });
81}
82
83function getFromToTags(question) {
84 var tagSchema = {
85 from: check.unemptyString,
86 to: check.unemptyString
87 };
88 la(check.schema(tagSchema, question), question);
89
90 return getTags(_.pick(question, 'user', 'repo'))
91 .then(function (allTags) {
92 la(check.array(allTags), 'missing tags', allTags);
93 var fromTag = _.find(allTags, 'name', question.from);
94 la(fromTag, 'cannot find tag', question.from);
95 var toTag = _.find(allTags, 'name', question.to);
96 la(toTag, 'cannot to tag', question.to);
97
98 return _.extend(question, {
99 fromTag: fromTag,
100 toTag: toTag
101 });
102 });
103}
104
105// getTags();
106getFromToTags(question)
107 .then(log);