@@ -7,7 +7,7 @@ const got = require('got')
77const updateCloudflareDnslink = require ( 'dnslink-cloudflare' )
88const ora = require ( 'ora' )
99const chalk = require ( 'chalk' )
10- const openUrl = require ( 'open' )
10+ const doOpen = require ( 'open' )
1111const _ = 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+
2027async 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 ( / j s i p f s / ) ? '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