11cb0ef41Sopenharmony_ciconst Fetcher = require('./fetcher.js')
21cb0ef41Sopenharmony_ciconst FileFetcher = require('./file.js')
31cb0ef41Sopenharmony_ciconst { Minipass } = require('minipass')
41cb0ef41Sopenharmony_ciconst tarCreateOptions = require('./util/tar-create-options.js')
51cb0ef41Sopenharmony_ciconst packlist = require('npm-packlist')
61cb0ef41Sopenharmony_ciconst tar = require('tar')
71cb0ef41Sopenharmony_ciconst _prepareDir = Symbol('_prepareDir')
81cb0ef41Sopenharmony_ciconst { resolve } = require('path')
91cb0ef41Sopenharmony_ciconst _readPackageJson = Symbol.for('package.Fetcher._readPackageJson')
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst runScript = require('@npmcli/run-script')
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst _tarballFromResolved = Symbol.for('pacote.Fetcher._tarballFromResolved')
141cb0ef41Sopenharmony_ciclass DirFetcher extends Fetcher {
151cb0ef41Sopenharmony_ci  constructor (spec, opts) {
161cb0ef41Sopenharmony_ci    super(spec, opts)
171cb0ef41Sopenharmony_ci    // just the fully resolved filename
181cb0ef41Sopenharmony_ci    this.resolved = this.spec.fetchSpec
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci    this.tree = opts.tree || null
211cb0ef41Sopenharmony_ci    this.Arborist = opts.Arborist || null
221cb0ef41Sopenharmony_ci  }
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  // exposes tarCreateOptions as public API
251cb0ef41Sopenharmony_ci  static tarCreateOptions (manifest) {
261cb0ef41Sopenharmony_ci    return tarCreateOptions(manifest)
271cb0ef41Sopenharmony_ci  }
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  get types () {
301cb0ef41Sopenharmony_ci    return ['directory']
311cb0ef41Sopenharmony_ci  }
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  [_prepareDir] () {
341cb0ef41Sopenharmony_ci    return this.manifest().then(mani => {
351cb0ef41Sopenharmony_ci      if (!mani.scripts || !mani.scripts.prepare) {
361cb0ef41Sopenharmony_ci        return
371cb0ef41Sopenharmony_ci      }
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci      // we *only* run prepare.
401cb0ef41Sopenharmony_ci      // pre/post-pack is run by the npm CLI for publish and pack,
411cb0ef41Sopenharmony_ci      // but this function is *also* run when installing git deps
421cb0ef41Sopenharmony_ci      const stdio = this.opts.foregroundScripts ? 'inherit' : 'pipe'
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci      // hide the banner if silent opt is passed in, or if prepare running
451cb0ef41Sopenharmony_ci      // in the background.
461cb0ef41Sopenharmony_ci      const banner = this.opts.silent ? false : stdio === 'inherit'
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci      return runScript({
491cb0ef41Sopenharmony_ci        pkg: mani,
501cb0ef41Sopenharmony_ci        event: 'prepare',
511cb0ef41Sopenharmony_ci        path: this.resolved,
521cb0ef41Sopenharmony_ci        stdio,
531cb0ef41Sopenharmony_ci        banner,
541cb0ef41Sopenharmony_ci        env: {
551cb0ef41Sopenharmony_ci          npm_package_resolved: this.resolved,
561cb0ef41Sopenharmony_ci          npm_package_integrity: this.integrity,
571cb0ef41Sopenharmony_ci          npm_package_json: resolve(this.resolved, 'package.json'),
581cb0ef41Sopenharmony_ci        },
591cb0ef41Sopenharmony_ci      })
601cb0ef41Sopenharmony_ci    })
611cb0ef41Sopenharmony_ci  }
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  [_tarballFromResolved] () {
641cb0ef41Sopenharmony_ci    if (!this.tree && !this.Arborist) {
651cb0ef41Sopenharmony_ci      throw new Error('DirFetcher requires either a tree or an Arborist constructor to pack')
661cb0ef41Sopenharmony_ci    }
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci    const stream = new Minipass()
691cb0ef41Sopenharmony_ci    stream.resolved = this.resolved
701cb0ef41Sopenharmony_ci    stream.integrity = this.integrity
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci    const { prefix, workspaces } = this.opts
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci    // run the prepare script, get the list of files, and tar it up
751cb0ef41Sopenharmony_ci    // pipe to the stream, and proxy errors the chain.
761cb0ef41Sopenharmony_ci    this[_prepareDir]()
771cb0ef41Sopenharmony_ci      .then(async () => {
781cb0ef41Sopenharmony_ci        if (!this.tree) {
791cb0ef41Sopenharmony_ci          const arb = new this.Arborist({ path: this.resolved })
801cb0ef41Sopenharmony_ci          this.tree = await arb.loadActual()
811cb0ef41Sopenharmony_ci        }
821cb0ef41Sopenharmony_ci        return packlist(this.tree, { path: this.resolved, prefix, workspaces })
831cb0ef41Sopenharmony_ci      })
841cb0ef41Sopenharmony_ci      .then(files => tar.c(tarCreateOptions(this.package), files)
851cb0ef41Sopenharmony_ci        .on('error', er => stream.emit('error', er)).pipe(stream))
861cb0ef41Sopenharmony_ci      .catch(er => stream.emit('error', er))
871cb0ef41Sopenharmony_ci    return stream
881cb0ef41Sopenharmony_ci  }
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci  manifest () {
911cb0ef41Sopenharmony_ci    if (this.package) {
921cb0ef41Sopenharmony_ci      return Promise.resolve(this.package)
931cb0ef41Sopenharmony_ci    }
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci    return this[_readPackageJson](this.resolved + '/package.json')
961cb0ef41Sopenharmony_ci      .then(mani => this.package = {
971cb0ef41Sopenharmony_ci        ...mani,
981cb0ef41Sopenharmony_ci        _integrity: this.integrity && String(this.integrity),
991cb0ef41Sopenharmony_ci        _resolved: this.resolved,
1001cb0ef41Sopenharmony_ci        _from: this.from,
1011cb0ef41Sopenharmony_ci      })
1021cb0ef41Sopenharmony_ci  }
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci  packument () {
1051cb0ef41Sopenharmony_ci    return FileFetcher.prototype.packument.apply(this)
1061cb0ef41Sopenharmony_ci  }
1071cb0ef41Sopenharmony_ci}
1081cb0ef41Sopenharmony_cimodule.exports = DirFetcher
109