# All files / src/utils fsUtil.ts

65.62% Statements 21/32
0% Branches 0/6
71.42% Functions 5/7
59.25% Lines 16/27

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
    6120x
    20x
    20x

    20x

    20x

    20x
    254x

    254x

    14x

    20x
    262x

    20x

    26x

    20x
    6x

    20x

     import path from 'path';
    import fs from 'fs-extra';
    import ensurePosix from 'ensure-posix-path';

    export interface CopyOptions {
      overwrite: boolean
    }

    const markdownFileExts = '.md';

    export { ensurePosix };

    export function fileExists(filePath: string) {
      try {
        // use decodeURIComponent to deal with space (%20) in file path, e.g
        // from docs\images\dev%20diagrams\architecture.png
        // to docs\images\dev diagrams\architecture.png
        return fs.statSync(decodeURIComponent(filePath)).isFile();
      } catch (err) {
        return false;
      }
    }

    export function isMarkdownFileExt(ext: string) {
      return markdownFileExts === ext;
    }

    export const removeExtension = (filePathWithExt: string) => path.join(
      path.dirname(filePathWithExt),
      path.basename(filePathWithExt, path.extname(filePathWithExt)),
    );

    export const removeExtensionPosix = (filePathWithExt: string) => ensurePosix(path.join(
      path.dirname(filePathWithExt),
      path.basename(filePathWithExt, path.extname(filePathWithExt)),
    ));

    export const setExtension = (normalizedFilename: string, ext: string) => (
      removeExtension(normalizedFilename) + ext
    );

    export function copySyncWithOptions(src: string, dest: string, options: CopyOptions) {
      const files = fs.readdirSync(src);
      files.forEach((file) => {
        const curSource = path.join(src, file);
        const curDest = path.join(dest, file);

        if (fs.lstatSync(curSource).isDirectory()) {
          Iif (!fs.existsSync(curDest)) {
            fs.mkdirSync(curDest);
          }
          copySyncWithOptions(curSource, curDest, options);
        } else {
          Iif (options.overwrite === false && fs.existsSync(curDest)) {
            return;
          }
          fs.copySync(curSource, curDest);
        }
      });
    }