1 | const Fetcher = require('./fetcher.js')
|
---|
2 | const FileFetcher = require('./file.js')
|
---|
3 | const cacache = require('cacache')
|
---|
4 | const Minipass = require('minipass')
|
---|
5 | const { promisify } = require('util')
|
---|
6 | const readPackageJson = require('read-package-json-fast')
|
---|
7 | const tarCreateOptions = require('./util/tar-create-options.js')
|
---|
8 | const packlist = require('npm-packlist')
|
---|
9 | const tar = require('tar')
|
---|
10 | const _prepareDir = Symbol('_prepareDir')
|
---|
11 | const { resolve } = require('path')
|
---|
12 |
|
---|
13 | const runScript = require('@npmcli/run-script')
|
---|
14 |
|
---|
15 | const _tarballFromResolved = Symbol.for('pacote.Fetcher._tarballFromResolved')
|
---|
16 | class DirFetcher extends Fetcher {
|
---|
17 | constructor (spec, opts) {
|
---|
18 | super(spec, opts)
|
---|
19 | // just the fully resolved filename
|
---|
20 | this.resolved = this.spec.fetchSpec
|
---|
21 | }
|
---|
22 |
|
---|
23 | // exposes tarCreateOptions as public API
|
---|
24 | static tarCreateOptions (manifest) {
|
---|
25 | return tarCreateOptions(manifest)
|
---|
26 | }
|
---|
27 |
|
---|
28 | get types () {
|
---|
29 | return ['directory']
|
---|
30 | }
|
---|
31 |
|
---|
32 | [_prepareDir] () {
|
---|
33 | return this.manifest().then(mani => {
|
---|
34 | if (!mani.scripts || !mani.scripts.prepare)
|
---|
35 | return
|
---|
36 |
|
---|
37 | // we *only* run prepare.
|
---|
38 | // pre/post-pack is run by the npm CLI for publish and pack,
|
---|
39 | // but this function is *also* run when installing git deps
|
---|
40 | const stdio = this.opts.foregroundScripts ? 'inherit' : 'pipe'
|
---|
41 |
|
---|
42 | // hide the banner if loglevel is silent, or if prepare running
|
---|
43 | // in the background.
|
---|
44 | const banner = this.opts.log && this.opts.log.level === 'silent' ? false
|
---|
45 | : stdio === 'inherit'
|
---|
46 |
|
---|
47 | return runScript({
|
---|
48 | pkg: mani,
|
---|
49 | event: 'prepare',
|
---|
50 | path: this.resolved,
|
---|
51 | stdioString: true,
|
---|
52 | stdio,
|
---|
53 | banner,
|
---|
54 | env: {
|
---|
55 | npm_package_resolved: this.resolved,
|
---|
56 | npm_package_integrity: this.integrity,
|
---|
57 | npm_package_json: resolve(this.resolved, 'package.json'),
|
---|
58 | },
|
---|
59 | })
|
---|
60 | })
|
---|
61 | }
|
---|
62 |
|
---|
63 | [_tarballFromResolved] () {
|
---|
64 | const stream = new Minipass()
|
---|
65 | stream.resolved = this.resolved
|
---|
66 | stream.integrity = this.integrity
|
---|
67 |
|
---|
68 | // run the prepare script, get the list of files, and tar it up
|
---|
69 | // pipe to the stream, and proxy errors the chain.
|
---|
70 | this[_prepareDir]()
|
---|
71 | .then(() => packlist({ path: this.resolved }))
|
---|
72 | .then(files => tar.c(tarCreateOptions(this.package), files)
|
---|
73 | .on('error', er => stream.emit('error', er)).pipe(stream))
|
---|
74 | .catch(er => stream.emit('error', er))
|
---|
75 | return stream
|
---|
76 | }
|
---|
77 |
|
---|
78 | manifest () {
|
---|
79 | if (this.package)
|
---|
80 | return Promise.resolve(this.package)
|
---|
81 |
|
---|
82 | return readPackageJson(this.resolved + '/package.json')
|
---|
83 | .then(mani => this.package = {
|
---|
84 | ...mani,
|
---|
85 | _integrity: this.integrity && String(this.integrity),
|
---|
86 | _resolved: this.resolved,
|
---|
87 | _from: this.from,
|
---|
88 | })
|
---|
89 | }
|
---|
90 |
|
---|
91 | packument () {
|
---|
92 | return FileFetcher.prototype.packument.apply(this)
|
---|
93 | }
|
---|
94 | }
|
---|
95 | module.exports = DirFetcher
|
---|