Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion cmd/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Comment on lines +27 to +30

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let fullOutputPath
if (opts.output) {
fullOutputPath = path.resolve(opts.output)
}
const fullOutputPath = opts.output !== null ? path.resolve(opts.output) : null;
  • stricter check
  • remove unnecessary modifiableness

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced this will work. It's worth checking but I don't think opts.output is ever null. It might be undefined or the empty string.

const files = paths.length
? await filesFromPaths(paths, { hidden })
? await filesFromPaths(paths, {
hidden,
filter: (filePath) => {
const include = !fullOutputPath || filePath !== fullOutputPath

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const include = !fullOutputPath || filePath !== fullOutputPath
const include = fullOutputPath !== null || filePath !== fullOutputPath
  • stricter check

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])
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 22 additions & 0 deletions test/bin.node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'], {
Expand Down