UNPKG

@acurast/cli

Version:

A cli to interact with the Acurast Cloud.

52 lines 2.39 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import axios from 'axios'; import FormDataModule from 'form-data'; import fs from 'fs'; import { getEnv } from '../config.js'; export const uploadScript = (config) => __awaiter(void 0, void 0, void 0, function* () { const tempFile = 'temp_script.js'; if ('file' in config) { fs.copyFileSync(config.file, tempFile); } else { // Convert the script string to a buffer const buffer = Buffer.from(config.script, 'utf-8'); fs.writeFileSync(tempFile, buffer); } // Use form-data to create the form const form = new FormDataModule(); form.append('file', fs.createReadStream(tempFile), 'script.js'); form.append('pinataOptions', '{"cidVersion": 0}'); form.append('pinataMetadata', '{"name": "script.js"}'); // TODO: name of project? const environment = { ipfsUrl: getEnv('ACURAST_IPFS_URL'), ipfsApiKey: getEnv('ACURAST_IPFS_API_KEY'), }; try { const res = yield axios.post(`${environment.ipfsUrl}/pinning/pinFileToIPFS`, form, { headers: Object.assign(Object.assign({}, form.getHeaders()), { Authorization: `Bearer ${environment.ipfsApiKey}` }), }); // Clean up temp file fs.unlinkSync(tempFile); return `ipfs://${res.data.IpfsHash}`; } catch (error) { console.error('Error uploading script:', error); // Clean up temp file in case of error fs.unlinkSync(tempFile); throw error; } }); // // Example usage // uploadScript({ script: 'console.log("Hello World");' }) // .then((hash) => console.log("Uploaded script hash:", hash)) // .catch((error) => console.error("Error:", error)); //# sourceMappingURL=uploadToIpfs.js.map