Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Adding ignore globs to fsadd. #502

Merged
merged 2 commits into from
Jan 10, 2017
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
test/setup/tmp-disposable-nodes-addrs.json
dist
coverage
**/*.swp
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ Complete documentation for these methods is coming with: https://github.com/ipfs

> `ipfs.util.addFromFs(path, option, callback)`

Reads a file from `path` on the filesystem and adds it to IPFS. If `path` is a directory, use option `{ recursive: true }` to add the directory and all its sub-directories.
Reads a file from `path` on the filesystem and adds it to IPFS. If `path` is a directory, use option `{ recursive: true }` to add the directory and all its sub-directories. To exclude fileglobs from the directory, use option `{ ignore: ['ignore/this/folder/**', 'and/this/file'] }`.
Copy link
Contributor

Choose a reason for hiding this comment

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

nice 👍


```JavaScript
ipfs.util.addFromFs('path/to/a/file', { recursive: true }, (err, result) => {
ipfs.util.addFromFs('path/to/a/folder', { recursive: true , ignore: ['subfolder/to/ignore/**']}, (err, result) => {
if (err) {
throw err
}
Expand Down
10 changes: 7 additions & 3 deletions src/get-files-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ function loadPaths (opts, file) {
}

if (stats.isDirectory() && opts.recursive) {
const mg = new glob.sync.GlobSync(`${escape(file)}/**/*`, {
follow: followSymlinks
const globEscapedDir = escape(file) + (file.endsWith('/') ? '' : '/')
const mg = new glob.sync.GlobSync(`${globEscapedDir}**/*`, {
follow: followSymlinks,
ignore: (opts.ignore || []).map(function (ignoreGlob) {
return globEscapedDir + ignoreGlob
})
})

return mg.found
Expand Down Expand Up @@ -88,7 +92,7 @@ function loadPaths (opts, file) {
}

return {
path: file,
path: path.basename(file),
content: fs.createReadStream(file)
}
}
Expand Down
12 changes: 11 additions & 1 deletion test/ipfs-api/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,21 @@ describe('.util', () => {
})
})

it('.fsAdd add and ignore a directory', (done) => {
const filesPath = path.join(__dirname, '../fixtures/test-folder')
ipfs.util.addFromFs(filesPath, { recursive: true, ignore: ['files/**'] }, (err, result) => {
expect(err).to.not.exist
expect(result.length).to.be.below(9)
done()
})
})

it('.fsAdd a file', (done) => {
const filePath = path.join(__dirname, '../fixtures/testfile.txt')
ipfs.util.addFromFs(filePath, (err, result) => {
expect(err).to.not.exist
expect(result.length).to.be.above(5)
expect(result.length).to.be.equal(1)
expect(result[0].path).to.be.equal('testfile.txt')
done()
})
})
Expand Down