Skip to content

Commit a713009

Browse files
committed
fix(parsing): parsing www.github.com urls
fixes #15
1 parent fab9a7a commit a713009

File tree

5 files changed

+77
-45
lines changed

5 files changed

+77
-45
lines changed

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
"marked": "0.3.5",
5757
"marked-terminal": "https://github.com/mikaelbr/marked-terminal.git#35f6ddac9d9f32b11e85ceb6bc5f42642f06e456",
5858
"marked-to-md": "1.0.1",
59-
"parse-github-repo-url": "git+https://github.com/bahmutov/parse-github-repo-url.git#38adb1190c3015c4c98e8feea5d5120e2fb5f00a",
59+
"parse-github-repo-url": "https://github.com/bahmutov/parse-github-repo-url.git#52c8b013e1ab3fa4ad8e305e8afeb9109313dc31",
60+
"really-need": "1.7.2",
6061
"simple-get": "1.4.3",
6162
"update-notifier": "0.5.0"
6263
},
@@ -70,7 +71,8 @@
7071
"npm run example-npm",
7172
"npm run example-github",
7273
"npm run example-es6-docs",
73-
"npm run test-search-in-text"
74+
"npm run test-search-in-text",
75+
"npm run test-hr"
7476
],
7577
"pre-push": [
7678
"npm run size",
@@ -81,7 +83,6 @@
8183
}
8284
},
8385
"devDependencies": {
84-
"describe-it": "1.7.0",
8586
"eslint": "1.10.1",
8687
"exact-semver": "1.2.0",
8788
"git-issues": "1.2.0",

src/find-section-spec.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
var la = require('lazy-ass');
22
var check = require('check-more-types');
3-
var partJs = require('path').join(__dirname, 'find-section.js');
4-
var describeIt = require('describe-it');
5-
var marked = require('marked');
63
var quote = require('quote');
74

85
/* global describe, it */
96

7+
// had to remove describe-it - it was conflicting with really-need
8+
/*
9+
var partJs = require('path').join(__dirname, 'find-section.js');
10+
var marked = require('marked');
11+
var describeIt = require('describe-it');
1012
describeIt(partJs, 'findSectionByHeader(search, tokens)', function () {
1113
it('is a function', function () {
1214
la(check.fn(this.findSectionByHeader));
@@ -88,6 +90,7 @@ describeIt(partJs, 'var toTokens', function () {
8890
la(tokens[3].text === 'bar', tokens);
8991
});
9092
});
93+
*/
9194

9295
describe('find section', function () {
9396
var find = require('./find-section');

src/get-readme-spec.js

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
var la = require('lazy-ass');
22
var check = require('check-more-types');
3-
var describeIt = require('describe-it');
4-
var getReadmeFilename = require('path').join(__dirname, 'get-readme.js');
3+
var utils = require('./utils');
54

6-
/* global it */
5+
/* global describe, it */
76

8-
describeIt(getReadmeFilename, 'maybeGithubRepoName(name)', function () {
7+
describe('maybeGithubRepoName(name)', function () {
98
it('returns true for common repos', function () {
109
var cases = [
1110
'foo/bar',
@@ -14,48 +13,48 @@ describeIt(getReadmeFilename, 'maybeGithubRepoName(name)', function () {
1413
'bevacqua/es6'
1514
];
1615
cases.forEach(function (c) {
17-
la(this.maybeGithubRepoName(c), c);
18-
}.bind(this));
16+
la(utils.maybeGithubRepoName(c), c);
17+
});
1918
});
2019

2120
it('returns false for non repos', function () {
22-
la(!this.maybeGithubRepoName('foo/bar/baz'));
23-
la(!this.maybeGithubRepoName('[email protected]:bahmutov/object-fitter.git'));
21+
la(!utils.maybeGithubRepoName('foo/bar/baz'));
22+
la(!utils.maybeGithubRepoName('[email protected]:bahmutov/object-fitter.git'));
2423
});
2524
});
2625

27-
describeIt(getReadmeFilename, 'maybeGithubRepoUrl(name)', function () {
26+
describe('maybeGithubRepoUrl(name)', function () {
2827
it('returns true for common repo urls', function () {
2928
var cases = [
3029
'https://github.com/bevacqua/es6',
3130
'[email protected]:bevacqua/es6.git'
3231
];
3332
cases.forEach(function (c) {
34-
la(this.maybeGithubRepoUrl(c), c);
35-
}.bind(this));
33+
la(utils.maybeGithubRepoUrl(c), c);
34+
});
3635
});
3736
});
3837

39-
describeIt(getReadmeFilename, 'parseGithub(url)', function () {
38+
describe('parseGithub(url)', function () {
4039
it('parses es6 repo url', function () {
4140
var url = 'https://github.com/bevacqua/es6';
42-
var parsed = this.parseGithub(url);
41+
var parsed = utils.parseGithub(url);
4342
la(check.object(parsed), parsed);
4443
la(parsed.user === 'bevacqua', 'invalid username', parsed);
4544
la(parsed.repo === 'es6', 'invalid repo', parsed);
4645
});
4746

4847
it('parses es6 .git repo url', function () {
4948
var url = '[email protected]:bevacqua/es6.git';
50-
var parsed = this.parseGithub(url);
49+
var parsed = utils.parseGithub(url);
5150
la(check.object(parsed), parsed);
5251
la(parsed.user === 'bevacqua', 'invalid username', parsed);
5352
la(parsed.repo === 'es6', 'invalid repo', parsed);
5453
});
5554

5655
it('parses user/repo format', function () {
5756
var url = 'bevacqua/es6';
58-
var parsed = this.parseGithub(url);
57+
var parsed = utils.parseGithub(url);
5958
la(check.object(parsed), parsed);
6059
la(parsed.user === 'bevacqua', 'invalid username', parsed);
6160
la(parsed.repo === 'es6', 'invalid repo', parsed);

src/get-readme.js

+23-24
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
var log = require('debug')('manpm');
22
var la = require('lazy-ass');
33
var check = require('check-more-types');
4+
var parseGithubRepoUrl = require('parse-github-repo-url');
5+
var utils = require('./utils');
6+
7+
// working around github-url-to-object used inside get-package-readme
8+
// that cannot handle www.github.com urls
9+
/* eslint no-undef:0 */
10+
require = require('really-need');
11+
require('github-url-to-object', {
12+
post: function () {
13+
return function gh(url) {
14+
log('parsing github url %s ourselves', url);
15+
var parsed = parseGithubRepoUrl(url);
16+
return {
17+
user: parsed[0],
18+
repo: parsed[1]
19+
};
20+
};
21+
}
22+
});
23+
424
var Promise = require('bluebird');
525
var getReadmeFile = Promise.promisify(require('get-package-readme'));
626
var simpleGet = require('simple-get');
7-
var parseGithubRepoUrl = require('parse-github-repo-url');
827

928
function get(url) {
1029
return new Promise(function (resolve, reject) {
@@ -22,26 +41,6 @@ function get(url) {
2241
});
2342
}
2443

25-
// TODO move to kensho/check-more-types
26-
function maybeGithubRepoName(name) {
27-
var regular = /^[a-zA-Z0-9]+\/[a-zA-Z0-9\-\.]+$/;
28-
return regular.test(name);
29-
}
30-
31-
function maybeGithubRepoUrl(name) {
32-
return Array.isArray(parseGithubRepoUrl(name));
33-
}
34-
35-
function parseGithub(url) {
36-
log('parsing github url', url);
37-
var parsed = parseGithubRepoUrl(url);
38-
la(check.array(parsed), 'could not parse github url', url);
39-
return {
40-
user: parsed[0],
41-
repo: parsed[1]
42-
};
43-
}
44-
4544
var githubSchema = {
4645
user: check.unemptyString,
4746
repo: check.unemptyString
@@ -64,7 +63,7 @@ function formGithubUrl(info, filename) {
6463
function getReadmeFromGithub(name) {
6564
la(check.unemptyString(name), 'missing github info', name);
6665
log('getting readme directly from github for', name);
67-
var parsed = parseGithub(name);
66+
var parsed = utils.parseGithub(name);
6867
la(isValidGithubInfo(parsed), parsed, 'from', name);
6968

7069
var fullUrl = formGithubUrl(parsed, 'README.md');
@@ -84,10 +83,10 @@ function getReadmeFromGithub(name) {
8483
function getReadme(name) {
8584
la(check.unemptyString(name), 'missing name');
8685

87-
if (maybeGithubRepoName(name)) {
86+
if (utils.maybeGithubRepoName(name)) {
8887
log('fetching README for github repo', name);
8988
return getReadmeFromGithub(name);
90-
} else if (maybeGithubRepoUrl(name)) {
89+
} else if (utils.maybeGithubRepoUrl(name)) {
9190
log('fetching README for github url', name);
9291
return getReadmeFromGithub(name);
9392
} else {

src/utils.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var log = require('debug')('manpm');
2+
var la = require('lazy-ass');
3+
var check = require('check-more-types');
4+
var parseGithubRepoUrl = require('parse-github-repo-url');
5+
6+
// TODO move to kensho/check-more-types
7+
function maybeGithubRepoName(name) {
8+
var regular = /^[a-zA-Z0-9]+\/[a-zA-Z0-9\-\.]+$/;
9+
return regular.test(name);
10+
}
11+
12+
function maybeGithubRepoUrl(name) {
13+
return Array.isArray(parseGithubRepoUrl(name));
14+
}
15+
16+
function parseGithub(url) {
17+
log('parsing github url', url);
18+
var parsed = parseGithubRepoUrl(url);
19+
la(check.array(parsed), 'could not parse github url', url);
20+
return {
21+
user: parsed[0],
22+
repo: parsed[1]
23+
};
24+
}
25+
26+
module.exports = {
27+
maybeGithubRepoName: maybeGithubRepoName,
28+
maybeGithubRepoUrl: maybeGithubRepoUrl,
29+
parseGithub: parseGithub
30+
};

0 commit comments

Comments
 (0)