UNPKG

2.31 kBJavaScriptView Raw
1require('lazy-ass');
2var check = require('check-more-types');
3var R = require('ramda');
4
5var repoSchema = {
6 user: check.unemptyString,
7 repo: check.unemptyString
8};
9
10var isRepoQuestion = R.partial(check.schema, repoSchema);
11function verifyRepoOptions(options) {
12 la(isRepoQuestion(options), 'missing repo info', options);
13}
14
15function isGithubUrl(url) {
16 return check.unemptyString(url) &&
17 /github\.com/.test(url);
18}
19
20function verifyGithub(repo) {
21 la(check.object(repo) &&
22 repo.type === 'git' &&
23 isGithubUrl(repo.url),
24 'not a github repo', repo);
25}
26
27var GitHubApi = require('github');
28var github = new GitHubApi({
29 // required
30 version: '3.0.0',
31 // optional
32 debug: false
33});
34
35function parseGithubUrl(url) {
36 la(isGithubUrl(url), 'not a github url', url);
37 var githubUrlRegex = /github\.com[\/:]([a-zA-Z-]+?)\/([a-zA-Z-]+?)(\.git)?$/;
38 var matches = githubUrlRegex.exec(url);
39 la(check.array(matches),
40 'could not extract user and repo name from github url', url);
41 return {
42 user: matches[1],
43 repo: matches[2]
44 };
45}
46
47function shortenSha(str) {
48 la(check.unemptyString(str), 'expected long commit sha string', str);
49 return str.substr(0, 7);
50}
51
52function trimVersion(str) {
53 la(check.unemptyString(str), 'missig tag', str);
54 var startsWithV = /^v\d+/;
55 if (startsWithV.test(str)) {
56 return str.substr(1);
57 }
58 return str;
59}
60
61function firstLine(str) {
62 la(check.string(str), 'expected str', str);
63 str = str.trim();
64 var firstNewline = str.indexOf('\n');
65 if (firstNewline !== -1) {
66 return str.substr(0, firstNewline);
67 }
68 return str;
69}
70
71// returns true if the package name is really github username/reponame
72var userRepo = /^([\w-]+)?\/([\w-]+)?$/;
73function isGithubName(str) {
74 return check.unemptyString(str) &&
75 userRepo.test(str);
76}
77
78function parseGithubName(str) {
79 la(isGithubName(str), 'not a github name', str);
80 var matches = userRepo.exec(str);
81 return {
82 user: matches[1],
83 repo: matches[2]
84 };
85}
86
87module.exports = {
88 isRepoQuestion: isRepoQuestion,
89 verifyRepoOptions: verifyRepoOptions,
90 github: github,
91 verifyGithub: verifyGithub,
92 parseGithubUrl: parseGithubUrl,
93 trimVersion: trimVersion,
94 firstLine: firstLine,
95 shortenSha: shortenSha,
96 isGithubName: isGithubName,
97 parseGithubName: parseGithubName
98};