diff --git a/index.js b/index.js index 90aa17e..07d82a1 100644 --- a/index.js +++ b/index.js @@ -53,6 +53,12 @@ class PackDir { } escapeArg(arg) { + if (isWindows) { + return arg + .trim() + .replace(' ' , '\ '); + } + return arg .trim() .replace(/(["\s'$`\\])/g, '\\$1'); @@ -66,6 +72,14 @@ class PackDir { 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; @@ -103,7 +117,7 @@ class PackDir { } } catch (e) { - console.error(`Error while packaging "${path}": ${e.message}.`); + console.error(`Error while packaging "${path}":\n${e.message.trim()}`); } return false; @@ -153,13 +167,22 @@ class PackDir { unzip(path, destination, callback) { let pathInfo = Path.parse(path), - pathToUnZip = isWindows - ? this.getUnZipPath() - : 'unzip', + extractWhat = this.escapeArg(path), extractTo = this.escapeArg(destination || pathInfo.dir), - cmd = `${pathToUnZip} -o ${this.escapeArg(path)} -d ${extractTo}`; - - this.exec(cmd, unset, callback || unset); + 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; } @@ -168,24 +191,34 @@ class PackDir { let fileName = path + this.ZIP, pathInfo = Path.parse(path), pathStat = FS.statSync(path), - pathWithMask = this.escapeArg( + pathWithMask = ( pathStat.isDirectory() - ? pathInfo.base + Path.sep + '*' + ? (pathInfo.base + Path.sep + '*') : pathInfo.base ), - pathToZip = isWindows - ? this.getZipPath() - : 'zip', - pathToZipFile = this.escapeArg(pathInfo.base + '.zip'), - cmd = `${pathToZip} -r ${pathToZipFile} ${pathWithMask}`, + pathToZipFile = pathInfo.base + '.zip', params = {}; - + if (pathInfo.dir) { params.cwd = pathInfo.dir; } this.cleanFile(fileName); - this.exec(cmd, params, callback || unset); + + let args = [ + '-r', + this.escapeArg(pathToZipFile), + this.escapeArg(pathWithMask) + ]; + + if (isWindows) { + // 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; diff --git a/package.json b/package.json index 2754868..30d1c4b 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ }, "dependencies": {}, "devDependencies": { - "jest-cli": "^12.0.2" + "jest-cli": "^12.0.3 || 11.0.2" }, "author": { "name": "Dmytro", diff --git a/tests/index.test.js b/tests/index.test.js index 5edcb13..2034cad 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -1,15 +1,16 @@ 'use strict'; +jest.autoMockOff(); + const fs = require('fs'); const isOSX = (process.platform === 'darwin'), + isWindows = (process.platform === 'win32'), TEST_PATH = 'tests', TEST_DIR_PATH = TEST_PATH + '/test dir', TEST_EXTRACT_PATH = TEST_PATH + '/test-extract', TEST_OSX_PATH = TEST_PATH + '/test-osx', TEST_OSX_REG = /osx/; -jest.autoMockOff(); - describe('Pack Dir', () => { let Pack; @@ -49,7 +50,7 @@ describe('Pack Dir', () => { }); it('executed sync/async', () => { - let cmd = 'echo test'; + let cmd = 'node -v'; Pack.param('isSync', true); expect(Pack.exec(cmd) instanceof Buffer).toBe(true); @@ -58,6 +59,17 @@ describe('Pack Dir', () => { expect(Pack.exec(cmd) instanceof require('events')).toBe(true); }); + it('executed file sync/async', () => { + let cmd = 'node', + args = ['-v']; + + Pack.param('isSync', true); + expect(Pack.execFile(cmd) instanceof Buffer).toBe(true); + + Pack.param('isSync', false); + expect(Pack.execFile(cmd, args) instanceof require('events')).toBe(true); + }); + it('logs are silent when off', () => { Pack.param('isSilent', true); expect(Pack.log('Something silent.')).toBe(false); @@ -105,7 +117,11 @@ describe('Pack Dir', () => { }); it('escapes args/paths', () => { - expect(Pack.escapeArg(TEST_PATH)).toEqual(TEST_PATH.replace(' ', '\\ ')); + if (isWindows) { + expect(Pack.escapeArg(TEST_PATH)).toEqual(TEST_PATH.replace(' ' , '\ ')); + } else { + expect(Pack.escapeArg(TEST_PATH)).toEqual(TEST_PATH.replace(' ', '\\ ')); + } }); // Cleanup diff --git a/zip/bzip2.dll b/zip/bzip2.dll new file mode 100755 index 0000000..8174912 Binary files /dev/null and b/zip/bzip2.dll differ