What is get-pkg-repo?
The get-pkg-repo npm package is designed to extract repository information from a package.json file. This can be useful for various tasks such as automating repository-related workflows, generating documentation, or integrating with other tools that require repository details.
What are get-pkg-repo's main functionalities?
Extract Repository URL
This feature allows you to extract the repository URL from a package.json file. The code sample demonstrates how to require the get-pkg-repo package, read the package.json file, and then extract and log the repository URL.
const getPkgRepo = require('get-pkg-repo');
const pkg = require('./package.json');
const repo = getPkgRepo(pkg);
console.log(repo.url);
Extract Repository Type
This feature allows you to extract the type of repository (e.g., git) from a package.json file. The code sample shows how to extract and log the repository type.
const getPkgRepo = require('get-pkg-repo');
const pkg = require('./package.json');
const repo = getPkgRepo(pkg);
console.log(repo.type);
Extract Repository Directory
This feature allows you to extract the directory within the repository if specified in the package.json file. The code sample demonstrates how to extract and log the repository directory.
const getPkgRepo = require('get-pkg-repo');
const pkg = require('./package.json');
const repo = getPkgRepo(pkg);
console.log(repo.directory);
Other packages similar to get-pkg-repo
normalize-package-data
The normalize-package-data package is used to normalize package metadata, including repository information. It provides a more comprehensive solution for handling package.json data but may be overkill if you only need repository details.
parse-package-name
The parse-package-name package is designed to parse package names and extract information such as scope and name. While it doesn't specifically focus on repository information, it can be used in conjunction with other tools to achieve similar results.
read-pkg
The read-pkg package is used to read and parse package.json files. It provides a simple way to access package metadata, including repository information, but requires additional logic to extract specific details like repository URL or type.
![Coverage Status](https://coveralls.io/repos/stevemao/get-pkg-repo/badge.svg)
Get repository url from package json data
Synopsis
People write different formats of repository url in package.json and sometimes there is even a typo.
This module uses normalize-package-data, hosted-git-info and npm/repo to parse data. Please check them out for more details.
This module can fix some common typos.
Install
$ npm install --save get-pkg-repo
Usage
var fs = require('fs');
var getPkgRepo = require('get-pkg-repo');
fs.readFile('package.json', function(err, pkgData) {
if (err) {
...
}
var repo = getPkgRepo(pkgData);
console.log(repo)
})
API
getPkgRepo(pkgData, [fixTypo])
Returns a hosted-git-info returned object if it matches a git host. If not returns a url.parse
object with a browse
function which returns the url that can be browsed.
pkgData
Type: object
or json
Package.json data
fixTypo
Type: boolean
If you want to fix your typical typos automatically, pass true. See the list of predefined typos.
CLI
$ npm install --global get-pkg-repo
You can use cli to see what your url will look like after being parsed.
You can enter interactive mode by typing
$ get-pkg-repo
github.com/stevemao/get-pkg-repo
{ protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: null,
pathname: 'github.com/stevemao/get-pkg-repo',
path: 'github.com/stevemao/get-pkg-repo',
href: 'github.com/stevemao/get-pkg-repo',
browse: [Function] }
https://nodejs.org/api/util.htm
{ protocol: 'https:',
slashes: true,
auth: null,
host: 'nodejs.org',
port: null,
hostname: 'nodejs.org',
hash: null,
search: null,
query: null,
pathname: '/api/util.htm',
path: '/api/util.htm',
href: 'https://nodejs.org/api/util.htm',
browse: [Function] }
You can also validate the repository url in your package.json by using the command followed by a package.json path. You can specify more than one path at a time.
$ get-pkg-repo package.json
{ type: 'github',
protocols: [ 'git', 'http', 'git+ssh', 'git+https', 'ssh', 'https' ],
...
$ cat package.json | get-pkg-repo --fix-typo
{ type: 'github',
protocols: [ 'git', 'http', 'git+ssh', 'git+https', 'ssh', 'https' ],
...
License
MIT © Steve Mao