Code coverage report for utils/file.js

Statements: 6.25% (3 / 48)      Branches: 0% (0 / 8)      Functions: 0% (0 / 14)      Lines: 6.25% (3 / 48)      Ignored: none     

All files » utils/ » file.js
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 1001 1     1                                                                                                                                                                                              
var log = require('./log');
var nodePath = require('path');
//todo: update to use more of https://nodejs.org/api/path.html#path_path_delimiter
 
module.exports = function File(fileObj){
    if (!fileObj.path) {
        return log.onError('File: fileObj.path is required');
    }
 
    var path;
    var name;
    var dir;
    var relativeDir;
    var ext;
    var slash = nodePath.sep;
    var cwd = fileObj.cwd;
    var base = fileObj.base;
    var contents = fileObj.contents;
 
    Object.defineProperty(this, 'cwd', {
        get: function () {
            return cwd;
        }
    });
 
    Object.defineProperty(this, 'base', {
        get: function () {
            return base;
        }
    });
 
    Object.defineProperty(this, 'relativeDir', {
        get: function () {
            return relativeDir;
        }
    });
 
    Object.defineProperty(this, 'path', {
        get: function () {
            return path;
        },
        set: function (value) {
            path = nodePath.normalize(value);
            var outDirs = path.split(slash);
            outDirs.pop();
 
            name = path.split(slash).pop();
            dir = outDirs.join(slash);
            relativeDir = (base) ? (dir + slash).replace(base,'') : undefined;
            ext = name.split('.').pop();
        }
    });
 
    Object.defineProperty(this, 'contents', {
        get: function () {
            return contents;
        },
        set: function (value) {
            contents = value;
        }
    });
 
    Object.defineProperty(this, 'name', {
        get: function () {
            return name;
        },
        set: function (value) {
            if (value.indexOf('/') > -1 || value.indexOf('\\') > -1) {
                return log.onError('File name cannot contain slashes');
            }
 
            var outDirs = path.split(slash);
            outDirs.pop();
 
            this.path = outDirs.join(slash) + slash + value;
        }
    });
 
    Object.defineProperty(this, 'dir', {
        get: function () {
            return dir;
        },
        set: function (value) {
            this.path = value + slash + name;
        }
    });
 
    Object.defineProperty(this, 'ext', {
        get: function () {
            return ext;
        },
        set: function (value) {
            var arrPath = path.split('.');
            arrPath.pop();
            this.path = arrPath.join('.') + '.' + value;
        }
    });
 
    this.path = nodePath.normalize(fileObj.path);
};