Skip to content

Commit 433b524

Browse files
author
Zork
committed
Async calls callback
1 parent d660d63 commit 433b524

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

index.js

+19-14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const
66
isOSX = (process.platform === 'darwin'),
77
isWindows = (process.platform === 'win32');
88

9+
let unset;
10+
911
class PackDir {
1012
constructor() {
1113
this.params = {
@@ -37,20 +39,23 @@ class PackDir {
3739
}
3840
}
3941

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

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

4750
return fileName;
4851
}
4952

50-
exec() {
51-
return this.params.isSync
53+
exec(cmd, params, callback) {
54+
let execute = this.params.isSync
5255
? require('child_process').execSync
5356
: require('child_process').exec;
57+
58+
return execute(cmd, params, callback);
5459
}
5560

5661
extract(path, destination) {
@@ -76,17 +81,17 @@ class PackDir {
7681
return this.unzip(path, destination);
7782
}
7883

79-
path(path) {
84+
path(path, callback) {
8085
try {
8186
if (!FS.existsSync(path)) {
8287
console.error(`Specified path does not exist: "${path}".`);
8388
return false;
8489
}
8590

8691
if (this.asDMG(path)) {
87-
return this.dmg(path);
92+
return this.dmg(path, callback);
8893
} else {
89-
return this.zip(path);
94+
return this.zip(path, callback);
9095
}
9196
}
9297
catch (e) {
@@ -96,13 +101,13 @@ class PackDir {
96101
return false;
97102
}
98103

99-
paths(paths) {
104+
paths(paths, callback) {
100105
let packs = false;
101106

102107
if (Array.isArray(paths)) {
103108
// Recursive packing for Array of paths
104109
packs = paths.map(path => {
105-
return this.path(path);
110+
return this.path(path, callback);
106111
});
107112
}
108113

@@ -138,20 +143,20 @@ class PackDir {
138143
return this.params[name] = value;
139144
}
140145

141-
unzip(path, destination) {
146+
unzip(path, destination, callback) {
142147
let pathInfo = Path.parse(path),
143148
pathToUnZip = isWindows
144149
? this.getUnZipPath()
145150
: 'unzip',
146151
extractTo = destination || pathInfo.dir,
147152
cmd = `${pathToUnZip} -o "${path}" -d "${extractTo}"`;
148153

149-
this.exec()(cmd);
154+
this.exec(cmd, unset, callback || unset);
150155

151156
return extractTo;
152157
}
153158

154-
zip(path) {
159+
zip(path, callback) {
155160
let fileName = path + this.ZIP,
156161
pathInfo = Path.parse(path),
157162
pathStat = FS.statSync(path),
@@ -169,7 +174,7 @@ class PackDir {
169174
}
170175

171176
this.cleanFile(fileName);
172-
this.exec()(cmd, params);
177+
this.exec(cmd, params, callback || unset);
173178
this.log(`ZIP archive created: "${fileName}"`);
174179

175180
return fileName;

tests/index.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ describe('Pack Dir', () => {
4949
});
5050

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

5454
Pack.param('isSync', true);
55-
expect(Pack.exec()).toBe(process.execSync);
55+
expect(Pack.exec(cmd) instanceof Buffer).toBe(true);
5656

5757
Pack.param('isSync', false);
58-
expect(Pack.exec()).toBe(process.exec);
58+
expect(Pack.exec(cmd) instanceof require('events')).toBe(true);
5959
});
6060

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

0 commit comments

Comments
 (0)