"Fossies" - the Fresh Open Source Software Archive

Member "cli-1.1280.1/src/lib/iac/file-utils.ts" (20 Feb 2024, 1256 Bytes) of package /linux/misc/snyk-cli-1.1280.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 import * as tar from 'tar';
    2 import { promises as fsPromises, constants as fsConstants } from 'fs';
    3 import { promisify } from 'util';
    4 
    5 export async function isExe(path: string): Promise<boolean> {
    6   try {
    7     await fsPromises.access(path, fsConstants.X_OK);
    8     return true;
    9   } catch (err) {
   10     return false;
   11   }
   12 }
   13 
   14 export async function isExists(path: string): Promise<boolean> {
   15   try {
   16     await fsPromises.stat(path);
   17     return true;
   18   } catch (err) {
   19     return false;
   20   }
   21 }
   22 
   23 export async function createDirIfNotExists(path: string): Promise<void> {
   24   const isDirExists = await isExists(path);
   25   if (!isDirExists) {
   26     fsPromises.mkdir(path, { recursive: true });
   27   }
   28 }
   29 
   30 export async function isFile(path: string): Promise<boolean> {
   31   try {
   32     return (await fsPromises.stat(path)).isFile();
   33   } catch (err) {
   34     return false;
   35   }
   36 }
   37 
   38 export async function isArchive(path: string): Promise<boolean> {
   39   try {
   40     const tarList = promisify(tar.list);
   41     await tarList({ file: path, strict: true });
   42     return true;
   43   } catch (e) {
   44     return false;
   45   }
   46 }
   47 
   48 export async function saveFile(
   49   dataBuffer: Buffer,
   50   savePath: string,
   51 ): Promise<void> {
   52   await fsPromises.writeFile(savePath, dataBuffer);
   53   await fsPromises.chmod(savePath, 0o744);
   54 }