summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/pacote/extract.js
blob: f49a54420242ae9def2358c598c7e6adfab1c689 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict'

const BB = require('bluebird')

const extractStream = require('./lib/extract-stream.js')
const fs = require('fs')
const mkdirp = BB.promisify(require('mkdirp'))
const npa = require('npm-package-arg')
const optCheck = require('./lib/util/opt-check.js')
const path = require('path')
const rimraf = BB.promisify(require('rimraf'))
const withTarballStream = require('./lib/with-tarball-stream.js')

const truncateAsync = BB.promisify(fs.truncate)
const readFileAsync = BB.promisify(fs.readFile)
const appendFileAsync = BB.promisify(fs.appendFile)

module.exports = extract
function extract (spec, dest, opts) {
  opts = optCheck(opts)
  spec = npa(spec, opts.where)
  const startTime = Date.now()

  return withTarballStream(spec, opts, stream => {
    return tryExtract(spec, stream, dest, opts)
  })
    .then(() => {
      if (!opts.resolved) {
        const pjson = path.join(dest, 'package.json')
        return readFileAsync(pjson, 'utf8')
          .then(str => truncateAsync(pjson)
            .then(() => appendFileAsync(pjson, str.replace(
              /}\s*$/,
              `\n,"_resolved": ${
                JSON.stringify(opts.resolved || '')
              }\n,"_integrity": ${
                JSON.stringify(opts.integrity || '')
              }\n,"_from": ${
                JSON.stringify(spec.toString())
              }\n}`
            ))))
      }
    })
    .then(() => opts.log.silly(
      'extract',
      `${spec} extracted to ${dest} (${Date.now() - startTime}ms)`
    ))
}

function tryExtract (spec, tarStream, dest, opts) {
  return new BB((resolve, reject) => {
    tarStream.on('error', reject)

    rimraf(dest)
      .then(() => mkdirp(dest))
      .then(() => {
        const xtractor = extractStream(spec, dest, opts)
        xtractor.on('error', reject)
        xtractor.on('close', resolve)
        tarStream.pipe(xtractor)
      })
      .catch(reject)
  })
    .catch(err => {
      if (err.code === 'EINTEGRITY') {
        err.message = `Verification failed while extracting ${spec}:\n${err.message}`
      }

      throw err
    })
}