all files / tool/ file.js

97.87% Statements 46/47
89.29% Branches 25/28
100% Functions 5/5
97.87% Lines 46/47
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 128 129 130 131 132                                                                                                    11×   11×   11× 25×     19× 18× 18×   14×   12× 12× 16×   16×                   11×                                        
/**
 * @file
 * @desc file handlers
 * @author https://github.com/hoperyy
 * @date  2017/08/11
 */
 
const path = require('path');
const fs = require('fs');
const fse = require('fs-extra');
 
const readdirEnhanced = require('readdir-enhanced').sync;
 
/**
 * @func
 * @desc read dir files
 * @param {String} dir: dir path
 * @param {String/RegExp} filter: situations of filtering files
 */
const readdirSync = (dir, filter) => {
    return readdirEnhanced(dir, {
        deep: filter || true,
        basePath: dir,
    });
};
 
module.exports = {
 
    readdirSync,
 
    /**
     * @func
     * @desc utime file
     * @param {String} filePath
     */
    utime(filePath) {
        const newTime = ((Date.now() - (10 * 1000))) / 1000;
        fs.utimesSync(filePath, newTime, newTime);
    },
 
    /**
     * @func
     * @desc copy file or dir and utime them
     * @param {String} from: src dir/file path
     * @param {String} to: target dir/file path
     */
    copySync(from, to) {
        fse.copySync(from, to);
        this.utime(to);
    },
 
    /**
     * @func
     * @desc write file
     * @param {String} filePath: file path
     * @param {String} content: file content for writing
     */
    writeFileSync(filePath, content) {
        const fd = fs.openSync(filePath, 'w+');
        fs.writeFileSync(filePath, content);
        fs.close(fd);
    },
 
    isEmptyDir({ dir, ignored } = {}) {
        const defaultIgnored = /(\.git)|(\.idea)|(\.ds_store)|(readme\.md)|(\.npm)/i;
 
        let isEmpty = true;
 
        fs.readdirSync(dir).forEach((filename) => {
            if (defaultIgnored.test(filename)) {
                return;
            }
 
            if (ignored) {
                const type = Object.prototype.toString.call(ignored);
                if (type === '[object RegExp]') {
                    if (ignored.test(filename)) {
                        return;
                    }
                } else if (type === '[object String]') {
                    if (ignored === filename) {
                        return;
                    }
                } else Eif (type === '[object Array]') {
                    for (let i = 0; i < ignored.length; i += 1) {
                        const itemType = Object.prototype.toString.call(ignored[i]);
 
                        if (itemType === '[object RegExp]') {
                            if (ignored[i].test(filename)) {
                                return;
                            }
                        } else Eif (itemType === '[object String]') {
                            if (ignored[i] === filename) {
                                return;
                            }
                        }
                    }
                }
            }
 
            isEmpty = false;
        });
 
        return isEmpty;
    },
 
    /**
     * @func
     * @desc rename some files
     * @param {String} dir: current project dir path
     */
    renameInvisableFiles(dir) {
        // rename files like gitignore/npmrc/npmignor to .gitignore/.npmrc/.npmignor
        const arr = fs.readdirSync(dir);
        arr.forEach((filename) => {
            if (/^((gitignore)|(npmrc)|(npmignore))$/.test(filename)) {
                const src = path.join(dir, filename);
                const target = path.join(dir, `.${filename}`);
 
                Eif (!fs.existsSync(target)) {
                    try {
                        fse.moveSync(src, target);
                    } catch (err) {
                        console.log(err);
                    }
                }
            }
        });
    },
 
};