codekingpro/portable-devtools
114k
1'use strict'
2
3const pacote = require('pacote')
4const npa = require('npm-package-arg')
5const runScript = require('@npmcli/run-script')
6const path = require('node:path')
7const Arborist = require('@npmcli/arborist')
8const { writeFile } = require('node:fs/promises')
9
10module.exports = pack
11async function pack (spec = 'file:.', opts = {}) {
12 // gets spec
13 spec = npa(spec)
14
15 const manifest = await pacote.manifest(spec, { ...opts, Arborist, _isRoot: true })
16
17 const stdio = opts.foregroundScripts ? 'inherit' : 'pipe'
18
19 if (spec.type === 'directory' && !opts.ignoreScripts) {
20 // prepack
21 await runScript({
22 ...opts,
23 event: 'prepack',
24 path: spec.fetchSpec,
25 stdio,
26 pkg: manifest,
27 })
28 }
29
30 // packs tarball
31 const tarball = await pacote.tarball(manifest._resolved, {
32 ...opts,
33 Arborist,
34 integrity: manifest._integrity,
35 })
36
37 // check for explicit `false` so the default behavior is to skip writing to disk
38 if (opts.dryRun === false) {
39 const filename = `${manifest.name}-${manifest.version}.tgz`
40 .replace(/^@/, '').replace(/\//, '-')
41 const destination = path.resolve(opts.packDestination, filename)
42 await writeFile(destination, tarball)
43 }
44
45 if (spec.type === 'directory' && !opts.ignoreScripts) {
46 // postpack
47 await runScript({
48 ...opts,
49 event: 'postpack',
50 path: spec.fetchSpec,
51 stdio,
52 pkg: manifest,
53 env: {
54 npm_package_from: tarball.from,
55 npm_package_resolved: tarball.resolved,
56 npm_package_integrity: tarball.integrity,
57 },
58 })
59 }
60
61 return tarball
62}
63 