Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

fix(cli): start fixing files add #483

Merged
merged 1 commit into from
Sep 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 61 additions & 36 deletions src/cli/commands/files/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const path = require('path')
const glob = require('glob')
const sortBy = require('lodash.sortby')
const pull = require('pull-stream')
const pullFile = require('pull-file')
const paramap = require('pull-paramap')
const zip = require('pull-zip')
const toPull = require('stream-to-pull-stream')

function checkPath (inPath, recursive) {
// This function is to check for the following possible inputs
Expand Down Expand Up @@ -59,46 +59,71 @@ module.exports = {
throw err
}

glob(path.join(inPath, '/**/*'), (err, list) => {
// TODO: revist when interface-ipfs-core exposes pull-streams
let createAddStream = (cb) => {
ipfs.files.createAddStream((err, stream) => {
cb(err, err ? null : toPull.transform(stream))
})
}

if (typeof ipfs.files.createAddPullStream === 'function') {
createAddStream = (cb) => {
cb(null, ipfs.files.createAddPullStream())
}
}

createAddStream((err, addStream) => {
if (err) {
throw err
}
if (list.length === 0) {
list = [inPath]
}

pull(
zip(
pull.values(list),
pull(
pull.values(list),
paramap(fs.stat.bind(fs), 50)
)
),
pull.map((pair) => ({
path: pair[0],
isDirectory: pair[1].isDirectory()
})),
pull.filter((file) => !file.isDirectory),
pull.map((file) => ({
path: file.path.substring(index, file.path.length),
content: pullFile(file.path)
})),
ipfs.files.createAddPullStream(),
pull.map((file) => ({
hash: file.hash,
path: file.path
})),
pull.collect((err, added) => {
if (err) throw err

sortBy(added, 'path')
.reverse()
.map((file) => `added ${file.hash} ${file.path}`)
.forEach((msg) => console.log(msg))
})
)
glob(path.join(inPath, '/**/*'), (err, list) => {
if (err) {
throw err
}
if (list.length === 0) {
list = [inPath]
}

addPipeline(index, addStream, list)
})
})
})
}
}

function addPipeline (index, addStream, list) {
pull(
zip(
pull.values(list),
pull(
pull.values(list),
paramap(fs.stat.bind(fs), 50)
)
),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We zip this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I get a combo of the name + the stat below

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also zip is combining two streams, not zip as in compressing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, wanna make a comment on that line, it was clearly not obvious (I would expect that to be called concat)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

concat is something entirely different zip is a standard name in function programming: https://en.wikipedia.org/wiki/Convolution_(computer_science)

pull.map((pair) => ({
path: pair[0],
isDirectory: pair[1].isDirectory()
})),
pull.filter((file) => !file.isDirectory),
pull.map((file) => ({
path: file.path.substring(index, file.path.length),
content: fs.createReadStream(file.path)
})),
addStream,
pull.map((file) => ({
hash: file.hash,
path: file.path
})),
pull.collect((err, added) => {
if (err) {
throw err
}

sortBy(added, 'path')
.reverse()
.map((file) => `added ${file.hash} ${file.path}`)
.forEach((msg) => console.log(msg))
})
)
}
14 changes: 8 additions & 6 deletions src/core/ipfs/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ const toStream = require('pull-stream-to-stream')
const toPull = require('stream-to-pull-stream')

module.exports = function files (self) {
const createAddPullStream = () => pull(
pull.map(normalizeContent),
pull.flatten(),
importer(self._dagS),
pull.asyncMap(prepareFile.bind(null, self))
)
const createAddPullStream = () => {
return pull(
pull.map(normalizeContent),
pull.flatten(),
importer(self._dagS),
pull.asyncMap(prepareFile.bind(null, self))
)
}

return {
createAddStream: (callback) => {
Expand Down
34 changes: 34 additions & 0 deletions test/cli/test-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,39 @@ describe('files', () => {
done()
})
})

it('add', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'add', 'src/init-files/init-docs/readme'], {env})
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(stdout[0]).to.equal('added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB readme')
done()
})
})

it('add recursively', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'add', '-r', 'src/init-files/init-docs'], {env})
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)

const expected = [
'added QmYE7xo6NxbHEVEHej1yzxijYaNY51BaeKxjXxn6Ssa6Bs init-docs/tour/0.0-intro',
'added QmciSU8hfpAXKjvK5YLUSwApomGSWN5gFbP4EpDAEzu2Te init-docs/tour',
'added QmTumTjvcYCAvRRwQ8sDRxh8ezmrcr88YFU7iYNroGGTBZ init-docs/security-notes',
'added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB init-docs/readme',
'added QmdncfsVm2h5Kqq9hPmU7oAVX2zTSVP3L869tgTbPYnsha init-docs/quick-start',
'added QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7 init-docs/help',
'added QmQN88TEidd3RY2u3dpib49fERTDfKtDpvxnvczATNsfKT init-docs/docs/index',
'added QmegvLXxpVKiZ4b57Xs1syfBVRd8CbucVHAp7KpLQdGieC init-docs/docs',
'added QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y init-docs/contact',
'added QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V init-docs/about',
'added QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU init-docs'
]
expect(stdout).to.deep.equal(expected)
done()
})
})
})
})