Skip to content

Commit 1bc5b8c

Browse files
larsgwzkat
authored andcommitted
pack: add dryRun option to packDirectory (#47)
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
1 parent d8e811d commit 1bc5b8c

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

Diff for: lib/pack.js

+14-17
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,13 @@ function pack_ (pkg, dir) {
6868
: mani.name
6969
const target = `${name}-${mani.version}.tgz`
7070
return pinflight(target, () => {
71+
const dryRun = npm.config.get('dry-run')
7172
if (mani._requested.type === 'directory') {
72-
return cacache.tmp.withTmp(npm.tmp, {tmpPrefix: 'packing'}, (tmp) => {
73-
const tmpTarget = path.join(tmp, path.basename(target))
74-
return prepareDirectory(mani._resolved)
75-
.then(() => {
76-
return packDirectory(mani, mani._resolved, tmpTarget, target, true)
77-
})
78-
.tap(() => {
79-
if (npm.config.get('dry-run')) {
80-
log.verbose('pack', '--dry-run mode enabled. Skipping write.')
81-
} else {
82-
return move(tmpTarget, target, {Promise: BB, fs})
83-
}
84-
})
85-
})
86-
} else if (npm.config.get('dry-run')) {
73+
return prepareDirectory(mani._resolved)
74+
.then(() => {
75+
return packDirectory(mani, mani._resolved, target, target, true, dryRun)
76+
})
77+
} else if (dryRun) {
8778
log.verbose('pack', '--dry-run mode enabled. Skipping write.')
8879
return cacache.tmp.withTmp(npm.tmp, {tmpPrefix: 'packing'}, (tmp) => {
8980
const tmpTarget = path.join(tmp, path.basename(target))
@@ -137,7 +128,7 @@ function prepareDirectory (dir) {
137128
}
138129

139130
module.exports.packDirectory = packDirectory
140-
function packDirectory (mani, dir, target, filename, logIt) {
131+
function packDirectory (mani, dir, target, filename, logIt, dryRun) {
141132
deprCheck(mani)
142133
return readJson(path.join(dir, 'package.json')).then((pkg) => {
143134
return lifecycle(pkg, 'prepack', dir)
@@ -165,7 +156,13 @@ function packDirectory (mani, dir, target, filename, logIt) {
165156
.then((files) => tar.create(tarOpt, files.map((f) => `./${f}`)))
166157
.then(() => getContents(pkg, tmpTarget, filename, logIt))
167158
// thread the content info through
168-
.tap(() => move(tmpTarget, target, {Promise: BB, fs}))
159+
.tap(() => {
160+
if (dryRun) {
161+
log.verbose('pack', '--dry-run mode enabled. Skipping write.')
162+
} else {
163+
return move(tmpTarget, target, {Promise: BB, fs})
164+
}
165+
})
169166
.tap(() => lifecycle(pkg, 'postpack', dir))
170167
})
171168
})

Diff for: test/tap/pack.js

+32
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,35 @@ test('pack --json', (t) => {
165165
})
166166
.then(() => rimraf(testDir))
167167
})
168+
169+
test('postpack', (t) => {
170+
const fixture = new Tacks(new Dir({
171+
'package.json': new File({
172+
name: 'generic-package',
173+
version: '90000.100001.5',
174+
scripts: {
175+
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\') }"'
176+
}
177+
})
178+
}))
179+
180+
return rimraf(testDir)
181+
.then(() => fixture.create(testDir))
182+
.then(() => common.npm([
183+
'pack',
184+
'--loglevel', 'notice',
185+
'--cache', cache,
186+
'--tmp', tmp,
187+
'--prefix', testDir,
188+
'--no-global'
189+
], {
190+
cwd: testDir
191+
}))
192+
.spread((code, stdout, stderr) => {
193+
t.equal(code, 0, 'npm pack exited ok')
194+
return fs.statAsync(
195+
path.join(testDir, 'postpack-step')
196+
)
197+
})
198+
.then(() => rimraf(testDir))
199+
})

0 commit comments

Comments
 (0)