@@ -15,6 +15,7 @@ const { logError } = require('./src/logging')
1515
1616const httpGatewayUrl = require ( './src/gateway' )
1717const { setupPinata } = require ( './src/pinata' )
18+ const { linkCid, linkUrl } = require ( './src/utils/pure-fns' )
1819
1920const white = chalk . whiteBright
2021
@@ -64,48 +65,81 @@ async function openUrl(url) {
6465 const spinner = ora ( )
6566 spinner . start ( 'π Opening web browserβ¦' )
6667 const childProcess = await doOpen ( url )
67- spinner . succeed ( 'π Opened web browser (call with -O to disable.)' )
68+ spinner . succeed ( 'π Opened URL on web browser (call with -O to disable):' )
69+ spinner . info ( linkUrl ( url ) )
6870 return childProcess
6971}
7072
71- async function updateCloudflareDns ( siteDomain , { apiEmail, apiKey } , hash ) {
73+ // returns (sub)domain deployed to or null when error
74+ async function updateCloudflareDns (
75+ siteDomain ,
76+ { apiEmail, apiKey, zone, record } ,
77+ hash
78+ ) {
79+ let result
7280 const spinner = ora ( )
7381
7482 spinner . start ( `π‘ Beaming new hash to DNS provider ${ white ( 'Cloudflare' ) } β¦` )
75- if ( fp . some ( _ . isEmpty ) ( [ siteDomain , apiEmail , apiKey ] ) ) {
83+ if ( fp . some ( _ . isEmpty ) ( [ apiEmail , apiKey ] ) ) {
7684 spinner . fail ( 'π Missing arguments for Cloudflare API.' )
7785 spinner . warn ( 'π§ Check if these environment variables are present:' )
7886 logError ( `
79- IPFS_DEPLOY_SITE_DOMAIN
8087 IPFS_DEPLOY_CLOUDFLARE__API_EMAIL
8188 IPFS_DEPLOY_CLOUDFLARE__API_KEY
8289
90+ (Note the 2 '_' after "CLOUDFLARE".)
91+ You can put them in a .env file if you want and they will be picked up.
92+ ` )
93+ }
94+ if ( _ . isEmpty ( siteDomain ) && fp . some ( _ . isEmpty ) ( [ zone , record ] ) ) {
95+ spinner . fail ( 'π Missing arguments for Cloudflare API.' )
96+ spinner . warn ( 'π§ Check if these environment variables are present:' )
97+ logError ( `
98+ IPFS_DEPLOY_CLOUDFLARE__ZONE
99+ IPFS_DEPLOY_CLOUDFLARE__RECORD
100+
101+ (Note the 2 '_' after "CLOUDFLARE".)
83102 You can put them in a .env file if you want and they will be picked up.
103+
104+ Example with top-level domain:
105+ IPFS_DEPLOY_CLOUDFLARE__ZONE=agentofuser.com
106+ IPFS_DEPLOY_CLOUDFLARE__RECORD=_dnslink.agentofuser.com
107+
108+ Example with subdomain:
109+ IPFS_DEPLOY_CLOUDFLARE__ZONE=agentofuser.com
110+ IPFS_DEPLOY_CLOUDFLARE__RECORD=_dnslink.test.agentofuser.com
84111 ` )
112+ result = null
85113 } else {
86- try {
87- const api = {
88- email : apiEmail ,
89- key : apiKey ,
90- }
114+ const api = {
115+ email : apiEmail ,
116+ key : apiKey ,
117+ }
91118
92- const opts = {
93- record : siteDomain ,
94- zone : siteDomain ,
95- link : `/ipfs/${ hash } ` ,
96- }
119+ const opts = {
120+ zone : zone || siteDomain ,
121+ record : record || `_dnslink. ${ siteDomain } ` ,
122+ link : `/ipfs/${ hash } ` ,
123+ }
97124
125+ try {
98126 const content = await updateCloudflareDnslink ( api , opts )
99127 spinner . succeed ( 'π SUCCESS!' )
100128 spinner . info ( `π Updated DNS TXT ${ white ( opts . record ) } to:` )
101129 spinner . info ( `π ${ white ( content ) } ` )
130+
131+ result = opts . record
132+ . split ( '.' )
133+ . slice ( 1 )
134+ . join ( '.' )
102135 } catch ( e ) {
103136 spinner . fail ( "π Updating Cloudflare DNS didn't work." )
104137 logError ( e )
138+ result = null
105139 }
106-
107- return siteDomain
108140 }
141+
142+ return result
109143}
110144
111145async function showSize ( path ) {
@@ -118,7 +152,9 @@ async function showSize(path) {
118152 } )
119153 const kibi = byteSize ( size , { units : 'iec' } )
120154 const readableSize = `${ kibi . value } ${ kibi . unit } `
121- spinner . succeed ( `π ${ chalk . blue ( path ) } weighs ${ readableSize } .` )
155+ spinner . succeed (
156+ `π Directory ${ chalk . blue ( path ) } weighs ${ readableSize } .`
157+ )
122158 return readableSize
123159 } catch ( e ) {
124160 spinner . fail ( "β Couldn't calculate website size." )
@@ -144,9 +180,9 @@ async function addToInfura(publicDirPath) {
144180 recursive : true ,
145181 } )
146182 spinner . succeed ( "π It's pinned to Infura now with hash:" )
147- const hash = response [ response . length - 1 ] . hash
148- spinner . info ( `π ${ hash } ` )
149- return hash
183+ const pinnedHash = response [ response . length - 1 ] . hash
184+ spinner . info ( linkCid ( pinnedHash , 'infura' ) )
185+ return pinnedHash
150186 } catch ( e ) {
151187 spinner . fail ( "π Uploading to Infura didn't work." )
152188 logError ( e )
@@ -160,7 +196,7 @@ function copyUrlToClipboard(url) {
160196 try {
161197 clipboardy . writeSync ( url )
162198 spinner . succeed ( 'π Copied HTTP gateway URL to clipboard:' )
163- spinner . info ( `π ${ chalk . green ( url ) } ` )
199+ spinner . info ( linkUrl ( url ) )
164200 return url
165201 } catch ( e ) {
166202 spinner . fail ( 'β οΈ Could not copy URL to clipboard.' )
@@ -180,6 +216,8 @@ async function deploy({
180216 cloudflare : {
181217 apiEmail,
182218 apiKey,
219+ zone,
220+ record,
183221 } ,
184222 pinata : {
185223 apiKey,
@@ -213,7 +251,10 @@ async function deploy({
213251 if ( remotePinners . includes ( 'pinata' ) ) {
214252 const addToPinata = setupPinata ( credentials . pinata )
215253 const pinataHash = await addToPinata ( publicDirPath , {
216- name : siteDomain || __dirname ,
254+ name :
255+ ( credentials . cloudflare && credentials . cloudflare . record ) ||
256+ siteDomain ||
257+ __dirname ,
217258 } )
218259
219260 if ( pinataHash ) {
@@ -224,30 +265,43 @@ async function deploy({
224265
225266 if ( successfulRemotePinners . length > 0 ) {
226267 const pinnedHash = Object . values ( pinnedHashes ) [ 0 ]
227- const isEqual = hash => hash === pinnedHash
268+ const isEqual = pinnedHash => pinnedHash === pinnedHash
228269 if ( ! fp . every ( isEqual ) ( Object . values ( pinnedHashes ) ) ) {
229270 const spinner = ora ( )
230271 spinner . fail ( 'β Found inconsistency in pinned hashes:' )
231272 logError ( pinnedHashes )
232273 return undefined
233274 }
234275
235- const gatewayUrl = httpGatewayUrl ( pinnedHash , successfulRemotePinners [ 0 ] )
236-
237- if ( copyHttpGatewayUrlToClipboard ) {
238- copyUrlToClipboard ( gatewayUrl )
239- }
240-
276+ let dnslinkedHostname
241277 if ( dnsProviders . includes ( 'cloudflare' ) ) {
242- await updateCloudflareDns ( siteDomain , credentials . cloudflare , pinnedHash )
278+ dnslinkedHostname = await updateCloudflareDns (
279+ siteDomain ,
280+ credentials . cloudflare ,
281+ pinnedHash
282+ )
243283 }
244284
245- if ( open && _ . isEmpty ( dnsProviders ) ) {
246- await openUrl ( gatewayUrl )
285+ const gatewayUrls = successfulRemotePinners . map ( pinner =>
286+ httpGatewayUrl ( pinnedHash , pinner )
287+ )
288+
289+ if ( open ) {
290+ gatewayUrls . forEach ( async gatewayUrl => await openUrl ( gatewayUrl ) )
291+
292+ if ( dnslinkedHostname ) {
293+ await openUrl ( `https://${ dnslinkedHostname } ` )
294+ }
247295 }
248- if ( open && ! _ . isEmpty ( dnsProviders ) ) {
249- await openUrl ( `https://${ siteDomain } ` )
296+
297+ if ( copyHttpGatewayUrlToClipboard ) {
298+ if ( dnslinkedHostname ) {
299+ copyUrlToClipboard ( `https://${ dnslinkedHostname } ` )
300+ } else {
301+ copyUrlToClipboard ( gatewayUrls [ 0 ] )
302+ }
250303 }
304+
251305 return pinnedHash
252306 } else {
253307 logError ( 'Failed to deploy.' )
0 commit comments