All files index.js

80.41% Statements 78/97
66.07% Branches 37/56
94.12% Functions 16/17
80.41% Lines 78/97
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233      1x 1x 1x 1x           1x               1x 1x       5x       5x 5x             3x 2x         1x 1x 1x 1x   1x 1x 1x   1x       7x           7x           5x       5x       2x       2x       3x 1x     2x 2x 1x         1x     1x 1x 1x             4x 4x 1x 1x     3x 1x   2x                     1x   1x   1x 2x       1x       1x       1x       6x 1x     5x 5x       12x 2x     10x 1x     9x                                                   2x 2x 2x 2x 2x 2x   2x 2x     2x   2x 2x 2x 2x     2x           2x         2x 2x     2x   2x       1x  
'use strict';
 
const
    FS = require('fs'),
    Path = require('path'),
    isOSX = (process.platform === 'darwin'),
    isWindows = (process.platform === 'win32');
 
let unset;
 
class PackDir {
    constructor() {
        this.params = {
            dmg: /darwin/,
            dmgFormat: 'UDZO',
            isSilent: false,
            isSync: true,
            skipDirName: true
        };
 
        this.DMG = '.dmg';
        this.ZIP = '.zip';
    }
 
    asDMG(path) {
        Iif (!isOSX || !this.params.dmg) {
            // Impossible to pack as DMG, or disabled
            return false;
        }
        Eif (this.params.dmg instanceof RegExp) {
            return this.params.dmg.test(path);
        }
 
        return !!this.params.dmg;
    }
 
    cleanFile(fileName) {
        if (fileName && FS.existsSync(fileName)) {
            FS.unlinkSync(fileName);
        }
    }
 
    dmg(path, callback) {
        let fileName = path + this.DMG,
            fileNameArg = this.escapeArg(fileName),
            pathArg = this.escapeArg(path),
            cmd = `hdiutil create -format ${this.params.dmgFormat} -srcfolder ${pathArg} ${fileNameArg}`;
 
        this.cleanFile(fileName);
        this.exec(cmd, unset, callback || unset);
        this.log(`DMG file created: "${fileName}"`);
 
        return fileName;
    }
 
    escapeArg(arg) {
        Iif (isWindows) {
            return arg
                    .trim()
                    .replace(' ' , '\ ');
        }
 
        return arg
            .trim()
            .replace(/(["\s'$`\\])/g, '\\$1');
    }
 
    exec(cmd, params, callback) {
        let execute = this.params.isSync
            ? require('child_process').execSync
            : require('child_process').exec;
 
        return execute(cmd, params, callback);
    }
 
    execFile(file, args, params, callback) {
        let execute = this.params.isSync
            ? require('child_process').execFileSync
            : require('child_process').execFile;
 
        return execute(file, args, params, callback);
    }
 
    extract(path, destination) {
        if (!path) {
            return -1;
        }
 
        try {
            let stats = FS.statSync(path);
            Iif (!stats.isFile()) {
                this.log(`Not a file: "${path}".`);
                return -2;
            }
        } catch (e) {
            return -2;
        }
 
        Eif (!path.endsWith(this.ZIP)) {
            this.log(`Only ZIP files can be extracted. Provided path: "${path}".`);
            return -3;
        }
 
        return this.unzip(path, destination);
    }
 
    path(path, callback) {
        try {
            if (!FS.existsSync(path)) {
                console.error(`Specified path does not exist: "${path}".`);
                return false;
            }
 
            if (this.asDMG(path)) {
                return this.dmg(path, callback);
            } else {
                return this.zip(path, callback);
            }
        }
        catch (e) {
            console.error(`Error while packaging "${path}":\n${e.message.trim()}`);
        }
 
        return false;
    }
 
    paths(paths, callback) {
        let packs = false;
 
        Eif (Array.isArray(paths)) {
            // Recursive packing for Array of paths
            packs = paths.map(path => {
                return this.path(path, callback);
            });
        }
 
        return packs;
    }
 
    getZipPath() {
        return Path.normalize(__dirname + '/zip/zip.exe');
    }
 
    getUnZipPath() {
        return Path.normalize(__dirname + '/zip/unzip.exe');
    }
 
    log(message) {
        if (this.params.isSilent) {
            return false;
        }
 
        console.log(message);
        return true;
    }
 
    param(name, value) {
        if (!this.params.hasOwnProperty(name)) {
            return null;
        }
 
        if (typeof value === 'undefined') {
            return this.params[name];
        }
 
        return this.params[name] = value;
    }
 
    unzip(path, destination, callback) {
        let pathInfo = Path.parse(path),
            extractWhat = this.escapeArg(path),
            extractTo = this.escapeArg(destination || pathInfo.dir),
            args = [
                '-o',
                extractWhat,
                '-d',
                extractTo
            ];
 
        if (isWindows) {
            // Within Electron + ASAR, we can only use `execFile()` for bundled zip.exe
            this.execFile(this.getUnZipPath(), args, unset, callback || unset);
        } else {
            let cmd = 'unzip ' + args.join(' ');
            this.exec(cmd, unset, callback || unset);
        }
 
        return extractTo;
    }
 
    zip(path, callback) {
        let fileName = path + this.ZIP,
            pathInfo = Path.parse(path),
            pathStat = FS.statSync(path),
            pathWithMask = pathInfo.base,
            pathToZipFile = pathInfo.base + '.zip',
            params = {};
            
        Eif (pathInfo.dir) {
            params.cwd = pathInfo.dir;
        }
 
        this.cleanFile(fileName);
 
        Eif (this.params.skipDirName && pathStat.isDirectory()) {
            params.cwd = path;
            pathWithMask = '*';
            pathToZipFile = Path.join('..', pathToZipFile);
        }
 
        let args = [
            '-r',
            this.escapeArg(pathToZipFile),
            this.escapeArg(pathWithMask)
        ];
 
        Iif (isWindows) {
            args.unshift('-S');
            // Within Electron + ASAR, we can only use `execFile()` for bundled zip.exe
            this.execFile(this.getZipPath(), args, params, callback || unset);
        } else {
            let cmd = 'zip ' + args.join(' ');
            this.exec(cmd, params, callback || unset);
        }
 
        this.log(`ZIP archive created: "${fileName}"`);
 
        return fileName;
    }
}
 
module.exports = new PackDir();