Skip to content

Commit

Permalink
Merge pull request #5 from annexare/execFile_for_Win
Browse files Browse the repository at this point in the history
Fix calls when package is built into ASAR archive
  • Loading branch information
dmythro committed May 11, 2016
2 parents 4d72a36 + 206eaf7 commit 7e8eda8
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 21 deletions.
65 changes: 49 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class PackDir {
}

escapeArg(arg) {
if (isWindows) {
return arg
.trim()
.replace(' ' , '\ ');
}

return arg
.trim()
.replace(/(["\s'$`\\])/g, '\\$1');
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"dependencies": {},
"devDependencies": {
"jest-cli": "^12.0.2"
"jest-cli": "^12.0.3 || 11.0.2"
},
"author": {
"name": "Dmytro",
Expand Down
24 changes: 20 additions & 4 deletions tests/index.test.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Binary file added zip/bzip2.dll
Binary file not shown.

0 comments on commit 7e8eda8

Please sign in to comment.