-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (36 loc) · 1.4 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
const core = require('@actions/core')
const github = require('@actions/github')
const fs = require('fs')
const { ArweaveClient } = require('ar-wrapper')
async function main() {
try {
const addr = core.getInput('wallet-address')
const key = core.getInput('wallet-key')
const path = core.getInput('document-path')
if (!(addr && key && path)) {
core.setFailed("document-path, wallet-address, and wallet-key all need to be passed!")
return
}
// init client
const client = new ArweaveClient(addr, key)
const docName = `${github.context.payload.repository.name}:${path}`
console.log(`Initialized new client. Looking for document ${docName}`)
// get new content
const data = fs.readFileSync(path, 'utf8')
// todo: maybe hash to fixed size?
const docs = await client.getDocumentsByName(docName)
const latestDoc = docs.sort((a, b) => b.version - a.version)[0]
if (latestDoc) {
console.log(`Found document with version ${latestDoc.version}!`)
} else {
console.log(`No existing document found. Publishing version 1...`)
}
// update/add doc and return txId
const newDoc = latestDoc ? await latestDoc.update(data) : await client.addDocument(docName, data, {})
console.log(`Success! New tx posted with id ${newDoc.txID}`)
core.setOutput("txId", newDoc.txID)
} catch (error) {
core.setFailed(error.message)
}
}
main()