diff --git a/README.md b/README.md index f1f64e5d9..e19c97ea5 100644 --- a/README.md +++ b/README.md @@ -150,7 +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. To exclude fileglobs from the directory, use option `{ ignore: ['ignore/this/folder/**', 'and/this/file'] }`. +Reads a file or folder from `path` on the filesystem and adds it to IPFS. Options: +- **recursive**: If `path` is a directory, use option `{ recursive: true }` to add the directory and all its sub-directories. + - **ignore**: To exclude fileglobs from the directory, use option `{ ignore: ['ignore/this/folder/**', 'and/this/file'] }`. + - **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 }`. ```JavaScript ipfs.util.addFromFs('path/to/a/folder', { recursive: true , ignore: ['subfolder/to/ignore/**']}, (err, result) => { diff --git a/src/get-files-stream.js b/src/get-files-stream.js index 73e113c8c..17a65ad5e 100644 --- a/src/get-files-stream.js +++ b/src/get-files-stream.js @@ -48,6 +48,7 @@ function loadPaths (opts, file) { const globEscapedDir = escape(file) + (file.endsWith('/') ? '' : '/') const mg = new glob.sync.GlobSync(`${globEscapedDir}**/*`, { follow: followSymlinks, + dot: opts.hidden, ignore: (opts.ignore || []).map(function (ignoreGlob) { return globEscapedDir + ignoreGlob }) diff --git a/test/fixtures/test-folder/.hiddenTest.txt b/test/fixtures/test-folder/.hiddenTest.txt new file mode 100644 index 000000000..880fecc33 --- /dev/null +++ b/test/fixtures/test-folder/.hiddenTest.txt @@ -0,0 +1 @@ +Aha! You found me! diff --git a/test/ipfs-api/util.spec.js b/test/ipfs-api/util.spec.js index 3ee8619b5..374142266 100644 --- a/test/ipfs-api/util.spec.js +++ b/test/ipfs-api/util.spec.js @@ -42,40 +42,53 @@ describe('.util', () => { }) }) - it('.fsAdd a directory', (done) => { - const filesPath = path.join(__dirname, '../fixtures/test-folder') - ipfs.util.addFromFs(filesPath, { recursive: true }, (err, result) => { - expect(err).to.not.exist - expect(result.length).to.be.above(8) - done() + describe('.fsAdd should add', () => { + it('a directory', (done) => { + const filesPath = path.join(__dirname, '../fixtures/test-folder') + ipfs.util.addFromFs(filesPath, { recursive: true }, (err, result) => { + expect(err).to.not.exist + expect(result.length).to.be.above(8) + done() + }) }) - }) - it('.fsAdd a directory with an odd name', (done) => { - const filesPath = path.join(__dirname, '../fixtures/weird name folder [v0]') - ipfs.util.addFromFs(filesPath, { recursive: true }, (err, result) => { - expect(err).to.not.exist - expect(result.length).to.be.above(8) - done() + it('a directory with an odd name', (done) => { + const filesPath = path.join(__dirname, '../fixtures/weird name folder [v0]') + ipfs.util.addFromFs(filesPath, { recursive: true }, (err, result) => { + expect(err).to.not.exist + expect(result.length).to.be.above(8) + done() + }) }) - }) - 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('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.equal(1) - expect(result[0].path).to.be.equal('testfile.txt') - done() + it('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.equal(1) + expect(result[0].path).to.be.equal('testfile.txt') + done() + }) + }) + + it('a hidden file in a directory', (done) => { + const filesPath = path.join(__dirname, '../fixtures/test-folder') + ipfs.util.addFromFs(filesPath, { recursive: true, hidden: true }, (err, result) => { + expect(err).to.not.exist + expect(result.length).to.be.above(10) + expect(result.map(object => object.path)).to.include('test-folder/.hiddenTest.txt') + expect(result.map(object => object.hash)).to.include('QmdbAjVmLRdpFyi8FFvjPfhTGB2cVXvWLuK7Sbt38HXrtt') + done() + }) }) })