Socket
Socket
Sign inDemoInstall

get-pkg-repo

Package Overview
Dependencies
62
Maintainers
2
Versions
26
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    get-pkg-repo

Get normalized repository from package json data


Version published
Weekly downloads
2.2M
increased by8.22%
Maintainers
2
Install size
761 kB
Created
Weekly downloads
 

Readme

Source

NPM version Build Status Dependency Status Coverage Status

Get normalized repository URL from package json data

Synopsis

People write the repository URL in their package.json file using different formats, and sometimes there is even a typo.

This module extracts the code from the npm package manager, and uses normalize-package-data, hosted-git-info and parse-github-repo-url, to parse the provided package input and return a repository URL object.

If you are interested, please check those other packages for more details.

Common misspelling of the repository field are also corrected (Please see the source code for available corrections.).

This module mutates/normalizes the input using normalize-package-data.

If you find the returned repository URL object is not correct, it's likely an issue with one of the libraries listed earlier. Please try to triage the problem before you open an issue here.

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) {
    ...
  }

  // Contents of `pkgData` are mutated/normalized by `normalize-package-data`.
  var repo = getPkgRepo(pkgData);
  console.log(repo)
  /*=>
  { type: 'github',
    protocols: [ 'git', 'http', 'git+ssh', 'git+https', 'ssh', 'https' ],
    domain: 'github.com',
    treepath: 'tree',
    filetemplate: 'https://{auth@}raw.githubusercontent.com/{user}/{project}/{committish}/{path}',
    bugstemplate: 'https://{domain}/{user}/{project}/issues',
    gittemplate: 'git://{auth@}{domain}/{user}/{project}.git{#committish}',
    sshtemplate: 'git@{domain}:{user}/{project}.git{#committish}',
    sshurltemplate: 'git+ssh://git@{domain}/{user}/{project}.git{#committish}',
    browsetemplate: 'https://{domain}/{user}/{project}{/tree/committish}',
    docstemplate: 'https://{domain}/{user}/{project}{/tree/committish}#readme',
    httpstemplate: 'git+https://{auth@}{domain}/{user}/{project}.git{#committish}',
    shortcuttemplate: '{type}:{user}/{project}{#committish}',
    pathtemplate: '{user}/{project}{#committish}',
    pathmatch: /^[\/]([^\/]+)[\/]([^\/]+?)(?:[.]git)?$/,
    protocols_re: /^(git|http|git\+ssh|git\+https|ssh|https):$/,
    user: 'stevemao',
    auth: null,
    project: 'get-pkg-repo',
    committish: null,
    default: 'https' }
  */
})

API

getPkgRepo(pkgData, [fixTypo])

Returns a hosted-git-info returned object if it matches a git host. If not, it returns a url.parse object with a browse function which returns the url that can be browsed.

pkgData

Type: object or json

Should be the contents of a package.json file.

Contents of pkgData are mutated/normalized by normalize-package-data.

fixTypo

Type: boolean

If you want to fix your typical typos automatically pass true.

Please keep in mind that this feature may be removed in the future. The repository field in package.json is well documented and it should be correctly spelled.

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
https://github.com/stevemao/get-pkg-repo
{ type: 'github',
  protocols: [ 'git', 'http', 'git+ssh', 'git+https', 'ssh', 'https' ],
  domain: 'github.com',
  treepath: 'tree',
  filetemplate: 'https://{auth@}raw.githubusercontent.com/{user}/{project}/{committish}/{path}',
  bugstemplate: 'https://{domain}/{user}/{project}/issues',
  gittemplate: 'git://{auth@}{domain}/{user}/{project}.git{#committish}',
  sshtemplate: 'git@{domain}:{user}/{project}.git{#committish}',
  sshurltemplate: 'git+ssh://git@{domain}/{user}/{project}.git{#committish}',
  browsetemplate: 'https://{domain}/{user}/{project}{/tree/committish}',
  docstemplate: 'https://{domain}/{user}/{project}{/tree/committish}#readme',
  httpstemplate: 'git+https://{auth@}{domain}/{user}/{project}.git{#committish}',
  shortcuttemplate: '{type}:{user}/{project}{#committish}',
  pathtemplate: '{user}/{project}{#committish}',
  pathmatch: /^[\/]([^\/]+)[\/]([^\/]+?)(?:[.]git)?$/,
  protocols_re: /^(git|http|git\+ssh|git\+https|ssh|https):$/,
  user: 'stevemao',
  auth: null,
  project: 'get-pkg-repo',
  committish: null,
  default: 'https' }

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' ],
...
# or
$ cat package.json | get-pkg-repo --fix-typo
{ type: 'github',
  protocols: [ 'git', 'http', 'git+ssh', 'git+https', 'ssh', 'https' ],
...

License

MIT © Steve Mao

Keywords

FAQs

Last updated on 16 Oct 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc