UNPKG

@acurast/cli

Version:

A cli to interact with the Acurast Cloud.

78 lines 3.71 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 { existsSync, mkdirSync, readdir, readFileSync, writeFileSync } from 'fs'; import { jobToNumber } from '../util/jobToNumber.js'; import { ACURAST_DEPLOYMENTS_PATH } from '../constants.js'; import { getDirectoryFromFilePath } from '../util/getDirectoryFromFilePath.js'; export const ensureDirectoryExistence = (filePath) => { const dir = getDirectoryFromFilePath(filePath); if (!existsSync(dir)) { mkdirSync(dir, { recursive: true }); } }; const getFileByDeploymentTime = (deploymentTime) => __awaiter(void 0, void 0, void 0, function* () { return new Promise((resolve, reject) => { readdir(ACURAST_DEPLOYMENTS_PATH, (err, files) => { if (err) { console.error('Error reading directory:', err); return; } const matchingFile = files.find((file) => file.includes(deploymentTime)); if (matchingFile) { // console.log('Files containing "deploymentTime":', matchingFile) const fileContents = readFileSync(`${ACURAST_DEPLOYMENTS_PATH}/${matchingFile}`, 'utf8'); try { const json = JSON.parse(fileContents); resolve({ filename: matchingFile, contentJson: json }); } catch (e) { reject(e); } } else { // console.log(`No files contain deploymentTime "${deploymentTime}".`) resolve(undefined); } }); }); }); export const storeDeployment = (deploymentTime, config, job, jobId) => __awaiter(void 0, void 0, void 0, function* () { const existingFile = yield getFileByDeploymentTime(deploymentTime.getTime().toString()); if (!existingFile) { const deployment = { // transactionId: "", // deploymentId?: "", deployedAt: deploymentTime.toISOString(), assignments: [ // { // processorId: string, // status: "matched" | "acknowledged" | "failed", // } ], status: 'init', config: config, registration: job, deploymentId: jobId, }; const fileName = `${ACURAST_DEPLOYMENTS_PATH}/${config.projectName}-${deploymentTime.getTime().toString()}.json`; ensureDirectoryExistence(fileName); writeFileSync(fileName, JSON.stringify(deployment, null, 2)); } else { if (jobId) { const newFilename = existingFile.filename.substring(0, existingFile.filename.length - 5) + `-${jobToNumber(jobId)}.json`; const newContent = Object.assign(Object.assign({}, existingFile.contentJson), { deploymentId: jobId }); // console.log('NEW CONTENT', newFilename, newContent) writeFileSync(`${ACURAST_DEPLOYMENTS_PATH}/${newFilename}`, JSON.stringify(newContent, null, 2)); } } }); //# sourceMappingURL=storeDeployment.js.map