All files / lib/utils directory-file-ops.js

86.44% Statements 51/59
44.12% Branches 15/34
100% Functions 10/10
86.44% Lines 51/59

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127                          56x 56x   56x 56x         3x 3x 3x 3x   3x       3x 3x 3x 5x 5x         5x 5x 5x     3x       1x 1x 1x 1x 1x   1x         1x 1x 1x 1x     1x       1x 1x 1x 3x     3x 1x     1x       1x 1x 1x 3x         3x 3x     1x         5x 5x       5x       4x     4x 4x 4x     56x                
/* 
 * Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
 * the License. A copy of the License is located at
 *
 *     http://aws.amazon.com/apache2.0/
 *
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
*/
"use strict";
const fs = require('fs-extra')
const path = require('path')
 
let _ignoredDirs = []
let _ignoredFiles = []
 
//get the last modification time of the directory including its files and subdirectories
function getDirContentMTime(dirPath, ignoredDirs, ignoredFiles){
    let mtime
    Eif(fs.existsSync(dirPath)){
        _ignoredDirs.push.apply(_ignoredDirs, ignoredDirs)
        _ignoredFiles.push.apply(_ignoredFiles, ignoredFiles)
        mtime = recursiveGetDirContentMTime(dirPath)
    }
    return mtime
}
 
function recursiveGetDirContentMTime(dirPath){
    let mtime = fs.lstatSync(dirPath).mtime
    let files = fs.readdirSync(dirPath)
	for(let i = 0; i < files.length; i++) {
		let stat = fs.lstatSync(path.join(dirPath, files[i]))
		Iif(stat.isDirectory()) {
            if(_ignoredDirs.indexOf(files[i]) < 0){
                let subDirMtime = recursiveGetDirContentMTime(path.join(dirPath, files[i]))
                mtime = (mtime > subDirMtime) ? mtime : subDirMtime
            }
        }else  Eif(_ignoredFiles.indexOf(files[i]) < 0) {
            let fileMtime = stat.mtime
            mtime = (mtime > fileMtime) ? mtime : fileMtime
		}
    }
    return mtime
}
 
function scan(dir, ignoredDirs, ignoredFiles){
    let fileList = []
    Eif(fs.existsSync(dir)){
        _ignoredDirs.push.apply(_ignoredDirs, ignoredDirs)
        _ignoredFiles.push.apply(_ignoredFiles, ignoredFiles)
        fileList = recursiveScan(dir, [])
    }
    return fileList
}
 
function findFile(dir, filename){
    let result
    Eif(fs.existsSync(dir)){
        let filelist = recursiveFindFile(dir, filename,[])
        Eif(filelist.length > 0){
            result = filelist[0]
        }
    }
    return result
}
 
function recursiveFindFile(dir, filename, filelist){
    let files = fs.readdirSync(dir)
    filelist = filelist || []
    files.forEach(function(file) {
        Iif (fs.statSync(path.join(dir, file)).isDirectory()) {
            filelist = recursiveFindFile(path.join(dir, file), filename, filelist)
        }
        else if(file == filename){
            filelist.push(path.join(dir, file))
        }
    })
    return filelist
}
 
function recursiveScan(dir, filelist) {
    let files = fs.readdirSync(dir)
    filelist = filelist || []
    files.forEach(function(file) {
        Iif (fs.statSync(path.join(dir, file)).isDirectory()) {
            if(_ignoredDirs.indexOf(file) < 0){
                filelist = recursiveScan(path.join(dir, file), filelist)
            }
        }
        else Eif (_ignoredFiles.indexOf(file) < 0) {
            filelist.push(path.join(dir, file))
        }
    })
    return filelist
}
 
function readJsonFile(filePath){
    let obj
    try{
        obj = JSON.parse(fs.readFileSync(filePath, 'utf8'))
    }catch(e){
        obj = undefined
    }
    return obj
}
 
function writeJsonFile(filePath, obj){
    Iif(!obj){
        obj = {}
    }
    const jsonString = JSON.stringify(obj, null, 4)
    fs.writeFileSync(filePath, jsonString, 'utf8')
    return obj
}
 
module.exports = {
    getDirContentMTime,
    scan,
    findFile,
    readJsonFile,
    writeJsonFile
}