All files / sdk/utils fileUtils.ts

45.45% Statements 10/22
0% Branches 0/11
0% Functions 0/3
36.84% Lines 7/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 596x 6x   6x 6x             6x                         6x                                         6x                          
import * as path from "path";
import * as os from "os";
 
import * as fs from "fs";
import { COMPOSIO_DIR, TEMP_FILES_DIRECTORY_NAME } from "./constants";
 
/**
 * Gets the Composio directory.
 * @param createDirIfNotExists - Whether to create the directory if it doesn't exist.
 * @returns The path to the Composio directory.
 */
export const getComposioDir = (createDirIfNotExists: boolean = false) => {
  const composioDir = path.join(os.homedir(), COMPOSIO_DIR);
  Iif (createDirIfNotExists && !fs.existsSync(composioDir)) {
    fs.mkdirSync(composioDir, { recursive: true });
  }
  return composioDir;
};
 
/**
 * Gets the Composio temporary files directory.
 * @param createDirIfNotExists - Whether to create the directory if it doesn't exist.
 * @returns The path to the Composio temporary files directory.
 */
export const getComposioTempFilesDir = (
  createDirIfNotExists: boolean = false
) => {
  const composioFilesDir = path.join(
    os.homedir(),
    COMPOSIO_DIR,
    TEMP_FILES_DIRECTORY_NAME
  );
  Iif (createDirIfNotExists && !fs.existsSync(composioFilesDir)) {
    fs.mkdirSync(composioFilesDir, { recursive: true });
  }
  return composioFilesDir;
};
 
/**
 * Saves a file to the Composio directory.
 * @param file - The name of the file to save.
 * @param content - The content of the file to save. Should be a string.
 * @param isTempFile - Whether the file is a temporary file.
 * @returns The path to the saved file.
 */
export const saveFile = (
  file: string,
  content: string,
  isTempFile: boolean = false
) => {
  const composioFilesDir = isTempFile
    ? getComposioTempFilesDir(true)
    : getComposioDir(true);
  const filePath = path.join(composioFilesDir, path.basename(file));
  fs.writeFileSync(filePath, content);
 
  return filePath;
};