From 117913e46178ccdd7a3ac0f9a86bba086c606f22 Mon Sep 17 00:00:00 2001 From: Jeff Downie Date: Tue, 10 Jan 2017 17:01:50 +0000 Subject: [PATCH 1/2] Adding option for dotfiles in addFromFs. --- README.md | 5 +- src/get-files-stream.js | 1 + test/fixtures/test-folder/.hiddenTest.txt | 1 + test/ipfs-api/util.spec.js | 68 +++++++++++++---------- 4 files changed, 46 insertions(+), 29 deletions(-) create mode 100644 test/fixtures/test-folder/.hiddenTest.txt diff --git a/README.md b/README.md index f1f64e5d9..2993204d4 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'] }`. + - **dot**: Dotfiles (files or folders starting with a `.`, for example, `.git/`) are not included by default. To add them, use the option `{ dot: 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..949c7b0e9 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.dot, 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..8dc32bbfd 100644 --- a/test/ipfs-api/util.spec.js +++ b/test/ipfs-api/util.spec.js @@ -42,40 +42,52 @@ 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 dotFile in a directory', (done) => { + const filesPath = path.join(__dirname, '../fixtures/test-folder') + ipfs.util.addFromFs(filesPath, { recursive: true, dot: 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') + done() + }) }) }) From 7414ccd0c497ffd3bc09c1cec67d091276b90f07 Mon Sep 17 00:00:00 2001 From: Jeff Downie Date: Thu, 12 Jan 2017 09:09:12 +0000 Subject: [PATCH 2/2] Adding changes for code review. * Changed `dot` option to `hidden` to match go-ipfs * Asserted hidden file hash in test for safety --- README.md | 2 +- src/get-files-stream.js | 2 +- test/ipfs-api/util.spec.js | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2993204d4..e19c97ea5 100644 --- a/README.md +++ b/README.md @@ -153,7 +153,7 @@ Complete documentation for these methods is coming with: https://github.com/ipfs 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'] }`. - - **dot**: Dotfiles (files or folders starting with a `.`, for example, `.git/`) are not included by default. To add them, use the option `{ dot: true }`. + - **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 949c7b0e9..17a65ad5e 100644 --- a/src/get-files-stream.js +++ b/src/get-files-stream.js @@ -48,7 +48,7 @@ function loadPaths (opts, file) { const globEscapedDir = escape(file) + (file.endsWith('/') ? '' : '/') const mg = new glob.sync.GlobSync(`${globEscapedDir}**/*`, { follow: followSymlinks, - dot: opts.dot, + dot: opts.hidden, ignore: (opts.ignore || []).map(function (ignoreGlob) { return globEscapedDir + ignoreGlob }) diff --git a/test/ipfs-api/util.spec.js b/test/ipfs-api/util.spec.js index 8dc32bbfd..374142266 100644 --- a/test/ipfs-api/util.spec.js +++ b/test/ipfs-api/util.spec.js @@ -80,12 +80,13 @@ describe('.util', () => { }) }) - it('a dotFile in a directory', (done) => { + it('a hidden file in a directory', (done) => { const filesPath = path.join(__dirname, '../fixtures/test-folder') - ipfs.util.addFromFs(filesPath, { recursive: true, dot: true }, (err, result) => { + 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() }) })