-
-
Notifications
You must be signed in to change notification settings - Fork 449
/
Copy pathpublish.js
152 lines (130 loc) · 4.93 KB
/
publish.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
module.exports = {
name: 'publish',
command: publish,
help: [
'Publish your dat to a Dat registry',
'Usage: dat publish [<registry>]',
'',
'By default it will publish to your active registry.',
'Specify the server to change where the dat is published.'
].join('\n'),
options: [
{
name: 'server',
help: 'Publish dat to this registry. Defaults to active login.'
}
]
}
function publish (opts) {
var path = require('path')
var Dat = require('dat-node')
var encoding = require('dat-encoding')
var output = require('neat-log/output')
var prompt = require('prompt')
var chalk = require('chalk')
var DatJson = require('dat-json')
var xtend = Object.assign
var Registry = require('../registry')
if (!opts.dir) opts.dir = process.cwd()
if (opts._[0]) opts.server = opts._[0]
if (!opts.server) opts.server = 'datbase.org' // nicer error message if not logged in
var client = Registry(opts)
var whoami = client.whoami()
if (!whoami || !whoami.token) {
var loginErr = output(`
Welcome to ${chalk.green(`dat`)} program!
Publish your dats to ${chalk.green(opts.server)}.
${chalk.bold('Please login before publishing')}
${chalk.green('dat login')}
New to ${chalk.green(opts.server)} and need an account?
${chalk.green('dat register')}
Explore public dats at ${chalk.blue('datbase.org/explore')}
`)
return exitErr(loginErr)
}
opts.createIfMissing = false // publish must always be a resumed archive
Dat(opts.dir, opts, function (err, dat) {
if (err && err.name === 'MissingError') return exitErr('No existing dat in this directory. Create a dat before publishing.')
else if (err) return exitErr(err)
dat.joinNetwork() // join network to upload metadata
var datjson = DatJson(dat.archive, { file: path.join(dat.path, 'dat.json') })
datjson.read(publish)
function publish (_, data) {
// ignore datjson.read() err, we'll prompt for name
// xtend dat.json with opts
var datInfo = xtend({
name: opts.name,
url: 'dat://' + encoding.toStr(dat.key), // force correct url in publish? what about non-dat urls?
title: opts.title,
description: opts.description
}, data)
var welcome = output(`
Publishing dat to ${chalk.green(opts.server)}!
`)
console.log(welcome)
if (datInfo.name) return makeRequest(datInfo)
prompt.message = ''
prompt.start()
prompt.get({
properties: {
name: {
description: chalk.magenta('dat name'),
pattern: /^[a-zA-Z0-9-]+$/,
message: `A dat name can only have letters, numbers, or dashes.\n Like ${chalk.bold('cool-cats-12meow')}`,
required: true
}
}
}, function (err, results) {
if (err) return exitErr(err)
datInfo.name = results.name
makeRequest(datInfo)
})
}
function makeRequest (datInfo) {
console.log(`Please wait, '${chalk.bold(datInfo.name)}' will soon be ready for its great unveiling...`)
client.dats.create(datInfo, function (err, resp, body) {
if (err) {
if (err.message) {
if (err.message === 'timed out') {
return exitErr(output(`${chalk.red('\nERROR: ' + opts.server + ' could not connect to your computer.')}
Troubleshoot here: ${chalk.green('https://docs.datproject.org/troubleshooting#networking-issues')}
`))
}
var str = err.message.trim()
if (str === 'jwt expired') return exitErr(`Session expired, please ${chalk.green('dat login')} again`)
return exitErr('ERROR: ' + err.message) // node error
}
// server response errors
return exitErr('ERROR: ' + err.toString())
}
if (body.statusCode === 400) return exitErr(new Error(body.message))
datjson.write(datInfo, function (err) {
if (err) return exitErr(err)
// TODO: write published url to dat.json (need spec)
var msg = output(`
We ${body.updated === 1 ? 'updated' : 'published'} your dat!
${chalk.blue.underline(`${opts.server}/${whoami.username}/${datInfo.name}`)}
`)// TODO: get url back? it'd be better to confirm link than guess username/datname structure
console.log(msg)
if (body.updated === 1) {
console.log(output(`
${chalk.dim.green('Cool fact #21')}
${opts.server} will live update when you are sharing your dat!
You only need to publish again if your dat link changes.
`))
} else {
console.log(output(`
Remember to use ${chalk.green('dat share')} before sharing.
This will make sure your dat is available.
`))
}
process.exit(0)
})
})
}
})
}
function exitErr (err) {
console.error(err)
process.exit(1)
}