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

Commit

Permalink
code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 9, 2016
1 parent 8df6248 commit aae2259
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 72 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"homepage": "https://github.com/ipfs/js-ipfs#readme",
"devDependencies": {
"aegir": "^3.0.1",
"async": "^1.5.2",
"async": "^2.0.0-rc.4",
"buffer-loader": "0.0.1",
"chai": "^3.5.0",
"expose-loader": "^0.7.1",
Expand All @@ -64,7 +64,7 @@
"hapi": "^13.3.0",
"ipfs-api": "^3.0.2",
"ipfs-block": "^0.3.0",
"ipfs-block-service": "^0.3.0",
"ipfs-block-service": "^0.4.0",
"ipfs-merkle-dag": "^0.5.0",
"ipfs-multipart": "^0.1.0",
"ipfs-repo": "^0.8.0",
Expand All @@ -79,7 +79,7 @@
"peer-book": "0.1.0",
"peer-id": "^0.6.6",
"peer-info": "^0.6.2",
"readable-stream": "^1.1.13",
"readable-stream": "1.1.13",
"ronin": "^0.3.11",
"temp": "^0.8.3"
},
Expand Down Expand Up @@ -115,4 +115,4 @@
"kumavis <[email protected]>",
"nginnever <[email protected]>"
]
}
}
15 changes: 6 additions & 9 deletions src/cli/commands/files/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ function checkPath (inPath, recursive) {
}
if (inPath === '.' && recursive === true) {
inPath = process.cwd()
return inPath
} else if (inPath === '.' && recursive === false) {
s = fs.statSync(process.cwd())
if (s.isDirectory()) {
Expand Down Expand Up @@ -76,15 +75,13 @@ module.exports = Command.extend({
if (res.length !== 0) {
const index = inPath.lastIndexOf('/')
async.eachLimit(res, 10, (element, callback) => {
const addPath = element.substring(index + 1, element.length)
if (fs.statSync(element).isDirectory()) {
callback()
} else {
rs = fs.createReadStream(element)
filePair = {path: addPath, stream: rs}
i.write(filePair)
callback()
if (!fs.statSync(element).isDirectory()) {
i.write({
path: element.substring(index + 1, element.length),
stream: fs.createReadStream(element)
})
}
callback()
}, (err) => {
if (err) {
throw err
Expand Down
72 changes: 28 additions & 44 deletions src/cli/commands/files/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ log.error = debug('cli:files:error')
var fs = require('fs')
const path = require('path')
const pathExists = require('path-exists')
const async = require('async')

function checkArgs (hash, outPath) {
if (!hash) {
Expand All @@ -30,60 +29,45 @@ function checkArgs (hash, outPath) {
}
}

function getFiles (result, dir) {
var filePath
result.on('file', (file) => {
function ensureDir (dir, cb) {
pathExists(dir)
.then((exists) => {
if (!exists) {
fs.mkdir(dir, cb)
} else {
cb()
}
})
.catch(cb)
}

function fileHandler (result, dir) {
return function onFile (file) {
// Check to see if the result is in a directory
if (file.path.lastIndexOf('/') === -1) {
filePath = file.path
const dirPath = path.join(dir, file.path)
// Check to see if the result is a directory
if (file.dir === false) {
const ws = fs.createWriteStream(path.join(dir, file.path))
file.stream.pipe(ws)
file.stream.pipe(fs.createWriteStream(dirPath))
} else {
// Check to see if the directory has already been created
pathExists(path.join(dir, file.path)).then(exists => {
if (!exists) {
fs.mkdir(path.join(dir, file.path), (err) => {
if (err) {
throw err
}
})
ensureDir(dirPath, (err) => {
if (err) {
throw err
}
})
}
} else {
// Check to see if the directory has already been created
filePath = file.path.substring(0, file.path.lastIndexOf('/') + 1)
pathExists(path.join(dir, filePath)).then(exists => {
// Create a directory for the incoming files
if (!exists) {
async.waterfall([
(cb) => {
fs.mkdir(path.join(dir, filePath), (err) => {
if (err) {
cb(err)
}
cb(null)
})
},
(cb) => {
const ws = fs.createWriteStream(path.join(dir, file.path))
file.stream.pipe(ws)
cb(null)
}
], (err) => {
if (err) {
throw err
}
})
const filePath = file.path.substring(0, file.path.lastIndexOf('/') + 1)
const dirPath = path.join(dir, filePath)
ensureDir(dirPath, (err) => {
if (err) {
throw err
}
// Just write the file
const ws = fs.createWriteStream(path.join(dir, file.path))
file.stream.pipe(ws)

file.stream.pipe(fs.createWriteStream(dirPath))
})
}
})
}
}

module.exports = Command.extend({
Expand All @@ -100,7 +84,7 @@ module.exports = Command.extend({
if (err) {
throw err
}
getFiles(result, dir)
result.on('file', fileHandler(result, dir))
})
})
}
Expand Down
16 changes: 7 additions & 9 deletions src/core/ipfs/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const glob = require('glob')
const async = require('async')
const Readable = require('stream').Readable
const fs = require('fs')
const Importer = require('ipfs-unixfs-engine').importer

module.exports = function init (self) {
return (opts, callback) => {
Expand Down Expand Up @@ -71,7 +72,6 @@ module.exports = function init (self) {
return doneImport(null)
}

const Importer = require('ipfs-unixfs-engine').importer
const blocks = new BlockService(self._repo)
const dag = new DagService(blocks)

Expand All @@ -87,23 +87,19 @@ module.exports = function init (self) {
const index = __dirname.lastIndexOf('/')
async.eachLimit(res, 10, (element, callback) => {
const addPath = element.substring(index + 1, element.length)
if (fs.statSync(element).isDirectory()) {
callback()
} else {
const buffered = fs.readFileSync(element)
if (!fs.statSync(element).isDirectory()) {
const rs = new Readable()
rs.push(buffered)
rs.push(fs.readFileSync(element))
rs.push(null)
const filePair = {path: addPath, stream: rs}
i.write(filePair)
callback()
}
callback()
}, (err) => {
if (err) {
throw err
}
i.end()
return
})
})

Expand All @@ -112,7 +108,9 @@ module.exports = function init (self) {
})

function doneImport (err, stat) {
if (err) { return callback(err) }
if (err) {
return callback(err)
}

// All finished!
callback(null, true)
Expand Down
6 changes: 1 addition & 5 deletions src/core/ipfs/libp2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ module.exports = function libp2p (self) {
})
},
stop: (callback) => {
try {
self._libp2pNode.swarm.close(callback)
} catch (err) {
console.log('It is fine :)')
}
self._libp2pNode.swarm.close(callback)
},
swarm: {
peers: (callback) => {
Expand Down
1 change: 0 additions & 1 deletion test/cli-tests/test-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ describe('block', () => {
describe('api running', () => {
let httpAPI
before((done) => {
console.log('repoPath ->', repoPath)
httpAPI = new HttpAPI(repoPath)
httpAPI.start((err) => {
expect(err).to.not.exist
Expand Down

0 comments on commit aae2259

Please sign in to comment.