Skip to content

Commit

Permalink
feat(libnpmpack): write tarball file when dryRun === false
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf committed Feb 8, 2022
1 parent f3fbeea commit 4884821
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
11 changes: 11 additions & 0 deletions workspaces/libnpmpack/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
const pacote = require('pacote')
const npa = require('npm-package-arg')
const runScript = require('@npmcli/run-script')
const path = require('path')
const util = require('util')
const writeFile = util.promisify(require('fs').writeFile)

module.exports = pack
async function pack (spec = 'file:.', opts = {}) {
Expand Down Expand Up @@ -33,6 +36,14 @@ async function pack (spec = 'file:.', opts = {}) {
integrity: manifest._integrity,
})

// check for explicit `false` so the default behavior is to skip writing to disk
if (opts.dryRun === false) {
const filename = `${manifest.name}-${manifest.version}.tgz`
.replace(/^@/, '').replace(/\//, '-')
const destination = path.resolve(opts.packDestination, filename)
await writeFile(destination, tarball)
}

if (spec.type === 'directory') {
// postpack
await runScript({
Expand Down
39 changes: 39 additions & 0 deletions workspaces/libnpmpack/test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict'

const t = require('tap')
const fs = require('fs')
const path = require('path')
const pack = require('../lib/index.js')
const tnock = require('./fixtures/tnock.js')

Expand Down Expand Up @@ -29,6 +31,43 @@ t.test('packs from local directory', async t => {
})
})

t.test('writes tarball to file when dryRun === false', async t => {
const testDir = t.testdir({
'package.json': JSON.stringify({
name: 'my-cool-pkg',
version: '1.0.0',
scripts: {
prepack: 'touch prepack',
postpack: 'touch postpack',
},
}, null, 2),
})

const cwd = process.cwd()
process.chdir(testDir)

const tarball = await pack('file:.', {
dryRun: false,
packDestination: testDir,
log: { level: 'silent' }, // so the test doesn't try to log
})
t.ok(tarball)
const expectedTarball = path.join(testDir, 'my-cool-pkg-1.0.0.tgz')
t.ok(fs.existsSync(expectedTarball), 'file was written')
t.same(fs.readFileSync(expectedTarball), tarball, 'wrote same data that was returned')

const prepackTimestamp = (await fs.promises.stat(path.join(testDir, 'prepack'))).mtime
const tarballTimestamp = (await fs.promises.stat(expectedTarball)).mtime
const postpackTimestamp = (await fs.promises.stat(path.join(testDir, 'postpack'))).mtime

t.ok(prepackTimestamp < tarballTimestamp, 'prepack ran before tarball was written')
t.ok(tarballTimestamp < postpackTimestamp, 'postpack ran after tarball was written')

t.teardown(async () => {
process.chdir(cwd)
})
})

t.test('packs from local directory with silent loglevel', async t => {
const testDir = t.testdir({
'package.json': JSON.stringify({
Expand Down

0 comments on commit 4884821

Please sign in to comment.