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

Commit bcec644

Browse files
JeffDowniedaviddias
authored andcommitted
Adding ignore globs to fsadd. (#502)
* Adding ignore globs to fsadd. Also fixed #408 whilst in the area - `util.addFromFs` goes up almost to the root directory for files. It looks like for directories it was fixed in 42ccb00, but it is now fixed for files too. * Linting fixes - see pull request #502
1 parent b1b2d39 commit bcec644

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
test/setup/tmp-disposable-nodes-addrs.json
44
dist
55
coverage
6+
**/*.swp

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ Complete documentation for these methods is coming with: https://github.com/ipfs
150150

151151
> `ipfs.util.addFromFs(path, option, callback)`
152152
153-
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.
153+
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'] }`.
154154

155155
```JavaScript
156-
ipfs.util.addFromFs('path/to/a/file', { recursive: true }, (err, result) => {
156+
ipfs.util.addFromFs('path/to/a/folder', { recursive: true , ignore: ['subfolder/to/ignore/**']}, (err, result) => {
157157
if (err) {
158158
throw err
159159
}

src/get-files-stream.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ function loadPaths (opts, file) {
4545
}
4646

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

5256
return mg.found
@@ -88,7 +92,7 @@ function loadPaths (opts, file) {
8892
}
8993

9094
return {
91-
path: file,
95+
path: path.basename(file),
9296
content: fs.createReadStream(file)
9397
}
9498
}

test/ipfs-api/util.spec.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,21 @@ describe('.util', () => {
6060
})
6161
})
6262

63+
it('.fsAdd add and ignore a directory', (done) => {
64+
const filesPath = path.join(__dirname, '../fixtures/test-folder')
65+
ipfs.util.addFromFs(filesPath, { recursive: true, ignore: ['files/**'] }, (err, result) => {
66+
expect(err).to.not.exist
67+
expect(result.length).to.be.below(9)
68+
done()
69+
})
70+
})
71+
6372
it('.fsAdd a file', (done) => {
6473
const filePath = path.join(__dirname, '../fixtures/testfile.txt')
6574
ipfs.util.addFromFs(filePath, (err, result) => {
6675
expect(err).to.not.exist
67-
expect(result.length).to.be.above(5)
76+
expect(result.length).to.be.equal(1)
77+
expect(result[0].path).to.be.equal('testfile.txt')
6878
done()
6979
})
7080
})

0 commit comments

Comments
 (0)