All files / src/utils fsUtil.ts

45.83% Statements 11/24
0% Branches 0/7
71.42% Functions 5/7
40.9% Lines 9/22

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                21x         238x       238x   10x         259x     24x         34x         21x 13x                                                  
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);
    let curDest = path.join(dest, file);
    Iif (file === 'gitignore') {
      curDest = path.join(dest, '.gitignore');
    }
 
    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);
    }
  });
}