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

Commit 49571a2

Browse files
committed
fix: fix version cmd and repo component
1 parent 4235ef4 commit 49571a2

File tree

8 files changed

+43
-84
lines changed

8 files changed

+43
-84
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"form-data": "^2.3.2",
7575
"hat": "0.0.3",
7676
"interface-ipfs-core": "~0.64.3",
77-
"ipfsd-ctl": "^0.33.1",
77+
"ipfsd-ctl": "~0.33.1",
7878
"lodash": "^4.17.10",
7979
"mocha": "^5.1.1",
8080
"ncp": "^2.0.0",
@@ -111,7 +111,7 @@
111111
"ipfs-block": "~0.7.1",
112112
"ipfs-block-service": "~0.14.0",
113113
"ipfs-multipart": "~0.1.0",
114-
"ipfs-repo": "0.20.0",
114+
"ipfs-repo": "~0.21.0",
115115
"ipfs-unixfs": "~0.1.14",
116116
"ipfs-unixfs-engine": "~0.29.0",
117117
"ipld": "^0.17.0",

src/cli/bin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ let cli = yargs(argv)
7777
// .recommendCommands()
7878
.completion()
7979

80-
if (argv[0] === 'daemon' || argv[0] === 'init' || argv[0] === 'id') {
80+
if (['daemon', 'init', 'id', 'version'].includes(argv[0])) {
8181
args = cli.fail((msg, err, yargs) => {
8282
if (err instanceof Error && err.message && !msg) {
8383
msg = err.message

src/cli/commands/version.js

+19-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const os = require('os')
4-
const print = require('../utils').print
4+
const {print, getNodeOrAPI} = require('../utils')
55

66
module.exports = {
77
command: 'version',
@@ -33,27 +33,25 @@ module.exports = {
3333
},
3434

3535
handler (argv) {
36-
argv.ipfs.version((err, data) => {
37-
if (err) {
38-
throw err
39-
}
36+
return getNodeOrAPI(argv, {forceInitialized: false})
37+
.then(node => node.version())
38+
.then(data => {
39+
const withCommit = argv.all || argv.commit
40+
const parsedVersion = `${data.version}${withCommit ? `-${data.commit}` : ''}`
4041

41-
const withCommit = argv.all || argv.commit
42-
const parsedVersion = `${data.version}${withCommit ? `-${data.commit}` : ''}`
43-
44-
if (argv.repo) {
42+
if (argv.repo) {
4543
// go-ipfs prints only the number, even without the --number flag.
46-
print(data.repo)
47-
} else if (argv.number) {
48-
print(parsedVersion)
49-
} else if (argv.all) {
50-
print(`js-ipfs version: ${parsedVersion}`)
51-
print(`Repo version: ${data.repo}`)
52-
print(`System version: ${os.arch()}/${os.platform()}`)
53-
print(`Node.js version: ${process.version}`)
54-
} else {
55-
print(`js-ipfs version: ${parsedVersion}`)
56-
}
57-
})
44+
print(data.repo)
45+
} else if (argv.number) {
46+
print(parsedVersion)
47+
} else if (argv.all) {
48+
print(`js-ipfs version: ${parsedVersion}`)
49+
print(`Repo version: ${data.repo}`)
50+
print(`System version: ${os.arch()}/${os.platform()}`)
51+
print(`Node.js version: ${process.version}`)
52+
} else {
53+
print(`js-ipfs version: ${parsedVersion}`)
54+
}
55+
})
5856
}
5957
}

src/cli/utils.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@ function getAPICtl (apiAddr) {
3838
return APIctl(apiAddr)
3939
}
4040

41-
exports.getNodeOrAPI = (argv) => {
41+
exports.getNodeOrAPI = (argv, stateOptions = {forceInitialized: true}) => {
4242
log('get node or api async')
4343
if (argv.api || isDaemonOn()) {
4444
return Promise.resolve(getAPICtl(argv.api))
4545
}
4646

47-
return IPFS.createReadyNodePromise({
47+
return IPFS.createNodePromise({
4848
repo: exports.getRepoPath(),
4949
init: false,
5050
start: false,
5151
pass: argv.pass,
5252
EXPERIMENTAL: {
5353
pubsub: true
5454
}
55-
})
55+
}, stateOptions)
5656
}
5757

5858
exports.getRepoPath = () => {

src/core/boot.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,7 @@ module.exports = (self) => {
3636
}
3737
], (err, res) => {
3838
if (err) {
39-
// If the error is that no repo exists,
40-
// which happens when the version file is not found
41-
// we just want to signal that no repo exist, not
42-
// fail the whole process.
43-
// TODO: improve datastore and ipfs-repo implemenations so this error is a bit more unified
44-
if (err.message.match(/not found/) || // indexeddb
45-
err.message.match(/ENOENT/) || // fs
46-
err.message.match(/No value/) // memory
47-
) {
39+
if (err.code === 'ERR_REPO_NOT_INITIALIZED') {
4840
return cb(null, false)
4941
}
5042
return cb(err)

src/core/components/repo.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,7 @@ module.exports = function repo (self) {
1919
version: promisify((callback) => {
2020
self._repo._isInitialized(err => {
2121
if (err) {
22-
// TODO: (dryajov) This is really hacky, there must be a better way
23-
const match = [
24-
/Key not found in database \[\/version\]/,
25-
/ENOENT/,
26-
/not yet initialized/
27-
].some((m) => {
28-
return m.test(err.message)
29-
})
30-
if (match) {
22+
if (err.code === 'ERR_REPO_NOT_INITIALIZED') {
3123
// this repo has not been initialized
3224
return callback(null, repoVersion)
3325
}

src/core/index.js

+15-38
Original file line numberDiff line numberDiff line change
@@ -147,56 +147,33 @@ IPFS.createNode = (options) => {
147147
/**
148148
* Static factory method to create a node wrapped with a Promise
149149
*
150-
* The Promise does *NOT* wait for the Ready Event to resolve with a IPFS instance
151-
* and rejects with Error Events.
152-
*
153-
* @param {object} options
154-
* @returns {IPFS}
155-
*/
156-
IPFS.createNodePromise = (options) => {
157-
return new Promise((resolve, reject) => {
158-
const node = new IPFS(options)
159-
160-
node.on('error', err => {
161-
reject(err)
162-
})
163-
164-
resolve(node)
165-
})
166-
}
167-
168-
/**
169-
* Static factory method to create a ready node wrapped with a Promise
170-
*
171150
* The Promise waits for the Ready Event to resolve with a IPFS instance
172-
* and rejects with Error Events or a repo not initialized.
151+
* and rejects with Error Events. Rejections can be customized the with
152+
* second param.
173153
*
174-
* @param {Object} options IPFS constructor options
154+
* @param {object} options - IPFS node options
155+
* @param {object} stateOptions - Node state options to reject with error
175156
* @returns {IPFS}
176157
*/
177-
IPFS.createReadyNodePromise = (options = {}) => {
158+
IPFS.createNodePromise = (options = {}, stateOptions = {}) => {
178159
return new Promise((resolve, reject) => {
179160
const node = new IPFS(options)
180161

181-
node.once('error', err => {
162+
node.on('error', err => {
182163
reject(err)
183164
})
184165

185166
node.once('ready', () => {
186-
/**
187-
* We shouldn't need to do this but until we get a unified error from ipfs-repo
188-
* we catch it here
189-
*/
190-
node._repo._isInitialized((err) => {
191-
if (err) {
192-
reject(Object.assign(new Error('repo is not initialized yet'),
193-
{
194-
code: 'ERR_REPO_NOT_INITIALIZED',
195-
path: node._repo.path
196-
}))
197-
}
167+
if (stateOptions.forceInitialized) {
168+
node._repo._isInitialized((err) => {
169+
if (err) {
170+
reject(err)
171+
}
172+
resolve(node)
173+
})
174+
} else {
198175
resolve(node)
199-
})
176+
}
200177
})
201178
})
202179
}

test/http-api/interface/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const fs = require('fs')
55
const path = require('path')
66

7-
describe('## interface-ipfs-core over ipfs-api', () => {
7+
describe.only('## interface-ipfs-core over ipfs-api', () => {
88
fs.readdirSync(path.join(__dirname))
99
.forEach((file) => file !== 'index.js' && require(`./${file}`))
1010
})

0 commit comments

Comments
 (0)