-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
82 lines (74 loc) · 3.04 KB
/
index.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
const path = require('path')
const fs = require('fs')
const semver = require('semver')
const core = require('@actions/core')
const tc = require('@actions/tool-cache')
const exec = require('@actions/exec')
const cp = require('child_process')
const MAXATTEMPTS = 30
const IPFSAPI = 'http://localhost:5001/api/v0/version'
const IPFSVERS = 'https://dist.ipfs.io/go-ipfs/versions'
const ISWIN = process.platform === 'win32'
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
async function ipfsDistVersion(version) {
const ipfsVersPath = await tc.downloadTool(IPFSVERS)
return fs.readFileSync(ipfsVersPath, 'utf8').trim().split('\n').filter(v => semver.satisfies(v, version)).sort(semver.rcompare)[0] || version
}
function ipfsDistUrl(version) {
const os = ISWIN ? 'windows' : process.platform
const arch = process.arch === 'x32' ? '386' : process.arch === 'x64' ? 'amd64' : process.arch
const pkg = ISWIN ? 'zip' : 'tar.gz'
return `https://dist.ipfs.io/go-ipfs/${version}/go-ipfs_${version}_${os}-${arch}.${pkg}`
}
async function run() {
try {
const ipfsVer = core.getInput('ipfs_version')
const runDaemon = core.getInput('run_daemon')
const ipfsDistVer = await ipfsDistVersion(ipfsVer)
const ipfsDownloadUrl = ipfsDistUrl(ipfsDistVer)
core.setOutput('resolved_ipfs_version', ipfsDistVer.replace(/^v/, ''))
core.setOutput('ipfs_download_url', ipfsDownloadUrl)
const ipfsPkgPath = await tc.downloadTool(ipfsDownloadUrl)
const ipfsExtractedFolder = ISWIN ? await tc.extractZip(ipfsPkgPath) : await tc.extractTar(ipfsPkgPath)
const ipfsPath = await tc.cacheDir(ipfsExtractedFolder, 'ipfs', ipfsDistVer);
core.addPath(path.join(ipfsPath, 'go-ipfs'))
const opts = {
ignoreReturnCode: true,
listeners: {
stdout: data => {
let msg = data.toString()
try {
core.setOutput('peer_id', msg.match(/peer identity: (\w+)/)[1])
} catch (error) {
// Do nothing
}
try {
core.setOutput('welcome_ref', msg.match(/ipfs cat \/ipfs\/(\w+)\/readme/)[1])
} catch (error) {
// Do nothing
}
}
}
}
await exec.exec('ipfs', ['init'], opts)
if (runDaemon) {
const daemon = cp.spawn('ipfs', ['daemon'], {detached: true, stdio: 'ignore'})
daemon.unref()
let attemptsLeft = MAXATTEMPTS
while (--attemptsLeft) {
try {
await exec.exec('curl', ['-s', '-X', 'POST', IPFSAPI])
break
} catch (error) {
await sleep(1000)
}
}
if (!attemptsLeft) {
throw new Error('IPFS API service unreachable')
}
}
} catch (error) {
core.setFailed(error.message);
}
}
run()