Skip to content

Commit

Permalink
feat: publish only from local directory
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiahdz committed Mar 27, 2020
1 parent 61b0da7 commit bc823ce
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 241 deletions.
107 changes: 4 additions & 103 deletions lib/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ module.exports = pack

const BB = require('bluebird')

const byteSize = require('byte-size')
const cacache = require('cacache')
const columnify = require('columnify')
const cp = require('child_process')
const deprCheck = require('./utils/depr-check')
const fpm = require('./fetch-package-metadata')
const fs = require('graceful-fs')
const install = require('./install')
const lifecycle = BB.promisify(require('./utils/lifecycle'))
const { getTarContents, logTarContents } = ('/.utils/tar-contents')
const log = require('npmlog')
const move = require('move-concurrently')
const npm = require('./npm')
Expand All @@ -33,7 +32,6 @@ const pinflight = require('promise-inflight')
const readJson = BB.promisify(require('read-package-json'))
const tar = require('tar')
const packlist = require('npm-packlist')
const ssri = require('ssri')

pack.usage = 'npm pack [[<@scope>/]<pkg>...] [--dry-run]'

Expand All @@ -55,7 +53,7 @@ function pack (args, silent, cb) {
if (!silent && npm.config.get('json')) {
output(JSON.stringify(tarballs, null, 2))
} else if (!silent) {
tarballs.forEach(logContents)
tarballs.forEach(logTarContents)
output(tarballs.map((f) => path.relative(cwd, f.filename)).join('\n'))
}
return tarballs
Expand Down Expand Up @@ -97,7 +95,7 @@ function packFromPackage (arg, target, filename) {
return pacote.extract(arg, tmpTarget, opts)
.then(() => readJson(path.join(tmpTarget, 'package.json')))
}))
.then((pkg) => getContents(pkg, target, filename))
.then((pkg) => getTarContents(pkg, target, filename))
}

module.exports.prepareDirectory = prepareDirectory
Expand Down Expand Up @@ -156,7 +154,7 @@ function packDirectory (mani, dir, target, filename, logIt, dryRun) {
// specifically with @ signs, so we just neutralize that one
// and any such future "features" by prepending `./`
.then((files) => tar.create(tarOpt, files.map((f) => `./${f}`)))
.then(() => getContents(pkg, tmpTarget, filename, logIt))
.then(() => getTarContents(pkg, tmpTarget, filename, logIt))
// thread the content info through
.tap(() => {
if (dryRun) {
Expand All @@ -170,103 +168,6 @@ function packDirectory (mani, dir, target, filename, logIt, dryRun) {
})
}

module.exports.logContents = logContents
function logContents (tarball) {
log.notice('')
log.notice('', `${npm.config.get('unicode') ? '📦 ' : 'package:'} ${tarball.name}@${tarball.version}`)
log.notice('=== Tarball Contents ===')
if (tarball.files.length) {
log.notice('', columnify(tarball.files.map((f) => {
const bytes = byteSize(f.size)
return {path: f.path, size: `${bytes.value}${bytes.unit}`}
}), {
include: ['size', 'path'],
showHeaders: false
}))
}
if (tarball.bundled.length) {
log.notice('=== Bundled Dependencies ===')
tarball.bundled.forEach((name) => log.notice('', name))
}
log.notice('=== Tarball Details ===')
log.notice('', columnify([
{name: 'name:', value: tarball.name},
{name: 'version:', value: tarball.version},
tarball.filename && {name: 'filename:', value: tarball.filename},
{name: 'package size:', value: byteSize(tarball.size)},
{name: 'unpacked size:', value: byteSize(tarball.unpackedSize)},
{name: 'shasum:', value: tarball.shasum},
{
name: 'integrity:',
value: tarball.integrity.toString().substr(0, 20) + '[...]' + tarball.integrity.toString().substr(80)},
tarball.bundled.length && {name: 'bundled deps:', value: tarball.bundled.length},
tarball.bundled.length && {name: 'bundled files:', value: tarball.entryCount - tarball.files.length},
tarball.bundled.length && {name: 'own files:', value: tarball.files.length},
{name: 'total files:', value: tarball.entryCount}
].filter((x) => x), {
include: ['name', 'value'],
showHeaders: false
}))
log.notice('', '')
}

module.exports.getContents = getContents
function getContents (pkg, target, filename, silent) {
const bundledWanted = new Set(
pkg.bundleDependencies ||
pkg.bundledDependencies ||
[]
)
const files = []
const bundled = new Set()
let totalEntries = 0
let totalEntrySize = 0
return tar.t({
file: target,
onentry (entry) {
totalEntries++
totalEntrySize += entry.size
const p = entry.path
if (p.startsWith('package/node_modules/')) {
const name = p.match(/^package\/node_modules\/((?:@[^/]+\/)?[^/]+)/)[1]
if (bundledWanted.has(name)) {
bundled.add(name)
}
} else {
files.push({
path: entry.path.replace(/^package\//, ''),
size: entry.size,
mode: entry.mode
})
}
},
strip: 1
})
.then(() => BB.all([
BB.fromNode((cb) => fs.stat(target, cb)),
ssri.fromStream(fs.createReadStream(target), {
algorithms: ['sha1', 'sha512']
})
]))
.then(([stat, integrity]) => {
const shasum = integrity['sha1'][0].hexDigest()
return {
id: pkg._id,
name: pkg.name,
version: pkg.version,
from: pkg._from,
size: stat.size,
unpackedSize: totalEntrySize,
shasum,
integrity: ssri.parse(integrity['sha512'][0]),
filename,
files,
entryCount: totalEntries,
bundled: Array.from(bundled)
}
})
}

const PASSTHROUGH_OPTS = [
'always-auth',
'auth-type',
Expand Down
Loading

0 comments on commit bc823ce

Please sign in to comment.