Giter Club home page Giter Club logo

npm-package-arg's Introduction

npm-package-arg

Parse package name and specifier passed to commands like npm install or npm cache add. This just parses the text given-- it's worth noting that npm has further logic it applies by looking at your disk to figure out what ambiguous specifiers are. If you want that logic, please see realize-package-specifier.

Arguments look like: [email protected], @bar/[email protected], foo@user/foo, http://x.com/foo.tgz, git+https://github.com/user/foo, bitbucket:user/foo, foo.tar.gz or bar

EXAMPLES

var assert = require("assert")
var npa = require("npm-package-arg")

// Pass in the descriptor, and it'll return an object
var parsed = npa("@bar/[email protected]")

// Returns an object like:
{
  raw: '@bar/[email protected]',   // what was passed in
  name: "@bar/foo",      // the name of the package
  scope: "@bar",         // the private scope of the package, or null
  type: "range",         // the type of specifier this is
  spec: ">=1.2.0 <1.3.0" // the expanded specifier
  rawSpec: "1.2"         // the specifier as passed in
 }

// Parsing urls pointing at hosted git services produces a variation:
var parsed = npa("git+https://github.com/user/foo")

// Returns an object like:
{
  raw: 'git+https://github.com/user/foo',
  scope: null,
  name: null,
  rawSpec: 'git+https://github.com/user/foo',
  spec: 'user/foo',
  type: 'hosted',
  hosted: {
    type: 'github',
    ssh: '[email protected]:user/foo.git',
    sshurl: 'git+ssh://[email protected]/user/foo.git',
    https: 'https://github.com/user/foo.git',
    directUrl: 'https://raw.githubusercontent.com/user/foo/master/package.json'
  }
}

// Completely unreasonable invalid garbage throws an error
// Make sure you wrap this in a try/catch if you have not
// already sanitized the inputs!
assert.throws(function() {
  npa("this is not \0 a valid package name or url")
})

USING

var npa = require('npm-package-arg')

  • var result = npa(arg)

Parses arg and returns a result object detailing what arg is.

arg -- a package descriptor, like: [email protected], or foo@user/foo, or http://x.com/foo.tgz, or git+https://github.com/user/foo

RESULT OBJECT

The objects that are returned by npm-package-arg contain the following keys:

  • name - If known, the name field expected in the resulting pkg.
  • type - One of the following strings:
    • git - A git repo
    • hosted - A hosted project, from github, bitbucket or gitlab. Originally either a full url pointing at one of these services or a shorthand like user/project or github:user/project for github or bitbucket:user/project for bitbucket.
    • tag - A tagged version, like "foo@latest"
    • version - A specific version number, like "[email protected]"
    • range - A version range, like "[email protected]"
    • local - A local file or folder path
    • remote - An http url (presumably to a tgz)
  • spec - The "thing". URL, the range, git repo, etc.
  • hosted - If type=hosted this will be an object with the following keys:
    • type - github, bitbucket or gitlab
    • ssh - The ssh path for this git repo
    • sshUrl - The ssh URL for this git repo
    • httpsUrl - The HTTPS URL for this git repo
    • directUrl - The URL for the package.json in this git repo
  • raw - The original un-modified string that was provided.
  • rawSpec - The part after the name@..., as it was originally provided.
  • scope - If a name is something like @org/module then the scope field will be set to org. If it doesn't have a scoped name, then scope is null.

If you only include a name and no specifier part, eg, foo or foo@ then a default of latest will be used (as of 4.1.0). This is contrast with previous behavior where * was used.

npm-package-arg's People

Contributors

ajcrites avatar bacra avatar dylang avatar iarna avatar isaacs avatar othiym23 avatar zkat avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.