All files / src/sdk/utils fileUtils.ts

20% Statements 7/35
0% Branches 0/13
0% Functions 0/3
12.5% Lines 4/32

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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 876x             6x                                             6x                                                             6x                                                  
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) => {
  try {
    // eslint-disable-next-line @typescript-eslint/no-require-imports
    const os = require("os");
    // eslint-disable-next-line @typescript-eslint/no-require-imports
    const path = require("path");
    // eslint-disable-next-line @typescript-eslint/no-require-imports
    const fs = require("fs");
    const composioDir = path.join(os.homedir(), COMPOSIO_DIR);
    Iif (createDirIfNotExists && !fs.existsSync(composioDir)) {
      fs.mkdirSync(composioDir, { recursive: true });
    }
    return composioDir;
  } catch (_error) {
    return null;
  }
};
 
/**
 * 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
) => {
  try {
    // eslint-disable-next-line @typescript-eslint/no-require-imports
    const os = require("os");
    // eslint-disable-next-line @typescript-eslint/no-require-imports
    const path = require("path");
    // eslint-disable-next-line @typescript-eslint/no-require-imports
    const fs = require("fs");
    const composioFilesDir = path.join(
      os.homedir(),
      COMPOSIO_DIR,
      TEMP_FILES_DIRECTORY_NAME
    );
    Iif (createDirIfNotExists && !fs.existsSync(composioFilesDir)) {
      fs.mkdirSync(composioFilesDir, { recursive: true });
    }
    return composioFilesDir;
  } catch (_error) {
    return null;
  }
};
 
/**
 * 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
) => {
  try {
    const path = require("path");
    // eslint-disable-next-line @typescript-eslint/no-require-imports
    const fs = require("fs");
    const composioFilesDir = isTempFile
      ? getComposioTempFilesDir(true)
      : getComposioDir(true);
    const filePath = path.join(composioFilesDir, path.basename(file));
 
    if (Buffer.isBuffer(content)) {
      fs.writeFileSync(filePath, content);
    } else {
      fs.writeFileSync(filePath, content, "utf8");
    }
 
    return filePath;
  } catch (_error) {
    return null;
  }
};