diff --git a/.gitignore b/.gitignore index e4c5b0e314..b620619d8c 100644 --- a/.gitignore +++ b/.gitignore @@ -30,7 +30,7 @@ build # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git -node_modules +# node_modules lib dist diff --git a/src/core/components/files.js b/src/core/components/files.js index 6a9f5fbced..e0f07f9215 100644 --- a/src/core/components/files.js +++ b/src/core/components/files.js @@ -80,7 +80,7 @@ module.exports = function files (self) { cat: promisify((ipfsPath, callback) => { if (typeof ipfsPath === 'function') { - return callback(new Error('You must supply a ipfsPath')) + return callback(new Error('You must supply an ipfsPath')) } pull( @@ -89,6 +89,7 @@ module.exports = function files (self) { if (err) { return callback(err) } + if (!files || !files.length) return callback(new Error('No such file')) callback(null, toStream.source(files[files.length - 1].content)) }) ) diff --git a/src/core/components/init-assets.js b/src/core/components/init-assets.js index 33778a50ef..1d6d46baad 100644 --- a/src/core/components/init-assets.js +++ b/src/core/components/init-assets.js @@ -6,12 +6,12 @@ const glob = require('glob') const importer = require('ipfs-unixfs-engine').importer const pull = require('pull-stream') const file = require('pull-file') -// const mh = require('multihashes') +const CID = require('cids') // Add the default assets to the repo. module.exports = function addDefaultAssets (self, log, callback) { const initDocsPath = path.join(__dirname, '../../init-files/init-docs') - const index = __dirname.lastIndexOf('/') + const index = initDocsPath.lastIndexOf('/') pull( pull.values([initDocsPath]), @@ -20,7 +20,7 @@ module.exports = function addDefaultAssets (self, log, callback) { }), pull.flatten(), pull.map((element) => { - const addPath = element.substring(index + 1, element.length) + const addPath = element.substring(index + 1) if (fs.statSync(element).isDirectory()) { return } @@ -34,14 +34,10 @@ module.exports = function addDefaultAssets (self, log, callback) { pull.filter(Boolean), importer(self._ipldResolver), pull.through((el) => { - if (el.path === 'files/init-docs/docs') { - log('to get started, enter:') - log() - log(`\t jsipfs files cat /ipfs/QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB`) - // TODO when we support pathing in unixfs-engine - // const hash = mh.toB58String(el.multihash) - // log(`\t jsipfs files cat /ipfs/${hash}/readme`) - log() + if (el.path === 'init-docs') { + const cid = new CID(el.multihash) + log('to get started, enter:\n') + log(`\t jsipfs files cat /ipfs/${cid.toBaseEncodedString()}/readme\n`) } }), pull.collect((err) => { diff --git a/test/cli/files.js b/test/cli/files.js index afcf581112..2a71143387 100644 --- a/test/cli/files.js +++ b/test/cli/files.js @@ -238,6 +238,14 @@ describe('files', () => runOnAndOff((thing) => { }) }) + it('cat non-existent file', () => { + return ipfs('cat QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB/dummy') + .then(() => expect.fail(0, 1, 'Should have thrown an error')) + .catch((err) => { + expect(err).to.exist() + }) + }) + it('get', () => { return ipfs('files get QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB') .then((out) => { diff --git a/test/cli/init.js b/test/cli/init.js index 919a74f3af..7953f0fcaf 100644 --- a/test/cli/init.js +++ b/test/cli/init.js @@ -12,6 +12,9 @@ describe('init', () => { let repoPath let ipfs + const readme = fs.readFileSync(path.join(process.cwd(), '/src/init-files/init-docs/readme')) + .toString('utf-8') + const repoExistsSync = (p) => fs.existsSync(path.join(repoPath, p)) const repoDirSync = (p) => { @@ -27,12 +30,17 @@ describe('init', () => { afterEach(() => clean(repoPath)) it('basic', () => { - return ipfs('init').then(() => { + return ipfs('init').then((out) => { expect(repoDirSync('blocks')).to.have.length.above(2) expect(repoExistsSync('config')).to.equal(true) expect(repoExistsSync('version')).to.equal(true) - }) - }) + + // Test that the following was written when init-ing the repo + // jsipfs files cat /ipfs/QmfGBRT6BbWJd7yUc2uYdaUZJBbnEFvTqehPFoSMQ6wgdr/readme + let command = out.substring(out.indexOf('files cat'), out.length - 2 /* omit the newline char */) + return ipfs(command) + }).then((out) => expect(out).to.equal(readme)) + }).timeout(8000) it('bits', () => { return ipfs('init --bits 1024').then(() => {