default-branch
Advanced tools
Comparing version 1.1.1 to 2.0.0
@@ -11,16 +11,10 @@ /** | ||
defaultBranch('knutkirkhorn/emorjis').then(branch => { | ||
console.log(branch); | ||
// => main | ||
}); | ||
console.log(await defaultBranch('knutkirkhorn/emorjis')); | ||
// => main | ||
defaultBranch('https://github.com/knutkirkhorn/btc-value-cli').then(branch => { | ||
console.log(branch); | ||
// => main | ||
}); | ||
console.log(await defaultBranch('https://github.com/knutkirkhorn/btc-value-cli')); | ||
// => main | ||
defaultBranch('https://enterprise.github.corp/org/repo').then(branch => { | ||
console.log(branch); | ||
// => default | ||
}); | ||
console.log(await defaultBranch('https://enterprise.github.corp/org/repo')); | ||
// => default | ||
``` | ||
@@ -27,0 +21,0 @@ */ |
62
index.js
@@ -1,39 +0,37 @@ | ||
'use strict'; | ||
import https from 'node:https'; | ||
const https = require('https'); | ||
const regexp = /class="(.*)branch-name(.*)>(.*)</g; // The default branch is always the first on the page | ||
module.exports = 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(); | ||
export default function defaultBranch(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(); | ||
return new Promise((resolve, reject) => { | ||
https.get(`${repositoryPath}/branches`, response => { | ||
if (response.statusCode < 200 || response.statusCode > 299) { | ||
reject(new Error(`Failed to load url: ${response.statusCode}`)); | ||
return; | ||
} | ||
return new Promise((resolve, reject) => { | ||
https.get(`${repositoryPath}/branches`, response => { | ||
if (response.statusCode < 200 || response.statusCode > 299) { | ||
reject(new Error(`Failed to load url: ${response.statusCode}`)); | ||
return; | ||
} | ||
response.setEncoding('UTF-8'); | ||
let data = ''; | ||
response.setEncoding('utf8'); | ||
let data = ''; | ||
response.on('data', body => { | ||
data += body; | ||
}); | ||
response.on('data', body => { | ||
data += body; | ||
}); | ||
response.on('end', () => { | ||
try { | ||
// The first item (0) will be at the top and will be the default branch | ||
const regexMatch = data.match(regexp)[0].split('>')[1].split('<')[0]; | ||
return resolve(regexMatch); | ||
} catch (error) { | ||
return reject(new Error(`Failed to get default branch: ${error}`)); | ||
} | ||
}); | ||
}).on('error', error => reject(error)); | ||
}); | ||
}; | ||
response.on('end', () => { | ||
try { | ||
// The first item (0) will be at the top and will be the default branch | ||
const regexMatch = data.match(regexp)[0].split('>')[1].split('<')[0]; | ||
return resolve(regexMatch); | ||
} catch (error) { | ||
return reject(new Error(`Failed to get default branch: ${error}`)); | ||
} | ||
}); | ||
}).on('error', error => reject(error)); | ||
}); | ||
} |
{ | ||
"name": "default-branch", | ||
"version": "1.1.1", | ||
"description": "Get the default branch of a GitHub repository", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "ava && tsd", | ||
"lint": "eslint ." | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/knutkirkhorn/default-branch.git" | ||
}, | ||
"keywords": [ | ||
"default", | ||
"branch", | ||
"GitHub", | ||
"repository" | ||
], | ||
"author": "Knut Kirkhorn", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/knutkirkhorn/default-branch/issues" | ||
}, | ||
"homepage": "https://github.com/knutkirkhorn/default-branch#readme", | ||
"devDependencies": { | ||
"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" | ||
} | ||
"name": "default-branch", | ||
"version": "2.0.0", | ||
"description": "Get the default branch of a GitHub repository", | ||
"scripts": { | ||
"test": "ava && tsd", | ||
"lint": "eslint ." | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/knutkirkhorn/default-branch.git" | ||
}, | ||
"keywords": [ | ||
"default", | ||
"branch", | ||
"GitHub", | ||
"repository" | ||
], | ||
"author": "Knut Kirkhorn", | ||
"type": "module", | ||
"exports": "./index.js", | ||
"types": "./index.d.ts", | ||
"engines": { | ||
"node": ">=14.16" | ||
}, | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/knutkirkhorn/default-branch/issues" | ||
}, | ||
"homepage": "https://github.com/knutkirkhorn/default-branch#readme", | ||
"devDependencies": { | ||
"ava": "^5.2.0", | ||
"eslint": "^8.34.0", | ||
"eslint-config-airbnb-base": "^15.0.0", | ||
"eslint-plugin-ava": "^14.0.0", | ||
"eslint-plugin-import": "^2.27.5", | ||
"eslint-plugin-unicorn": "^45.0.2", | ||
"tsd": "^0.25.0" | ||
} | ||
} |
# default-branch | ||
> Get the default branch of a GitHub repository | ||
## Installation | ||
``` | ||
$ npm install default-branch | ||
npm install default-branch | ||
``` | ||
## Usage | ||
```js | ||
const defaultBranch = require('default-branch'); | ||
import defaultBranch from 'default-branch'; | ||
defaultBranch('knutkirkhorn/emorjis').then(branch => { | ||
console.log(branch); | ||
// => main | ||
}); | ||
console.log(await defaultBranch('knutkirkhorn/emorjis')); | ||
// => main | ||
defaultBranch('https://github.com/knutkirkhorn/btc-value-cli').then(branch => { | ||
console.log(branch); | ||
// => main | ||
}); | ||
console.log(await defaultBranch('https://github.com/knutkirkhorn/btc-value-cli')); | ||
// => main | ||
defaultBranch('https://enterprise.github.corp/org/repo').then(branch => { | ||
console.log(branch); | ||
// => default | ||
}); | ||
console.log(await defaultBranch('https://enterprise.github.corp/org/repo')); | ||
// => default | ||
``` | ||
## API | ||
### defaultBranch(path) | ||
Returns the default branch of a repository. The `path` can be `username/repo-name`, or a full url to the repository (example: `https://github.com/knutkirkhorn/btc-value-cli`). | ||
## Related | ||
- [has-license](https://github.com/knutkirkhorn/has-license) - Check if a repository has a license | ||
- [gh-repo-has-license](https://github.com/knutkirkhorn/gh-repo-has-license) - Check if a GitHub repository has a license | ||
## License | ||
MIT © [Knut Kirkhorn](LICENSE) |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
0
Yes
4621
7
48
36