Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
feat: resolve to tarball data Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiahdz committed Mar 27, 2020
1 parent 1d14655 commit cb2ecf2
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 485 deletions.
74 changes: 6 additions & 68 deletions README.md
Expand Up @@ -27,15 +27,12 @@ const pack = require('libnpmpack')

### API


#### <a name="pack"></a> `> pack(spec, [opts]) -> Promise`

Packs a tarball from a local directory or from a registry or github spec and saves it on disk. Returns a Promise that resolves to an object containing the tarball contents.
Packs a tarball from a local directory or from a registry or github spec and returns a Promise that resolves to the tarball data Buffer, with from, resolved, and integrity fields attached.

If no options are passed, the tarball file will be saved on the same directory from which `pack` was called in.

If `opts.target` is passed in, it will save the tarball file on the location entered.

`libnpmpack` uses [`pacote`](https://npm.im/pacote).
Most options are passed through directly to that library, so please refer to
[its own `opts`
Expand All @@ -45,74 +42,15 @@ for options that can be passed in.
##### Examples

```javascript
// packs from local directory
const localTar = await pack()
console.log(localTar)
/*
{
id: 'my-cool-pkg@1.0.0',
name: 'my-cool-pkg',
version: '1.0.0',
size: 260,
unpackedSize: 133,
shasum: '535bdcc05fd4a1b7f2603c5527a7c63ba5b88cff',
integrity: ssri.parse(integrity.sha512[0]),
filename: 'my-cool-pkg-1.0.0.tgz',
files: [
{ path: 'index.js', size: 5, mode: 420 },
{ path: 'node_modules/a/package.json', size: 39, mode: 420 },
{ path: 'package.json', size: 89, mode: 420 }
],
entryCount: 3,
bundled: ['a']
}
*/
// packs from cwd
const tarball = await pack()

// packs from a local directory
const localTar = await pack('/Users/claudiahdz/projects/my-cool-pkg')

// packs from a registry spec
const registryTar = await pack('abbrev@1.0.3')
console.log(registryTar)
/*
{
id: abbrev@1.0.3,
name: 'abbrev',
version: '1.0.3',
size: 1526,
unpackedSize: 3358,
shasum: 'aa049c967f999222aa42e14434f0c562ef468241',
integrity: Integrity { sha512: [ [Hash] ] },
filename: 'abbrev-1.0.3.tgz',
files: [
{ path: 'package.json', size: 277, mode: 420 },
{ path: 'README.md', size: 499, mode: 420 },
{ path: 'lib/abbrev.js', size: 2582, mode: 420 }
],
entryCount: 3,
bundled: []
}
*/

// packs from a github spec
const githubTar = await pack('isaacs/rimraf#PR-192')
/*
{
id: 'rimraf@2.6.3',
name: 'rimraf',
version: '2.6.3',
size: 5664,
unpackedSize: 15463,
shasum: '9f5edf99046b4096d610532f0ec279135a624b15',
integrity: Integrity { sha512: [ [Hash] ] },
filename: 'rimraf-2.6.3.tgz',
files: [
{ path: 'LICENSE', size: 765, mode: 420 },
{ path: 'bin.js', size: 1196, mode: 493 },
{ path: 'rimraf.js', size: 9225, mode: 420 },
{ path: 'package.json', size: 677, mode: 420 },
{ path: 'README.md', size: 3600, mode: 420 }
],
entryCount: 5,
bundled: []
}
*/
```

37 changes: 5 additions & 32 deletions index.js
@@ -1,27 +1,15 @@
'use strict'

const os = require('os')
const fs = require('fs')
const path = require('path')
const util = require('util')
const pacote = require('pacote')
const npa = require('npm-package-arg')
const mv = require('move-concurrently')
const runScript = require('@npmcli/run-script')

const rimraf = util.promisify(require('rimraf'))
const mkdtemp = util.promisify(fs.mkdtemp)

const { getContents, logTar } = require('./utils/tar')

module.exports = pack
async function pack (spec = 'file:.', opts = {}) {
const { target = null } = opts
// gets spec
spec = npa(spec)

const manifest = await pacote.manifest(spec, opts)
const filename = path.basename(`${manifest.name}-${manifest.version}.tgz`)
const dest = target || `${process.cwd()}/${filename}`

if (spec.type === 'directory') {
// prepack
Expand All @@ -30,26 +18,16 @@ async function pack (spec = 'file:.', opts = {}) {
event: 'prepack',
path: spec.fetchSpec,
stdio: 'inherit',
pkg: manifest,
env: {
npm_package_target: dest
}
pkg: manifest
})
}

const tmpDir = await mkdtemp(path.join(os.tmpdir(), 'libnpmpack-'))
const tmpTarget = `${tmpDir}/${filename}`

// packs tarball on tmp location
const tarball = await pacote.tarball.file(manifest._resolved, tmpTarget, {
// packs tarball
const tarball = await pacote.tarball(manifest._resolved, {
...opts,
integrity: manifest._integrity
})

// moves tarball to dest
await mv(tmpTarget, dest)
await rimraf(tmpDir)

if (spec.type === 'directory') {
// postpack
await runScript({
Expand All @@ -59,17 +37,12 @@ async function pack (spec = 'file:.', opts = {}) {
stdio: 'inherit',
pkg: manifest,
env: {
npm_package_target: dest,
npm_package_from: tarball.from,
npm_package_resolved: tarball.resolved,
npm_package_integrity: tarball.integrity
}
})
}

const contents = await getContents(manifest, dest)
return contents
return tarball
}

pack.logTar = logTar
module.exports = pack
108 changes: 26 additions & 82 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cb2ecf2

Please sign in to comment.