-
Notifications
You must be signed in to change notification settings - Fork 6
/
bin.js
executable file
·261 lines (221 loc) · 6.12 KB
/
bin.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env node
const { getIpfs, providers } = require('ipfs-provider')
const { httpClient } = providers
const meow = require('meow')
const cohost = require('.')
const ora = require('ora')
const prettyBytes = require('pretty-bytes')
const yesno = require('yesno')
let log = null
let spin = null
const cli = meow(`
Usage
$ ipfs-cohost <domain>...
$ ipfs-cohost add <domain>... [--lazy] [--full] [--non-interactive]
$ ipfs-cohost rm <domain>... [--all]
$ ipfs-cohost ls [domain]...
$ ipfs-cohost mv [domain]... [--lazy] [--full]
$ ipfs-cohost sync
$ ipfs-cohost prune [n]
$ ipfs-cohost latest <domain>... [--lazy] [--full]
Example
$ ipfs-cohost docs.ipfs.io cid.ipfs.io
Options
--silent, -s Just do your job
--full Fully cohost a website (default)
--lazy Lazily cohost a website
--all Remove all cohosted websites (lazy AND full)
--non-interactive Cohost websites no matter their size
`, {
flags: {
silent: {
type: 'boolean',
default: false,
alias: 's'
},
full: {
type: 'boolean'
},
lazy: {
type: 'boolean'
},
all: {
type: 'boolean'
},
nonInteractive: {
type: 'boolean',
default: false
}
}
})
async function addCheckSizes (ipfs, input) {
const stats = await Promise.all(input.map(async domain => {
const cid = await ipfs.resolve(`/ipns/${domain}`)
return ipfs.files.stat(cid)
}))
const size = stats.reduce((acc, v) => acc + v.cumulativeSize, 0)
if (size >= 10485760) {
// Over or equal to 10 MiB
return yesno({
question: `⚠️ This operation will use ${prettyBytes(size)}. Do you wish to continue?`
})
}
return true
}
async function add (ipfs, input) {
if (!cli.flags.nonInteractive && !await addCheckSizes(ipfs, input)) {
return
}
const longestDomain = input.reduce((r, c) => r.length >= c.length ? r : c)
let size = 0
for (const domain of input) {
const spinner = spin(`Cohosting ${domain}...`)
const { hash, cumulativeSize } = await cohost.add(ipfs, domain, {
full: !cli.flags.lazy
})
spinner.stop()
size += cumulativeSize
log('📍', domain.padEnd(longestDomain.length), hash, prettyBytes(cumulativeSize))
}
log('📦', 'Total size', prettyBytes(size), 'for', input.length, 'domains')
log('🤝', 'Co-hosting', input.length, 'domains via IPFS.')
}
async function rm (ipfs, input) {
let spinner
if (cli.flags.all) {
spinner = spin('Removing all cohosted websites...')
await cohost.rmAll(ipfs)
spinner.succeed('All cohosted websites removed.')
return
}
for (const domain of input) {
spinner = spin(`Stopping to cohost ${domain}...`)
await cohost.rm(ipfs, domain)
spinner.succeed(` ${domain} no longer cohosted.`)
}
}
async function ls (ipfs, input) {
if (input.length > 0) {
for (const domain of input) {
const { lazy, full } = await cohost.ls(ipfs, domain)
if (lazy.length > 0) {
log(`⏱ Lazy snapshots for ${domain}:`)
lazy.forEach(n => log(` ${n}`))
}
if (full.length > 0) {
log(`⏱ Full snapshots for ${domain}:`)
full.forEach(n => log(` ${n}`))
}
}
return
}
const { lazy, full } = await cohost.ls(ipfs)
if (lazy.length > 0) {
log('📍 Lazily cohosted domains:')
lazy.forEach(n => log(` ${n}`))
}
if (full.length > 0) {
log('📍 Fully cohosted domains:')
full.forEach(n => log(` ${n}`))
}
}
async function sync (ipfs) {
const spinner = spin('Syncing snapshots...')
await cohost.sync(ipfs)
spinner.succeed(' Snapshots synced!')
}
function printSnapshots (snap) {
for (const { domain, snapshots } of snap) {
log(` ${domain}`)
snapshots.forEach(n => log(` ${n}`))
}
}
async function prune (ipfs, input) {
let num = 1
if (input.length > 0) num = parseInt(input[0], 10)
const spinner = spin('Cleaning cohosted websites...')
const { lazy, full } = await cohost.prune(ipfs, num)
spinner.succeed(' Cohosted websites cleaned!')
if (lazy.length > 0) {
log('⏱ Lazy snapshots pruned:')
printSnapshots(lazy)
}
if (full.length > 0) {
log('⏱ Full snapshots pruned:')
printSnapshots(full)
}
}
async function mv (ipfs, input) {
for (const domain of input) {
await cohost.mv(ipfs, domain, { lazy: !cli.flags.full })
log(`📍 ${input} now ${cli.flags.full ? 'fully' : 'lazily'} cohosted!`)
}
}
async function latest (ipfs, input) {
cli.flags.all = true
await rm(ipfs)
await add(ipfs, input)
}
async function run () {
if (cli.input.length === 0) {
return cli.showHelp()
}
const cmd = cli.input.shift()
const input = cli.input
const res = await getIpfs({
loadHttpClientModule: () => require('ipfs-http-client'),
providers: [httpClient()]
})
if (!res) {
throw new Error('Unable to find IPFS node. Make sure ipfs daemon is running.')
}
const { ipfs, provider } = res
log = logger.bind(null, cli.flags.silent)
spin = spinner.bind(null, cli.flags.silent)
if (!cli.flags.silent) {
log('🔌 Using', provider === 'httpClient' ? 'local ipfs daemon via http api' : 'js-ipfs node')
}
try {
switch (cmd) {
case 'add':
await add(ipfs, input)
break
case 'rm':
await rm(ipfs, input)
break
case 'ls':
await ls(ipfs, input)
break
case 'sync':
await sync(ipfs)
break
case 'prune':
await prune(ipfs, input)
break
case 'mv':
await mv(ipfs, input)
break
case 'latest':
await latest(ipfs, input)
break
default:
await add(ipfs, input.concat(cmd))
}
} catch (error) {
console.error(error.message || error)
process.exit(-1)
}
if (provider === 'JS_IPFS') {
await ipfs.stop()
process.exit()
}
}
function spinner (silent, text) {
if (silent) return { stop: () => {}, fail: () => {}, succeed: () => {} }
return ora({ text: ` ${text}`, spinner: 'dots' }).start()
}
function logger (silent, ...args) {
if (silent) return
console.log(args.join(' '))
}
run()