Skip to content

Commit

Permalink
Merge pull request #3 from annexare/async-callback
Browse files Browse the repository at this point in the history
Async calls callback
  • Loading branch information
dmythro committed May 10, 2016
2 parents d660d63 + 433b524 commit 290e104
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
33 changes: 19 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const
isOSX = (process.platform === 'darwin'),
isWindows = (process.platform === 'win32');

let unset;

class PackDir {
constructor() {
this.params = {
Expand Down Expand Up @@ -37,20 +39,23 @@ class PackDir {
}
}

dmg(path) {
let fileName = path + this.DMG;
dmg(path, callback) {
let fileName = path + this.DMG,
cmd = `hdiutil create -format ${this.params.dmgFormat} -srcfolder "${path}" "${fileName}"`;

this.cleanFile(fileName);
this.exec()(`hdiutil create -format ${this.params.dmgFormat} -srcfolder "${path}" "${fileName}"`);
this.exec(cmd, unset, callback || unset);
this.log(`DMG file created: "${fileName}"`);

return fileName;
}

exec() {
return this.params.isSync
exec(cmd, params, callback) {
let execute = this.params.isSync
? require('child_process').execSync
: require('child_process').exec;

return execute(cmd, params, callback);
}

extract(path, destination) {
Expand All @@ -76,17 +81,17 @@ class PackDir {
return this.unzip(path, destination);
}

path(path) {
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);
return this.dmg(path, callback);
} else {
return this.zip(path);
return this.zip(path, callback);
}
}
catch (e) {
Expand All @@ -96,13 +101,13 @@ class PackDir {
return false;
}

paths(paths) {
paths(paths, callback) {
let packs = false;

if (Array.isArray(paths)) {
// Recursive packing for Array of paths
packs = paths.map(path => {
return this.path(path);
return this.path(path, callback);
});
}

Expand Down Expand Up @@ -138,20 +143,20 @@ class PackDir {
return this.params[name] = value;
}

unzip(path, destination) {
unzip(path, destination, callback) {
let pathInfo = Path.parse(path),
pathToUnZip = isWindows
? this.getUnZipPath()
: 'unzip',
extractTo = destination || pathInfo.dir,
cmd = `${pathToUnZip} -o "${path}" -d "${extractTo}"`;

this.exec()(cmd);
this.exec(cmd, unset, callback || unset);

return extractTo;
}

zip(path) {
zip(path, callback) {
let fileName = path + this.ZIP,
pathInfo = Path.parse(path),
pathStat = FS.statSync(path),
Expand All @@ -169,7 +174,7 @@ class PackDir {
}

this.cleanFile(fileName);
this.exec()(cmd, params);
this.exec(cmd, params, callback || unset);
this.log(`ZIP archive created: "${fileName}"`);

return fileName;
Expand Down
6 changes: 3 additions & 3 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ describe('Pack Dir', () => {
});

it('executed sync/async', () => {
let process = require('child_process');
let cmd = 'echo test';

Pack.param('isSync', true);
expect(Pack.exec()).toBe(process.execSync);
expect(Pack.exec(cmd) instanceof Buffer).toBe(true);

Pack.param('isSync', false);
expect(Pack.exec()).toBe(process.exec);
expect(Pack.exec(cmd) instanceof require('events')).toBe(true);
});

it('logs are silent when off', () => {
Expand Down

0 comments on commit 290e104

Please sign in to comment.