UNPKG

@apillon/cli

Version:
107 lines 4.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deleteCloudFunctionJob = exports.listCloudFunctionJobs = exports.setCloudFunctionEnvironment = exports.createCloudFunctionJob = exports.createCloudFunction = exports.getCloudFunction = exports.listCloudFunctions = void 0; /* eslint-disable security/detect-non-literal-fs-filename */ const sdk_1 = require("@apillon/sdk"); const fs_1 = require("fs"); const options_1 = require("../../lib/options"); const utils_1 = require("../../lib/utils"); const sdk_2 = require("@apillon/sdk"); async function listCloudFunctions(optsWithGlobals) { await (0, utils_1.withErrorHandler)(async () => { const data = await new sdk_1.CloudFunctions(optsWithGlobals).listCloudFunctions(Object.assign({}, (0, options_1.paginate)(optsWithGlobals))); console.log(data.items.map((d) => d.serialize())); }); } exports.listCloudFunctions = listCloudFunctions; async function getCloudFunction(optsWithGlobals) { await (0, utils_1.withErrorHandler)(async () => { const cloudFunction = await new sdk_1.CloudFunctions(optsWithGlobals) .cloudFunction(optsWithGlobals.uuid) .get(); cloudFunction.jobs = cloudFunction.jobs.map((job) => job.serialize()); console.info(cloudFunction.serialize()); }); } exports.getCloudFunction = getCloudFunction; async function createCloudFunction(optsWithGlobals) { await (0, utils_1.withErrorHandler)(async () => { const cloudFunctions = new sdk_1.CloudFunctions(optsWithGlobals); const data = await cloudFunctions.createCloudFunction({ name: optsWithGlobals.name, description: optsWithGlobals.description, }); console.log(data.serialize()); console.log('Cloud function created successfully!'); }); } exports.createCloudFunction = createCloudFunction; async function createCloudFunctionJob(optsWithGlobals) { await (0, utils_1.withErrorHandler)(async () => { let scriptContent; const script = optsWithGlobals.script; if (!script.endsWith('.js')) { return console.error('The script file must have a .js extension.'); } try { scriptContent = (0, fs_1.readFileSync)(script, 'utf-8'); } catch (e) { return e.code === 'ENOENT' ? console.error(`Error: Script file not found (${script}).`) : console.error(e); } const cloudFunction = await new sdk_1.CloudFunctions(optsWithGlobals) .cloudFunction(optsWithGlobals.uuid) .get(); // Upload the script to IPFS const files = await new sdk_2.Storage(optsWithGlobals) .bucket(cloudFunction.bucketUuid) .uploadFiles([ { fileName: script.split('/').pop(), content: Buffer.from(scriptContent, 'utf-8'), contentType: 'application/javascript', }, ], { awaitCid: true }); const job = await cloudFunction.createJob({ name: optsWithGlobals.name, scriptCid: files[0].CID, }); console.log(job.serialize()); console.log('Cloud function job created successfully!'); }); } exports.createCloudFunctionJob = createCloudFunctionJob; async function setCloudFunctionEnvironment(optsWithGlobals) { await (0, utils_1.withErrorHandler)(async () => { const variables = optsWithGlobals.variables.split(',').map((v) => { const [key, value] = v.split('='); return { key, value }; }); await new sdk_1.CloudFunctions(optsWithGlobals) .cloudFunction(optsWithGlobals.uuid) .setEnvironment(variables); console.log('Environment variables set successfully!'); }); } exports.setCloudFunctionEnvironment = setCloudFunctionEnvironment; async function listCloudFunctionJobs(optsWithGlobals) { await (0, utils_1.withErrorHandler)(async () => { const data = await new sdk_1.CloudFunctions(optsWithGlobals) .cloudFunction(optsWithGlobals.uuid) .get(); console.log(data.jobs.map((job) => job.serialize())); }); } exports.listCloudFunctionJobs = listCloudFunctionJobs; async function deleteCloudFunctionJob(optsWithGlobals) { await (0, utils_1.withErrorHandler)(async () => { await new sdk_1.CloudFunctions(optsWithGlobals) .cloudFunctionJob(optsWithGlobals.jobUuid) .delete(); console.log('Cloud function job deleted successfully!'); }); } exports.deleteCloudFunctionJob = deleteCloudFunctionJob; //# sourceMappingURL=cloud-functions.service.js.map