-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathpublish.js
More file actions
85 lines (69 loc) · 2.36 KB
/
publish.js
File metadata and controls
85 lines (69 loc) · 2.36 KB
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
'use strict'
const Command = require('ronin').Command
const fs = require('fs')
const ipfsAPI = require('ipfs-api')
const path = require('path')
module.exports = Command.extend({
desc: 'Publish your project',
run: function (name) {
let configPath
try {
configPath = path.resolve(process.cwd() + '/ipscend.json')
fs.statSync(configPath)
publish()
} catch (err) {
console.log('Project must be initiated first, run `ipscend init`')
}
function publish () {
const config = require(configPath)
let host = config.provider.host || 'ipfs.infura.io'
let port = config.provider.port || '5001'
let opts = config.provider.opts || { protocol:'https' }
const ipfs = ipfsAPI(host, port, opts)
ipfs.util.addFromFs(config.path, {
recursive: true,
'stream-channels': false
}, (err, res) => {
if (err || !res) {
return console.error('err', err)
}
console.log()
console.log('Uploaded files:')
console.log(res)
console.log()
console.log()
let hash = ''
for(let k in res){
if (res[k].path==config.path) {
hash = res[k].hash
}
}
const duplicate = config.versions.filter(function (v) {
return v.hash === hash
})[0]
if (duplicate) {
console.log('This version (' + duplicate.hash + ') has already been published on:', duplicate.timestamp)
console.log('You can access it by url http://ipfs.io/ipfs/' + duplicate.hash)
console.log()
return
}
const version = {
hash: hash,
timestamp: new Date()
}
console.log('Published', config.path, 'with the following hash:', version.hash)
console.log('You can access it through your local node or through a public IPFS gateway:')
if (config.provider.host=='localhost') {
console.log('http://'+config.provider.host+':8080/ipfs/' + version.hash)
}
console.log('https://ipfs.io/ipfs/' + version.hash)
console.log(' OR ')
console.log('https://ipfs.infura.io/ipfs/' + version.hash)
console.log()
config.versions.push(version)
const fd = fs.openSync(configPath, 'w')
fs.writeSync(fd, JSON.stringify(config, null, ' '), 0, 'utf-8')
})
}
}
})