From cdbf7485cb6e60f3d4fb3b4be495a9c2a04eb43b Mon Sep 17 00:00:00 2001 From: Lars Willighagen Date: Tue, 14 Aug 2018 15:33:49 +0200 Subject: [PATCH 1/3] pack: add dryRun option to packDirectory Add dryRun option to packDirectory. This makes it possible to run postscript after the archive has been written to cwd. See https://npm.community/t/592?u=larsgw --- lib/pack.js | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/pack.js b/lib/pack.js index 93c21ad55971c..83b7dea4a9548 100644 --- a/lib/pack.js +++ b/lib/pack.js @@ -68,23 +68,16 @@ function pack_ (pkg, dir) { : mani.name const target = `${name}-${mani.version}.tgz` return pinflight(target, () => { - 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')) { + const dryRun = npm.config.get('dry-run') + if (dryRun) { log.verbose('pack', '--dry-run mode enabled. Skipping write.') + } + if (mani._requested.type === 'directory') { + return prepareDirectory(mani._resolved) + .then(() => { + return packDirectory(mani, mani._resolved, target, target, true, dryRun) + }) + } else if (dryRun) { return cacache.tmp.withTmp(npm.tmp, {tmpPrefix: 'packing'}, (tmp) => { const tmpTarget = path.join(tmp, path.basename(target)) return packFromPackage(pkg, tmpTarget, target) @@ -137,7 +130,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) @@ -165,7 +158,11 @@ 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) { + return move(tmpTarget, target, {Promise: BB, fs}) + } + }) .tap(() => lifecycle(pkg, 'postpack', dir)) }) }) From e740633025912473101d702c3597f30841f42b6d Mon Sep 17 00:00:00 2001 From: Lars Willighagen Date: Tue, 14 Aug 2018 15:39:18 +0200 Subject: [PATCH 2/3] pack: move --dry-run logging Move --dry-run logging closer to original position. --- lib/pack.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/pack.js b/lib/pack.js index 83b7dea4a9548..48fcd420f1b60 100644 --- a/lib/pack.js +++ b/lib/pack.js @@ -69,15 +69,13 @@ function pack_ (pkg, dir) { const target = `${name}-${mani.version}.tgz` return pinflight(target, () => { const dryRun = npm.config.get('dry-run') - if (dryRun) { - log.verbose('pack', '--dry-run mode enabled. Skipping write.') - } if (mani._requested.type === 'directory') { 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)) return packFromPackage(pkg, tmpTarget, target) @@ -159,7 +157,9 @@ function packDirectory (mani, dir, target, filename, logIt, dryRun) { .then(() => getContents(pkg, tmpTarget, filename, logIt)) // thread the content info through .tap(() => { - if (!dryRun) { + if (dryRun) { + log.verbose('pack', '--dry-run mode enabled. Skipping write.') + } else { return move(tmpTarget, target, {Promise: BB, fs}) } }) From faa7e10d1ec29daac97698c835bc31dda23da586 Mon Sep 17 00:00:00 2001 From: Lars Willighagen Date: Wed, 15 Aug 2018 23:55:38 +0200 Subject: [PATCH 3/3] pack: add postpack test --- test/tap/pack.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/tap/pack.js b/test/tap/pack.js index a0c326210ea6c..1813f47bf46a5 100644 --- a/test/tap/pack.js +++ b/test/tap/pack.js @@ -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)) +})