Skip to content

Commit 2711044

Browse files
committed
feat: Spawn bundled daemon when ipfs not installed
1 parent aeff354 commit 2711044

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

index.js

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const got = require('got')
77
const updateCloudflareDnslink = require('dnslink-cloudflare')
88
const ora = require('ora')
99
const chalk = require('chalk')
10-
const openUrl = require('open')
10+
const doOpen = require('open')
1111
const _ = require('lodash')
1212

1313
// # Pure functions
@@ -17,6 +17,13 @@ function publicGatewayUrl(hash) {
1717

1818
// Effectful functions
1919

20+
function openUrl(url) {
21+
const spinner = ora()
22+
spinner.start('🏄 Opening web browser…')
23+
doOpen(url)
24+
spinner.succeed('🏄 Opened web browser (call with -O to disable.)')
25+
}
26+
2027
async function updateCloudflareDns(siteDomain, { apiEmail, apiKey }, hash) {
2128
const spinner = ora()
2229

@@ -71,21 +78,38 @@ async function deploy({
7178
},
7279
},
7380
} = {}) {
81+
const spinner = ora()
82+
7483
const ipfsBinAbsPath =
7584
which.sync('ipfs', { nothrow: true }) ||
7685
which.sync('jsipfs', { nothrow: true })
7786

78-
const df = IPFSFactory.create({ exec: ipfsBinAbsPath })
79-
80-
const spinner = ora()
81-
spinner.start('☎️ Connecting to local IPFS daemon…')
82-
83-
const spawn = util.promisify(df.spawn.bind(df))
84-
ipfsd = await spawn({ disposable: false, init: false, start: false })
85-
86-
const start = util.promisify(ipfsd.start.bind(ipfsd))
87-
const ipfsClient = await start([])
88-
spinner.succeed('☎️ Connected to local IPFS daemon.')
87+
let ipfsd
88+
let ipfsClient
89+
let killDaemonAfterDone = false
90+
if (ipfsBinAbsPath) {
91+
spinner.start('☎️ Connecting to local IPFS daemon…')
92+
const type = ipfsBinAbsPath.match(/jsipfs/) ? 'js' : 'go'
93+
const df = IPFSFactory.create({ type, exec: ipfsBinAbsPath })
94+
const spawn = util.promisify(df.spawn.bind(df))
95+
ipfsd = await spawn({ disposable: false, init: false, start: false })
96+
if (!ipfsd.started) {
97+
const start = util.promisify(ipfsd.start.bind(ipfsd))
98+
spinner.start('☎️ Starting local IPFS daemon…')
99+
ipfsClient = await start([])
100+
killDaemonAfterDone = true
101+
}
102+
spinner.succeed('☎️ Connected to local IPFS daemon.')
103+
} else {
104+
spinner.start('⏲️ Starting temporary IPFS daemon…\n')
105+
const df = IPFSFactory.create({ type: 'js' })
106+
const spawn = util.promisify(df.spawn.bind(df))
107+
ipfsd = await spawn({ disposable: true, init: true, start: false })
108+
const start = util.promisify(ipfsd.start.bind(ipfsd))
109+
ipfsClient = await start([])
110+
killDaemonAfterDone = true
111+
spinner.succeed('☎️ Connected to temporary IPFS daemon.')
112+
}
89113

90114
spinner.start('🔗 Pinning to local IPFS…')
91115
const localPinResult = await ipfsClient.addFromFs(publicDirPath, {
@@ -145,9 +169,17 @@ async function deploy({
145169
}
146170
}
147171

172+
if (killDaemonAfterDone) {
173+
const stop = util.promisify(ipfsd.stop.bind(ipfsd))
174+
// spinner.start('✋️ Stopping IPFS daemon…')
175+
await stop()
176+
// spinner.succeed('✋️ Stopped IPFS daemon.')
177+
}
178+
179+
spinner.start('📋 Copying public gateway URL to clipboard…')
148180
if (copyPublicGatewayUrlToClipboard)
149181
clipboardy.writeSync(publicGatewayUrl(hash))
150-
spinner.succeed('📋 Public gateway URL copied to clipboard.')
182+
spinner.succeed('📋 Copied public gateway URL to clipboard.')
151183

152184
if (dnsProviders.includes('cloudflare'))
153185
await updateCloudflareDns(siteDomain, credentials.cloudflare, hash)

0 commit comments

Comments
 (0)