default-branch
Advanced tools
Comparing version 1.0.8 to 1.1.0
33
index.js
'use strict'; | ||
const https = require('https'); | ||
const regexp = /class="(.*)branch-name(.*)>(.*)</g; // The default branch is always the first on the page | ||
module.exports = path => { | ||
if (!(path.startsWith('https://github.com/') || path.startsWith('http://github.com/'))) { | ||
path = 'https://github.com/' + path; | ||
} | ||
// Normalize path to be URL relative to https://github.com/ by default | ||
const url = new URL(path, 'https://github.com/'); | ||
// Force https connection | ||
url.protocol = 'https:'; | ||
const repositoryPath = url.toString(); | ||
if (path.startsWith('http://github.com/')) { | ||
// Force https connection | ||
path = path.substr(0, 4) + 's' + path.substr(4); | ||
} | ||
return new Promise((resolve, reject) => { | ||
https.get(path + '/branches', (response) => { | ||
https.get(`${repositoryPath}/branches`, response => { | ||
if (response.statusCode < 200 || response.statusCode > 299) { | ||
reject(new Error('Failed to load url: ' + response.statusCode)); | ||
reject(new Error(`Failed to load url: ${response.statusCode}`)); | ||
return; | ||
@@ -25,7 +24,7 @@ } | ||
response.on('data', function(body) { | ||
response.on('data', body => { | ||
data += body; | ||
}); | ||
response.on('end', function() { | ||
response.on('end', () => { | ||
try { | ||
@@ -35,10 +34,8 @@ // The first item (0) will be at the top and will be the default branch | ||
return resolve(regexMatch); | ||
} catch (err) { | ||
return reject(new Error('Failed to get default branch: ' + err)); | ||
} catch (error) { | ||
return reject(new Error(`Failed to get default branch: ${error}`)); | ||
} | ||
}); | ||
}).on('error', (error) => { | ||
return reject(error); | ||
}); | ||
}).on('error', error => reject(error)); | ||
}); | ||
}; | ||
}; |
{ | ||
"name": "default-branch", | ||
"version": "1.0.8", | ||
"version": "1.1.0", | ||
"description": "Get the default branch of a GitHub repository", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "ava" | ||
"test": "ava && tsd", | ||
"lint": "eslint ." | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"repository": { | ||
@@ -26,4 +31,9 @@ "type": "git", | ||
"devDependencies": { | ||
"ava": "^1.3.1" | ||
"ava": "^3.15.0", | ||
"eslint": "^7.24.0", | ||
"eslint-config-airbnb-base": "^14.2.1", | ||
"eslint-plugin-ava": "^12.0.0", | ||
"eslint-plugin-import": "^2.22.1", | ||
"tsd": "^0.14.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# default-branch [![Build Status](https://travis-ci.org/Knutakir/default-branch.svg?branch=master)](https://travis-ci.org/Knutakir/default-branch) | ||
# default-branch [![Build Status](https://travis-ci.org/Knutakir/default-branch.svg?branch=main)](https://travis-ci.org/Knutakir/default-branch) | ||
> Get the default branch of a GitHub repository | ||
@@ -15,3 +15,3 @@ | ||
console.log(branch); | ||
// => master | ||
// => main | ||
}); | ||
@@ -21,4 +21,9 @@ | ||
console.log(branch); | ||
// => master | ||
// => main | ||
}); | ||
defaultBranch('https://enterprise.github.corp/org/repo').then(branch => { | ||
console.log(branch); | ||
// => default | ||
}); | ||
``` | ||
@@ -25,0 +30,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5241
55
39
6
5