From 4884821f637ca1992b494fbdbd94d000e4428a40 Mon Sep 17 00:00:00 2001 From: nlf Date: Tue, 8 Feb 2022 12:24:11 -0800 Subject: [PATCH] feat(libnpmpack): write tarball file when dryRun === false --- workspaces/libnpmpack/lib/index.js | 11 ++++++++ workspaces/libnpmpack/test/index.js | 39 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/workspaces/libnpmpack/lib/index.js b/workspaces/libnpmpack/lib/index.js index 23bb9df4b2247..a2c95cf938dcf 100644 --- a/workspaces/libnpmpack/lib/index.js +++ b/workspaces/libnpmpack/lib/index.js @@ -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 = {}) { @@ -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({ diff --git a/workspaces/libnpmpack/test/index.js b/workspaces/libnpmpack/test/index.js index dcc5e41f4b509..5f25f416655fd 100644 --- a/workspaces/libnpmpack/test/index.js +++ b/workspaces/libnpmpack/test/index.js @@ -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') @@ -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({