Skip to content

Commit 726fd8d

Browse files
committed
feat: Add --help and parameterize CLI and library
ipfs-deploy [options] path Pin path locally, upload to pinning service, and update DNS Positionals: path The path to deploy [string] [default: "./public/"] Options: --version Show version number [boolean] --help Show help [boolean] -D DON'T update Cloudflare DNS' TXT dnslink [boolean] [default: false] -o Open URL after deploying [boolean] [default: false] -p, --pinner Pinning services to which path will be uploaded [choices: "pinata", "infura"] [default: ["pinata","infura"]] -P DON'T pin remotely, only to local daemon (overrides -p) [boolean] [default: false] Examples: ipfs-deploy _site # Deploys path "_site" to pinata and infura, and updates cloudflare DNS ipfs-deploy -p infura -p pinata # Deploys path "./public/" to pinata and infura, and updates cloudflare DNS ipfs-deploy -p pinata static # Deploys path "static" ONLY to pinata and updates cloudflare DNS ipfs-deploy -D docs # Deploys path "docs" to pinata and infura, and DON'T update DNS
1 parent 51db254 commit 726fd8d

File tree

4 files changed

+149
-20
lines changed

4 files changed

+149
-20
lines changed

bin/ipfs-deploy.js

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,90 @@
11
#!/usr/bin/env node
2+
const chalk = require('chalk')
23

34
const deploy = require('../index')
45

5-
deploy()
6+
const argv = require('yargs')
7+
.scriptName('ipfs-deploy')
8+
.usage(
9+
'$0 [options] path',
10+
'Pin path locally, upload to pinning service, and update DNS',
11+
yargs => {
12+
yargs
13+
.positional('path', {
14+
type: 'string',
15+
default: './public/',
16+
describe: 'The path to deploy',
17+
})
18+
.options({
19+
D: {
20+
type: 'boolean',
21+
default: false,
22+
describe: "DON'T update Cloudflare DNS' TXT dnslink",
23+
},
24+
o: {
25+
type: 'boolean',
26+
default: false,
27+
describe: 'Open URL after deploying',
28+
},
29+
p: {
30+
alias: 'pinner',
31+
choices: ['pinata', 'infura'],
32+
default: ['pinata', 'infura'],
33+
describe: `Pinning services to which ${chalk.whiteBright(
34+
'path'
35+
)} will be uploaded`,
36+
},
37+
P: {
38+
type: 'boolean',
39+
default: false,
40+
describe:
41+
"DON'T pin remotely, only to local daemon (overrides -p)",
42+
},
43+
})
44+
.example(
45+
'$0 _site',
46+
`# Deploys path "${chalk.whiteBright(
47+
'_site'
48+
)}" to ${chalk.whiteBright('pinata')} and ${chalk.whiteBright(
49+
'infura'
50+
)}, and updates ${chalk.whiteBright('cloudflare')} DNS`
51+
)
52+
.example(
53+
'$0 -p infura -p pinata',
54+
`# Deploys path "${chalk.whiteBright(
55+
'./public/'
56+
)}" to ${chalk.whiteBright('pinata')} and ${chalk.whiteBright(
57+
'infura'
58+
)}, and updates ${chalk.whiteBright('cloudflare')} DNS`
59+
)
60+
.example(
61+
'$0 -p pinata static',
62+
`# Deploys path "${chalk.whiteBright(
63+
'static'
64+
)}" ONLY to ${chalk.whiteBright(
65+
'pinata'
66+
)} and updates ${chalk.whiteBright('cloudflare')} DNS`
67+
)
68+
.example(
69+
'$0 -D docs',
70+
`# Deploys path "${chalk.whiteBright(
71+
'docs'
72+
)}" to ${chalk.whiteBright('pinata')} and ${chalk.whiteBright(
73+
'infura'
74+
)}, and ${chalk.whiteBright("DON'T")} update DNS`
75+
)
76+
}
77+
)
78+
.help().argv
79+
80+
function main() {
81+
deploy({
82+
updateDns: !argv.D,
83+
open: argv.o,
84+
// pinners: argv.p, TODO
85+
// pinRemotely: !argv.P, TODO
86+
publicDirPath: argv.path,
87+
})
88+
}
89+
90+
main()

index.js

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ const pinataSDK = require('@pinata/sdk')
55
const got = require('got')
66
const updateCloudflareDnslink = require('dnslink-cloudflare')
77
const ora = require('ora')
8+
const chalk = require('chalk')
9+
const openUrl = require('open')
810

911
require('dotenv').config()
1012

11-
async function updateDns(hash) {
13+
async function doUpdateDns(hash) {
1214
const key = process.env.CF_API_KEY
1315
const email = process.env.CF_API_EMAIL
1416
const domain = process.env.SITE_DOMAIN
1517

1618
const spinner = ora()
1719

1820
if (!key || !email || !domain || !hash) {
19-
throw new Error('Missing information for updateDns()')
21+
throw new Error('Missing information for doUpdateDns()')
2022
}
2123

2224
const api = {
@@ -31,17 +33,29 @@ async function updateDns(hash) {
3133
}
3234

3335
try {
34-
spinner.info('📡 Beaming new hash to DNS provider...')
36+
spinner.info(
37+
`📡 Beaming new hash to DNS provider ${chalk.whiteBright(
38+
'Cloudflare'
39+
)}...`
40+
)
3541
const content = await updateCloudflareDnslink(api, opts)
36-
spinner.succeed(`🙌 SUCCESS! Updated TXT ${opts.record} to ${content}.`)
37-
spinner.succeed('🌐 Your website is deployed now :)')
42+
spinner.succeed('🙌 SUCCESS!')
43+
spinner.info(`Updated TXT ${chalk.whiteBright(opts.record)} to:`)
44+
spinner.info(`${chalk.whiteBright(content)}.`)
45+
spinner.succeed('🌐 Your website is deployed now.')
3846
} catch (err) {
3947
console.log(err)
4048
process.exit(1)
4149
}
4250
}
4351

44-
async function main() {
52+
async function deploy({
53+
updateDns = true,
54+
open = false,
55+
// pinners = ['pinata', 'infura'], TODO
56+
// pinRemotely = true, TODO
57+
publicDirPath = 'public',
58+
} = {}) {
4559
const ipfsBinAbsPath =
4660
which.sync('ipfs', { nothrow: true }) ||
4761
which.sync('jsipfs', { nothrow: true })
@@ -57,17 +71,25 @@ async function main() {
5771

5872
ipfsd.start([], (err2, ipfsClient) => {
5973
if (err2) throw err2
60-
spinner.succeed('📶 Connected.')
74+
// spinner.succeed('📶 Connected.')
75+
76+
spinner.info(
77+
`💾 Adding and pinning ${chalk.blue(publicDirPath)} locally...`
78+
)
6179

62-
spinner.info('💾 Adding and pinning ./public/ locally...')
6380
ipfsClient.addFromFs(
64-
'public',
81+
publicDirPath,
6582
{ recursive: true },
6683
(err3, localPinResult) => {
67-
if (err3) throw err3
84+
if (err3) {
85+
spinner.fail(
86+
"☠ Couldn't connect to local ipfs daemon. Is it running?"
87+
)
88+
throw err3
89+
}
6890

6991
const { hash } = localPinResult[localPinResult.length - 1]
70-
spinner.succeed(`🔗 Added locally as ${hash}.`)
92+
spinner.succeed(`🔗 Added locally as ${chalk.green(hash)}.`)
7193

7294
ipfsClient.id((err4, { addresses }) => {
7395
if (err4) throw err4
@@ -94,14 +116,22 @@ async function main() {
94116
process.env.PINATA_SECRET_API_KEY
95117
)
96118

97-
spinner.info('📠 Requesting remote pin to pinata.cloud...')
119+
spinner.info(
120+
`📠 Requesting remote pin to ${chalk.whiteBright(
121+
'pinata.cloud'
122+
)}...`
123+
)
98124
pinata
99125
.pinHashToIPFS(hash, pinataOptions)
100126
.then(async _pinataPinResult => {
101127
spinner.succeed("📌 It's pinned to Pinata now.")
102128

103129
try {
104-
spinner.info('📠 Requesting remote pin to infura.io...')
130+
spinner.info(
131+
`📠 Requesting remote pin to ${chalk.whiteBright(
132+
'infura.io'
133+
)}.`
134+
)
105135
const infuraResponse = await got(
106136
`https://ipfs.infura.io:5001/api/v0/pin/add?arg=${hash}` +
107137
'&recursive=true'
@@ -117,9 +147,13 @@ async function main() {
117147
}
118148

119149
clipboardy.writeSync(hash)
120-
spinner.succeed(`📋 Hash ${hash} copied to clipboard.`)
150+
spinner.succeed(
151+
`📋 Hash ${chalk.green(hash)} copied to clipboard.`
152+
)
153+
154+
if (updateDns) doUpdateDns(hash)
121155

122-
updateDns(hash)
156+
if (open) openUrl(`https://${process.env.SITE_DOMAIN}`)
123157
})
124158
.catch(err5 => {
125159
throw err5
@@ -131,4 +165,4 @@ async function main() {
131165
})
132166
}
133167

134-
module.exports = main
168+
module.exports = deploy

package-lock.json

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@
3030
},
3131
"dependencies": {
3232
"@pinata/sdk": "^1.0.16",
33+
"chalk": "^2.4.2",
3334
"clipboardy": "^1.2.3",
3435
"dnslink-cloudflare": "^2.0.1",
3536
"dotenv": "^7.0.0",
3637
"got": "^9.6.0",
3738
"ipfs": "^0.35.0-rc.4",
3839
"ipfsd-ctl": "^0.42.1",
40+
"open": "^6.0.0",
3941
"ora": "^3.4.0",
40-
"which": "^1.3.1"
42+
"which": "^1.3.1",
43+
"yargs": "^13.2.2"
4144
},
4245
"devDependencies": {
4346
"@semantic-release/changelog": "^3.0.2",

0 commit comments

Comments
 (0)