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

Commit eae6e1a

Browse files
JeffDowniedaviddias
authored andcommitted
Adding option for dotfiles in addFromFs (#504)
* Adding option for dotfiles in addFromFs. * Adding changes for code review. * Changed `dot` option to `hidden` to match go-ipfs * Asserted hidden file hash in test for safety
1 parent 0c4bee0 commit eae6e1a

File tree

4 files changed

+47
-29
lines changed

4 files changed

+47
-29
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +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. To exclude fileglobs from the directory, use option `{ ignore: ['ignore/this/folder/**', 'and/this/file'] }`.
153+
Reads a file or folder from `path` on the filesystem and adds it to IPFS. Options:
154+
- **recursive**: If `path` is a directory, use option `{ recursive: true }` to add the directory and all its sub-directories.
155+
- **ignore**: To exclude fileglobs from the directory, use option `{ ignore: ['ignore/this/folder/**', 'and/this/file'] }`.
156+
- **hidden**: hidden/dot files (files or folders starting with a `.`, for example, `.git/`) are not included by default. To add them, use the option `{ hidden: true }`.
154157

155158
```JavaScript
156159
ipfs.util.addFromFs('path/to/a/folder', { recursive: true , ignore: ['subfolder/to/ignore/**']}, (err, result) => {

src/get-files-stream.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function loadPaths (opts, file) {
4848
const globEscapedDir = escape(file) + (file.endsWith('/') ? '' : '/')
4949
const mg = new glob.sync.GlobSync(`${globEscapedDir}**/*`, {
5050
follow: followSymlinks,
51+
dot: opts.hidden,
5152
ignore: (opts.ignore || []).map(function (ignoreGlob) {
5253
return globEscapedDir + ignoreGlob
5354
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Aha! You found me!

test/ipfs-api/util.spec.js

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,40 +42,53 @@ describe('.util', () => {
4242
})
4343
})
4444

45-
it('.fsAdd a directory', (done) => {
46-
const filesPath = path.join(__dirname, '../fixtures/test-folder')
47-
ipfs.util.addFromFs(filesPath, { recursive: true }, (err, result) => {
48-
expect(err).to.not.exist
49-
expect(result.length).to.be.above(8)
50-
done()
45+
describe('.fsAdd should add', () => {
46+
it('a directory', (done) => {
47+
const filesPath = path.join(__dirname, '../fixtures/test-folder')
48+
ipfs.util.addFromFs(filesPath, { recursive: true }, (err, result) => {
49+
expect(err).to.not.exist
50+
expect(result.length).to.be.above(8)
51+
done()
52+
})
5153
})
52-
})
5354

54-
it('.fsAdd a directory with an odd name', (done) => {
55-
const filesPath = path.join(__dirname, '../fixtures/weird name folder [v0]')
56-
ipfs.util.addFromFs(filesPath, { recursive: true }, (err, result) => {
57-
expect(err).to.not.exist
58-
expect(result.length).to.be.above(8)
59-
done()
55+
it('a directory with an odd name', (done) => {
56+
const filesPath = path.join(__dirname, '../fixtures/weird name folder [v0]')
57+
ipfs.util.addFromFs(filesPath, { recursive: true }, (err, result) => {
58+
expect(err).to.not.exist
59+
expect(result.length).to.be.above(8)
60+
done()
61+
})
6062
})
61-
})
6263

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()
64+
it('add and ignore a directory', (done) => {
65+
const filesPath = path.join(__dirname, '../fixtures/test-folder')
66+
ipfs.util.addFromFs(filesPath, { recursive: true, ignore: ['files/**'] }, (err, result) => {
67+
expect(err).to.not.exist
68+
expect(result.length).to.be.below(9)
69+
done()
70+
})
6971
})
70-
})
7172

72-
it('.fsAdd a file', (done) => {
73-
const filePath = path.join(__dirname, '../fixtures/testfile.txt')
74-
ipfs.util.addFromFs(filePath, (err, result) => {
75-
expect(err).to.not.exist
76-
expect(result.length).to.be.equal(1)
77-
expect(result[0].path).to.be.equal('testfile.txt')
78-
done()
73+
it('a file', (done) => {
74+
const filePath = path.join(__dirname, '../fixtures/testfile.txt')
75+
ipfs.util.addFromFs(filePath, (err, result) => {
76+
expect(err).to.not.exist
77+
expect(result.length).to.be.equal(1)
78+
expect(result[0].path).to.be.equal('testfile.txt')
79+
done()
80+
})
81+
})
82+
83+
it('a hidden file in a directory', (done) => {
84+
const filesPath = path.join(__dirname, '../fixtures/test-folder')
85+
ipfs.util.addFromFs(filesPath, { recursive: true, hidden: true }, (err, result) => {
86+
expect(err).to.not.exist
87+
expect(result.length).to.be.above(10)
88+
expect(result.map(object => object.path)).to.include('test-folder/.hiddenTest.txt')
89+
expect(result.map(object => object.hash)).to.include('QmdbAjVmLRdpFyi8FFvjPfhTGB2cVXvWLuK7Sbt38HXrtt')
90+
done()
91+
})
7992
})
8093
})
8194

0 commit comments

Comments
 (0)