diff --git a/cmd/pack.js b/cmd/pack.js index 8fd5877..72013f5 100644 --- a/cmd/pack.js +++ b/cmd/pack.js @@ -24,8 +24,21 @@ export default async function pack (filePath, opts = {}) { const rest = opts._ ?? [] const paths = checkPathsExist([filePath, ...rest].filter(Boolean)) const hidden = !!opts.hidden + let fullOutputPath + if (opts.output) { + fullOutputPath = path.resolve(opts.output) + } const files = paths.length - ? await filesFromPaths(paths, { hidden }) + ? await filesFromPaths(paths, { + hidden, + filter: (filePath) => { + const include = !fullOutputPath || filePath !== fullOutputPath + if (!include) { + console.log('A version of the output CAR file exists in the source path(s). It will not be included in the new output CAR file.') + } + return include + } + }) : /** @type {import('../types').FileLike[]} */ ([{ name: 'stdin', stream: () => Readable.toWeb(process.stdin) }]) const blockStream = files.length === 1 && (files[0].name === 'stdin' || !opts.wrap) ? UnixFS.createFileEncoderStream(files[0]) diff --git a/package-lock.json b/package-lock.json index bfe8bb5..e1e8412 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,7 @@ "@ipld/dag-pb": "^4.0.2", "@ipld/unixfs": "^3.0.0", "@web3-storage/car-block-validator": "^1.0.1", - "files-from-path": "^1.0.0", + "files-from-path": "^1.1.5-rc.1", "ipfs-unixfs-exporter": "^13.0.1", "multiformats": "^13.0.1", "sade": "^1.8.1", @@ -2679,9 +2679,9 @@ } }, "node_modules/files-from-path": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/files-from-path/-/files-from-path-1.0.0.tgz", - "integrity": "sha512-EobUbrzh1fPOZpQvDdTikGpCs+ZDcTNyBOnFuHvW2BQXEkMSPbEPQ0eVTQrz0oHlBcPS9Lnw+uPzACfft1sDYg==", + "version": "1.1.5-rc.1", + "resolved": "https://registry.npmjs.org/files-from-path/-/files-from-path-1.1.5-rc.1.tgz", + "integrity": "sha512-BfaE1Vyl03WdY7rIo9XADadtv4MigWZNczZApZjmlct7RlnGbvCtpQXs3zUOqhgAuPSuf+oTTNyo3FToIn5VxQ==", "dependencies": { "graceful-fs": "^4.2.10" }, diff --git a/package.json b/package.json index 67c64eb..630b5b3 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "@ipld/dag-pb": "^4.0.2", "@ipld/unixfs": "^3.0.0", "@web3-storage/car-block-validator": "^1.0.1", - "files-from-path": "^1.0.0", + "files-from-path": "^1.1.5-rc.1", "ipfs-unixfs-exporter": "^13.0.1", "multiformats": "^13.0.1", "sade": "^1.8.1", diff --git a/test/bin.node.test.js b/test/bin.node.test.js index a8a0917..04215e0 100644 --- a/test/bin.node.test.js +++ b/test/bin.node.test.js @@ -70,6 +70,28 @@ describe('CLI', function () { ) }) + it('pack a path that includes the output, skipping the output file', async () => { + const filePath = './test/fixtures/comic/pinpie.jpg' + const packPath = tmpPath() + let res = execaSync(binPath, ['pack', filePath, '--output', packPath]) + + let root = res.stdout.trim() + assert.equal(root, 'bafybeiajdopsmspomlrpaohtzo5sdnpknbolqjpde6huzrsejqmvijrcea') + + res = execaSync(binPath, ['pack', filePath, packPath, '--output', packPath]) + + root = res.stdout.trim() + assert.equal(root, 'A version of the output CAR file exists in the source path(s). It will not be included in the new output CAR file.\nbafybeiajdopsmspomlrpaohtzo5sdnpknbolqjpde6huzrsejqmvijrcea') + + const unpackPath = tmpPath() + execaSync(binPath, ['unpack', packPath, '--output', unpackPath]) + + assert.deepEqual( + await fs.promises.readFile(path.join(unpackPath, 'pinpie.jpg')), + await fs.promises.readFile(filePath) + ) + }) + it('stdin | pack | unpack | stdout a file', async () => { const filePath = './test/fixtures/comic/pinpie.jpg' const res0 = execaSync(binPath, ['pack', '--no-wrap'], {