|
| 1 | +const core = require("@actions/core"); |
| 2 | +const exec = require("@actions/exec"); |
| 3 | +const jsonfile = require("jsonfile"); |
| 4 | + |
| 5 | +const commands = { |
| 6 | + uploadFile: "arweave deploy", |
| 7 | +}; |
| 8 | + |
| 9 | +const flags = { |
| 10 | + deploy: { |
| 11 | + ipfs: "--ipfs-publish", |
| 12 | + keyFile: "--key-file", |
| 13 | + package: "--package", |
| 14 | + skipConfirmation: "--force-skip-confirmation", |
| 15 | + }, |
| 16 | +}; |
| 17 | + |
| 18 | +const paths = { |
| 19 | + keyFile: "keyfile.json", |
| 20 | +}; |
| 21 | + |
| 22 | +const outputs = { |
| 23 | + arweaveUrl: "arweave_url", |
| 24 | + ipfsUrl: "ipfs_url", |
| 25 | + cost: "cost", |
| 26 | +}; |
| 27 | + |
| 28 | +async function run() { |
| 29 | + try { |
| 30 | + const inputs = { |
| 31 | + keyFileContent: JSON.parse(core.getInput("KEY_FILE_CONTENT")), |
| 32 | + filePath: core.getInput("FILE_PATH"), |
| 33 | + package: core.getInput("PACKAGE").toLowerCase() === "true", |
| 34 | + ipfs: core.getInput("IPFS").toLowerCase() === "true", |
| 35 | + }; |
| 36 | + |
| 37 | + jsonfile |
| 38 | + .writeFile(paths.keyFile, inputs.keyFileContent) |
| 39 | + .then(async () => { |
| 40 | + const args = [ |
| 41 | + inputs.filePath, |
| 42 | + flags.deploy.keyFile, |
| 43 | + paths.keyFile, |
| 44 | + flags.deploy.skipConfirmation, |
| 45 | + ...(inputs.package ? [flags.deploy.package] : []), |
| 46 | + ...(inputs.ipfs ? [flags.deploy.ipfs] : []), |
| 47 | + ]; |
| 48 | + |
| 49 | + let commandOutput = ""; |
| 50 | + const options = { |
| 51 | + listeners: { |
| 52 | + stdout: (data) => { |
| 53 | + commandOutput += data.toString(); |
| 54 | + }, |
| 55 | + }, |
| 56 | + }; |
| 57 | + |
| 58 | + await exec.exec(commands.uploadFile, args, options); |
| 59 | + |
| 60 | + const result = getResults(commandOutput, inputs.ipfs); |
| 61 | + |
| 62 | + core.setOutput(outputs.arweaveUrl, result.arweaveUrl); |
| 63 | + core.setOutput(outputs.ipfsUrl, result.ipfsUrl); |
| 64 | + core.setOutput(outputs.cost, result.cost); |
| 65 | + }) |
| 66 | + .catch((error) => { |
| 67 | + core.setFailed(error.message); |
| 68 | + }); |
| 69 | + } catch (error) { |
| 70 | + core.setFailed(error.message); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +function getResults(output, ipfs) { |
| 75 | + // Ugly function, but the output data is not structured, just a huuge string |
| 76 | + const splittedOutput = output.split("\n"); |
| 77 | + |
| 78 | + const indexes = { |
| 79 | + arweaveUrlIndex: splittedOutput.findIndex(e => e.includes("https://arweave.net")), |
| 80 | + ipfsUrlIndex: ipfs && splittedOutput.findIndex(e => e.includes("https://ipfs.io")), |
| 81 | + costIndex: splittedOutput.findIndex(e => e.includes("Price")), |
| 82 | + }; |
| 83 | + |
| 84 | + return { |
| 85 | + arweaveUrl: splittedOutput[indexes.arweaveUrlIndex], |
| 86 | + ipfsUrl: ipfs ? splittedOutput[indexes.ipfsUrlIndex] : "", |
| 87 | + cost: splittedOutput[indexes.costIndex].split(" ")[1], |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +run(); |
0 commit comments