-
Notifications
You must be signed in to change notification settings - Fork 15
/
ipfs-deploy.js
65 lines (55 loc) · 2.26 KB
/
ipfs-deploy.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
const axios = require('axios')
const fs = require('fs-extra')
const FormData = require('form-data')
const recursive = require('recursive-fs')
var pinataKey = process.env.PINATA_API_KEY;
var pinataPrivateKey = process.env.PINATA_SECRET_API_KEY;
if (process.env.PINATA_API_KEY == undefined || process.env.PINATA_SECRET_API_KEY == undefined) {
const config = require('./config.js')
pinataKey = config.PINATA_API_KEY
pinataPrivateKey = config.PINATA_SECRET_API_KEY
}
const deploy = async () => {
const url = 'https://api.pinata.cloud/pinning/pinFileToIPFS'
// we gather the files from a local directory in this example, but a valid
// readStream is all that's needed for each file in the directory.
try {
const response = await new Promise(resolve => {
console.log('deploying...');
recursive.readdirr("./dist", (_err, _dirs, files) => {
let data = new FormData()
files.forEach(file => {
data.append('file', fs.createReadStream(file), {
// for each file stream, we need to include the correct
// relative file path
filepath: file,
})
})
const metadata = JSON.stringify({
name: 'ipfsDeploy',
});
data.append('pinataMetadata', metadata)
axios
.post(url, data, {
// Infinity is needed to prevent axios from erroring out with
// large directories
maxContentLength: 'Infinity',
headers: {
'Content-Type': `multipart/form-data; boundary=${data._boundary}`,
pinata_api_key: pinataKey,
pinata_secret_api_key: pinataPrivateKey
},
})
.then(resolve)
})
})
const pinnedHash = response.data.IpfsHash
console.info('Deployed with hash: ', pinnedHash)
return pinnedHash
} catch (e) {
console.info("Uploading to Pinata didn't work.")
console.info(e)
return undefined
}
}
deploy();