• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# libnpmpublish [![npm version](https://img.shields.io/npm/v/libnpmpublish.svg)](https://npm.im/libnpmpublish) [![license](https://img.shields.io/npm/l/libnpmpublish.svg)](https://npm.im/libnpmpublish) [![Travis](https://img.shields.io/travis/npm/libnpmpublish.svg)](https://travis-ci.org/npm/libnpmpublish) [![AppVeyor](https://ci.appveyor.com/api/projects/status/github/zkat/libnpmpublish?svg=true)](https://ci.appveyor.com/project/zkat/libnpmpublish) [![Coverage Status](https://coveralls.io/repos/github/npm/libnpmpublish/badge.svg?branch=latest)](https://coveralls.io/github/npm/libnpmpublish?branch=latest)
2
3[`libnpmpublish`](https://github.com/npm/libnpmpublish) is a Node.js library for
4programmatically publishing and unpublishing npm packages. It does not take care
5of packing tarballs from source code, but once you have a tarball, it can take
6care of putting it up on a nice registry for you.
7
8## Example
9
10```js
11const { publish, unpublish } = require('libnpmpublish')
12
13```
14
15## Install
16
17`$ npm install libnpmpublish`
18
19## Table of Contents
20
21* [Example](#example)
22* [Install](#install)
23* [API](#api)
24  * [publish/unpublish opts](#opts)
25  * [`publish()`](#publish)
26  * [`unpublish()`](#unpublish)
27
28### API
29
30#### <a name="opts"></a> `opts` for `libnpmpublish` commands
31
32`libnpmpublish` uses [`npm-registry-fetch`](https://npm.im/npm-registry-fetch).
33Most options are passed through directly to that library, so please refer to
34[its own `opts`
35documentation](https://www.npmjs.com/package/npm-registry-fetch#fetch-options)
36for options that can be passed in.
37
38A couple of options of note for those in a hurry:
39
40* `opts.token` - can be passed in and will be used as the authentication token for the registry. For other ways to pass in auth details, see the n-r-f docs.
41* `opts.Promise` - If you pass this in, the Promises returned by `libnpmpublish` commands will use this Promise class instead. For example: `{Promise: require('bluebird')}`
42
43#### <a name="publish"></a> `> libpub.publish(pkgJson, tarData, [opts]) -> Promise`
44
45Publishes `tarData` to the appropriate configured registry. `pkgJson` should be
46the parsed `package.json` for the package that is being published.
47
48`tarData` can be a Buffer, a base64-encoded string, or a binary stream of data.
49Note that publishing itself can't be streamed, so the entire stream will be
50consumed into RAM before publishing (and are thus limited in how big they can
51be).
52
53Since `libnpmpublish` does not generate tarballs itself, one way to build your
54own tarball for publishing is to do `npm pack` in the directory you wish to
55pack. You can then `fs.createReadStream('my-proj-1.0.0.tgz')` and pass that to
56`libnpmpublish`, along with `require('./package.json')`.
57
58`publish()` does its best to emulate legacy publish logic in the standard npm
59client, and so should generally be compatible with any registry the npm CLI has
60been able to publish to in the past.
61
62If `opts.npmVersion` is passed in, it will be used as the `_npmVersion` field in
63the outgoing packument. It's recommended you add your own user agent string in
64there!
65
66If `opts.algorithms` is passed in, it should be an array of hashing algorithms
67to generate `integrity` hashes for. The default is `['sha512']`, which means you
68end up with `dist.integrity = 'sha512-deadbeefbadc0ffee'`. Any algorithm
69supported by your current node version is allowed -- npm clients that do not
70support those algorithms will simply ignore the unsupported hashes.
71
72If `opts.access` is passed in, it must be one of `public` or `restricted`.
73Unscoped packages cannot be `restricted`, and the registry may agree or disagree
74with whether you're allowed to publish a restricted package.
75
76##### Example
77
78```javascript
79const pkg = require('./dist/package.json')
80const tarball = fs.createReadStream('./dist/pkg-1.0.1.tgz')
81await libpub.publish(pkg, tarball, {
82  npmVersion: 'my-pub-script@1.0.2',
83  token: 'my-auth-token-here'
84})
85// Package has been published to the npm registry.
86```
87
88#### <a name="unpublish"></a> `> libpub.unpublish(spec, [opts]) -> Promise`
89
90Unpublishes `spec` from the appropriate registry. The registry in question may
91have its own limitations on unpublishing.
92
93`spec` should be either a string, or a valid
94[`npm-package-arg`](https://npm.im/npm-package-arg) parsed spec object. For
95legacy compatibility reasons, only `tag` and `version` specs will work as
96expected. `range` specs will fail silently in most cases.
97
98##### Example
99
100```javascript
101await libpub.unpublish('lodash', { token: 'i-am-the-worst'})
102//
103// `lodash` has now been unpublished, along with all its versions, and the world
104// devolves into utter chaos.
105//
106// That, or we all go home to our friends and/or family and have a nice time
107// doing nothing having to do with programming or JavaScript and realize our
108// lives are just so much happier now, and we just can't understand why we ever
109// got so into this JavaScript thing but damn did it pay well. I guess you'll
110// settle for gardening or something.
111```
112