Skip to content

Commit

Permalink
pack: add dryRun option to packDirectory (#47)
Browse files Browse the repository at this point in the history
Add dryRun option to packDirectory. This makes it possible to run postscript after the archive has been written to cwd.

Fixes: https://npm.community/t/592
PR-URL: #47
Credit: @larsgw
Reviewed-By: @zkat
  • Loading branch information
larsgw authored and zkat committed Aug 20, 2018
1 parent d8e811d commit 05ab727
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
31 changes: 14 additions & 17 deletions lib/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,13 @@ function pack_ (pkg, dir) {
: mani.name
const target = `${name}-${mani.version}.tgz`
return pinflight(target, () => {
const dryRun = npm.config.get('dry-run')
if (mani._requested.type === 'directory') {
return cacache.tmp.withTmp(npm.tmp, {tmpPrefix: 'packing'}, (tmp) => {
const tmpTarget = path.join(tmp, path.basename(target))
return prepareDirectory(mani._resolved)
.then(() => {
return packDirectory(mani, mani._resolved, tmpTarget, target, true)
})
.tap(() => {
if (npm.config.get('dry-run')) {
log.verbose('pack', '--dry-run mode enabled. Skipping write.')
} else {
return move(tmpTarget, target, {Promise: BB, fs})
}
})
})
} else if (npm.config.get('dry-run')) {
return prepareDirectory(mani._resolved)
.then(() => {
return packDirectory(mani, mani._resolved, target, target, true, dryRun)
})
} else if (dryRun) {
log.verbose('pack', '--dry-run mode enabled. Skipping write.')
return cacache.tmp.withTmp(npm.tmp, {tmpPrefix: 'packing'}, (tmp) => {
const tmpTarget = path.join(tmp, path.basename(target))
Expand Down Expand Up @@ -137,7 +128,7 @@ function prepareDirectory (dir) {
}

module.exports.packDirectory = packDirectory
function packDirectory (mani, dir, target, filename, logIt) {
function packDirectory (mani, dir, target, filename, logIt, dryRun) {
deprCheck(mani)
return readJson(path.join(dir, 'package.json')).then((pkg) => {
return lifecycle(pkg, 'prepack', dir)
Expand Down Expand Up @@ -165,7 +156,13 @@ function packDirectory (mani, dir, target, filename, logIt) {
.then((files) => tar.create(tarOpt, files.map((f) => `./${f}`)))
.then(() => getContents(pkg, tmpTarget, filename, logIt))
// thread the content info through
.tap(() => move(tmpTarget, target, {Promise: BB, fs}))
.tap(() => {
if (dryRun) {
log.verbose('pack', '--dry-run mode enabled. Skipping write.')
} else {
return move(tmpTarget, target, {Promise: BB, fs})
}
})
.tap(() => lifecycle(pkg, 'postpack', dir))
})
})
Expand Down
32 changes: 32 additions & 0 deletions test/tap/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,35 @@ test('pack --json', (t) => {
})
.then(() => rimraf(testDir))
})

test('postpack', (t) => {
const fixture = new Tacks(new Dir({
'package.json': new File({
name: 'generic-package',
version: '90000.100001.5',
scripts: {
postpack: 'node -e "var fs = require(\'fs\'); fs.openSync(\'postpack-step\', \'w+\'); if (!fs.existsSync(\'generic-package-90000.100001.5.tgz\')) { throw new Error(\'tar archive does not exist on postpack\') }"'
}
})
}))

return rimraf(testDir)
.then(() => fixture.create(testDir))
.then(() => common.npm([
'pack',
'--loglevel', 'notice',
'--cache', cache,
'--tmp', tmp,
'--prefix', testDir,
'--no-global'
], {
cwd: testDir
}))
.spread((code, stdout, stderr) => {
t.equal(code, 0, 'npm pack exited ok')
return fs.statAsync(
path.join(testDir, 'postpack-step')
)
})
.then(() => rimraf(testDir))
})

0 comments on commit 05ab727

Please sign in to comment.