forked from ipfs-shipyard/ipfs-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
408 lines (357 loc) · 10.8 KB
/
index.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
const util = require('util')
const { existsSync } = require('fs')
const stringify = require('json-stringify-safe')
const prettier = require('prettier')
const jsonifyError = require('jsonify-error')
const trammel = util.promisify(require('trammel'))
const byteSize = require('byte-size')
const clipboardy = require('clipboardy')
const publicIp = require('public-ip')
const isPortReachable = require('is-port-reachable')
const ipfsClient = require('ipfs-http-client')
const IPFS = require('ipfs')
const pinataSDK = require('@pinata/sdk')
const updateCloudflareDnslink = require('dnslink-cloudflare')
const ora = require('ora')
const chalk = require('chalk')
const doOpen = require('open')
const _ = require('lodash')
const fp = require('lodash/fp')
const neatFrame = require('neat-frame')
const { stripIndent } = require('common-tags')
// # Pure functions
function httpGatewayUrl(hash, gatewayProvider = 'ipfs') {
const gateways = {
ipfs: 'https://ipfs.io',
infura: 'https://ipfs.infura.io',
pinata: 'https://gateway.pinata.cloud',
}
const origin = gateways[gatewayProvider] || gateways['ipfs']
return `${origin}/ipfs/${hash}`
}
function formatError(e) {
const prettierJson = obj =>
prettier.format(stringify(obj), {
parser: 'json',
printWidth: 72,
tabWidth: 2,
})
const beautifyStr = fp.pipe(
stripIndent,
str => neatFrame(str, { trim: false })
)
if (_.isError(e)) {
eStr = prettierJson(jsonifyError(e))
} else if (_.isString(e)) {
eStr = e
} else if (_.isObjectLike(e)) {
eStr = prettierJson(e)
}
const beautifulErrorString = '\n' + beautifyStr(eStr)
return beautifulErrorString
}
const white = chalk.whiteBright
// Effectful functions
function logError(e) {
const errorString = formatError(e)
console.error(errorString)
return errorString
}
async function isNodeReachable(port) {
const isIpv4Reachable = await isPortReachable(port, {
host: await publicIp.v4(),
timeout: 5000,
})
return isIpv4Reachable
}
function guessedPath() {
const guesses = [
'_site', // jekyll, hakyll
'site',
'public', // gatsby, hugo
'dist', // nuxt
'output', // pelican
'out', // hexo
'build', // metalsmith, middleman
'website/build', // docusaurus
'docs',
]
return fp.filter(existsSync)(guesses)[0]
}
function guessPathIfEmpty(publicPath) {
let result
const spinner = ora()
if (_.isEmpty(publicPath)) {
spinner.info(
`🤔 No ${white('path')} argument specified. Looking for common ones…`
)
result = guessedPath()
if (result) {
spinner.succeed(
`📂 Found local ${chalk.blue(result)} directory. Deploying that.`
)
return result
} else {
spinner.fail(
`🔮 Couldn't guess what to deploy. Please specify a ${white('path')}.`
)
return undefined
}
} else {
return publicPath
}
}
async function openUrl(url) {
const spinner = ora()
spinner.start('🏄 Opening web browser…')
const childProcess = await doOpen(url)
spinner.succeed('🏄 Opened web browser (call with -O to disable.)')
return childProcess
}
async function updateCloudflareDns(siteDomain, { apiEmail, apiKey }, hash) {
const spinner = ora()
spinner.start(`📡 Beaming new hash to DNS provider ${white('Cloudflare')}…`)
if (fp.some(_.isEmpty)([siteDomain, apiEmail, apiKey])) {
spinner.fail('💔 Missing arguments for Cloudflare API.')
spinner.warn('🧐 Check if these environment variables are present:')
logError(`
IPFS_DEPLOY_SITE_DOMAIN
IPFS_DEPLOY_CLOUDFLARE__API_EMAIL
IPFS_DEPLOY_CLOUDFLARE__API_KEY
You can put them in a .env file if you want and they will be picked up.
`)
} else {
try {
const api = {
email: apiEmail,
key: apiKey,
}
const opts = {
record: siteDomain,
zone: siteDomain,
link: `/ipfs/${hash}`,
}
const content = await updateCloudflareDnslink(api, opts)
spinner.succeed('🙌 SUCCESS!')
spinner.info(`🔄 Updated DNS TXT ${white(opts.record)} to:`)
spinner.info(`🔗 ${white(content)}`)
} catch (e) {
spinner.fail("💔 Updating Cloudflare DNS didn't work.")
logError(e)
}
return siteDomain
}
}
async function showSize(path) {
const spinner = ora()
spinner.start(`📦 Calculating size of ${chalk.blue(path)}…`)
try {
const size = await trammel(path, {
stopOnError: true,
type: 'raw',
})
const kibi = byteSize(size, { units: 'iec' })
const readableSize = `${kibi.value} ${kibi.unit}`
spinner.succeed(`🚚 ${chalk.blue(path)} weighs ${readableSize}.`)
return readableSize
} catch (e) {
spinner.fail("⚖ Couldn't calculate website size.")
logError(e)
return undefined
}
}
function startIpfsNode(port) {
return new Promise((resolve, reject) => {
const spinner = ora()
spinner.start('♻️️ Starting temporary local IPFS node…\n')
const node = new IPFS({
silent: true,
config: {
Addresses: {
Swarm: [`/ip4/0.0.0.0/tcp/${port}`],
},
},
})
node.on('ready', async () => {
spinner.succeed('☎️ Connected to temporary local IPFS node.')
spinner.start(`🔌 Checking if port ${port} is externally reachable…`)
const isReachable = await isNodeReachable(port)
if (isReachable) {
spinner.succeed(`📶 Port ${port} is externally reachable.`)
node.port = port
resolve(node)
} else {
spinner.fail(`💔 Could not reach port ${port} from the outside :(`)
spinner.info(
'💡 Please forward it or try a different one with the --port option.'
)
reject(new Error(`Could not reach port ${port} from the outside`))
}
})
})
}
async function stopIpfsNode(node) {
const spinner = ora()
spinner.start('✋️ Stopping temporary local IPFS node…')
try {
await node.stop()
spinner.succeed('✋️ Stopped temporary local IPFS node.')
} catch (e) {
spinner.fail("🚂 Couldn't stop temporary local IPFS node.")
logError(e)
}
}
async function pinToTmpIpfsNode(ipfsNode, publicDirPath) {
const spinner = ora()
spinner.start('🔗 Pinning to temporary local IPFS node…')
const localPinResult = await ipfsNode.addFromFs(publicDirPath, {
recursive: true,
})
const { hash } = localPinResult[localPinResult.length - 1]
spinner.succeed('📌 Pinned to temporary local IPFS node with hash:')
spinner.info(`🔗 ${hash}`)
return hash
}
async function pinToPinata(ipfsNode, credentials, metadata = {}, hash) {
const spinner = ora()
spinner.start(`📠 Requesting remote pin to ${white('pinata.cloud')}…`)
if (fp.some(_.isEmpty)([credentials.apiKey, credentials.secretApiKey])) {
spinner.fail('💔 Missing credentials for Pinata API.')
spinner.warn('🧐 Check if these environment variables are present:')
logError(`
IPFS_DEPLOY_PINATA__API_KEY
IPFS_DEPLOY_PINATA__SECRET_API_KEY
You can put them in a .env file if you want and they will be picked up.
`)
} else {
const nodeId = util.promisify(ipfsNode.id.bind(ipfsNode))
const nodeInfo = await nodeId()
const pinataOptions = {
host_nodes: [
`/ip4/${await publicIp.v4()}/tcp/${ipfsNode.port}/ipfs/${nodeInfo.id}`,
],
pinataMetadata: metadata,
}
try {
const pinata = pinataSDK(credentials.apiKey, credentials.secretApiKey)
await pinata.pinHashToIPFS(hash, pinataOptions)
spinner.succeed("📌 It's pinned to Pinata now with hash:")
spinner.info(`🔗 ${hash}`)
return hash
} catch (e) {
spinner.fail("💔 Pinning to Pinata didn't work.")
logError(e)
return undefined
}
}
}
async function addToInfura(publicDirPath) {
const spinner = ora()
const infuraClient = ipfsClient({
host: 'ipfs.infura.io',
port: '5001',
protocol: 'https',
})
try {
spinner.start(
`📠 Uploading and pinning via https to ${white('infura.io')}…`
)
const response = await infuraClient.addFromFs(publicDirPath, {
recursive: true,
})
spinner.succeed("📌 It's pinned to Infura now with hash:")
const hash = response[response.length - 1].hash
spinner.info(`🔗 ${hash}`)
return hash
} catch (e) {
spinner.fail("💔 Uploading to Infura didn't work.")
logError(e)
return undefined
}
}
function copyUrlToClipboard(url) {
const spinner = ora()
spinner.start('📋 Copying HTTP gateway URL to clipboard…')
clipboardy.writeSync(url)
spinner.succeed('📋 Copied HTTP gateway URL to clipboard:')
spinner.info(`🔗 ${chalk.green(url)}`)
}
async function deploy({
publicDirPath,
copyHttpGatewayUrlToClipboard = false,
open = false,
port = '4002',
remotePinners = ['infura'],
dnsProviders = [],
siteDomain,
credentials = {
cloudflare: {
apiEmail,
apiKey,
},
pinata: {
apiKey,
secretApiKey,
},
},
} = {}) {
publicDirPath = guessPathIfEmpty(publicDirPath)
if (!publicDirPath) {
return undefined
}
const readableSize = await showSize(publicDirPath)
if (!readableSize) {
return undefined
}
let successfulRemotePinners = []
let pinnedHashes = {}
if (remotePinners.includes('infura')) {
const infuraHash = await addToInfura(publicDirPath)
if (infuraHash) {
successfulRemotePinners = successfulRemotePinners.concat(['infura'])
Object.assign(pinnedHashes, { infuraHash })
}
}
if (remotePinners.includes('pinata')) {
const ipfsNode = await startIpfsNode(port)
const localHash = await pinToTmpIpfsNode(ipfsNode, publicDirPath)
const pinataHash = await pinToPinata(
ipfsNode,
credentials.pinata,
{ name: siteDomain },
localHash
)
if (pinataHash) {
successfulRemotePinners = successfulRemotePinners.concat(['pinata'])
Object.assign(pinnedHashes, { localHash, pinataHash })
}
await stopIpfsNode(ipfsNode)
}
if (successfulRemotePinners.length > 0) {
const pinnedHash = Object.values(pinnedHashes)[0]
const gatewayUrl = httpGatewayUrl(pinnedHash, successfulRemotePinners[0])
const isEqual = hash => hash === pinnedHash
if (!fp.every(isEqual)(Object.values(pinnedHashes))) {
const spinner = ora()
spinner.fail('≠ Found inconsistency in pinned hashes:')
logError(pinnedHashes)
}
if (copyHttpGatewayUrlToClipboard) {
copyUrlToClipboard(gatewayUrl)
}
if (dnsProviders.includes('cloudflare')) {
await updateCloudflareDns(siteDomain, credentials.cloudflare, pinnedHash)
}
if (open && _.isEmpty(dnsProviders)) {
await openUrl(gatewayUrl)
}
if (open && !_.isEmpty(dnsProviders)) {
await openUrl(`https://${siteDomain}`)
}
return pinnedHash
} else {
logError('Failed to deploy.')
return undefined
}
}
module.exports = deploy