This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
utils.js
113 lines (99 loc) · 2.68 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict'
const fs = require('fs')
const os = require('os')
const multiaddr = require('multiaddr')
const path = require('path')
const log = require('debug')('ipfs:cli:utils')
const Progress = require('progress')
const byteman = require('byteman')
exports.isDaemonOn = isDaemonOn
function isDaemonOn () {
try {
fs.readFileSync(path.join(exports.getRepoPath(), 'api'))
log('daemon is on')
return true
} catch (err) {
log('daemon is off')
return false
}
}
exports.getAPICtl = getAPICtl
function getAPICtl (apiAddr) {
if (!apiAddr && !isDaemonOn()) {
throw new Error('daemon is not on')
}
if (!apiAddr) {
const apiPath = path.join(exports.getRepoPath(), 'api')
apiAddr = multiaddr(fs.readFileSync(apiPath).toString()).toString()
}
// Required inline to reduce startup time
const APIctl = require('ipfs-http-client')
return APIctl(apiAddr)
}
exports.getIPFS = argv => {
if (argv.api || isDaemonOn()) {
return getAPICtl(argv.api)
}
// Required inline to reduce startup time
const IPFS = require('../core')
return IPFS.create({
silent: argv.silent,
repoAutoMigrate: argv.migrate,
repo: exports.getRepoPath(),
init: { allowNew: false },
start: false,
pass: argv.pass
})
}
exports.getRepoPath = () => {
return process.env.IPFS_PATH || os.homedir() + '/.jsipfs'
}
let visible = true
exports.disablePrinting = () => { visible = false }
exports.print = (msg, newline, isError = false) => {
if (newline === undefined) {
newline = true
}
if (visible) {
if (msg === undefined) {
msg = ''
}
msg = newline ? msg + '\n' : msg
const outStream = isError ? process.stderr : process.stdout
outStream.write(msg)
}
}
exports.createProgressBar = (totalBytes) => {
const total = byteman(totalBytes, 2, 'MB')
const barFormat = `:progress / ${total} [:bar] :percent :etas`
// 16 MB / 34 MB [=========== ] 48% 5.8s //
return new Progress(barFormat, {
incomplete: ' ',
clear: true,
stream: process.stdout,
total: totalBytes
})
}
exports.rightpad = (val, n) => {
let result = String(val)
for (let i = result.length; i < n; ++i) {
result += ' '
}
return result
}
exports.ipfsPathHelp = 'ipfs uses a repository in the local file system. By default, the repo is ' +
'located at ~/.jsipfs. To change the repo location, set the $IPFS_PATH environment variable:\n\n' +
'export IPFS_PATH=/path/to/ipfsrepo\n'
exports.singleton = create => {
let promise
return function getter () {
if (!promise) {
promise = (async () => {
const instance = await create()
getter.instance = instance
return instance
})()
}
return promise
}
}