Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: better fallback for Windows #3

Merged
merged 2 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 6 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
var plugin = require('shelljs/plugin');
var shell = require('shelljs');
var child = require('child_process');
var path = require('path');

var pathToSleepHelper = path.join(__dirname, 'sleepHelper.js');
var nodeSleep = shell.config.execPath + ' ' + pathToSleepHelper + ' ';
function execSleep(time) {
if (shell.which('sleep')) {
var sleepCmd = 'sleep'; // actual unix sleep command
if (child.execFileSync) {
child.execFileSync(sleepCmd, [time]);
} else {
shell.exec(sleepCmd + ' ' + time, { silent: true });
}
child.execSync('sleep ' + time);
} else {
throw new Error('Unable to find `sleep` command');
}
}

function busyWait(time) {
var start = new Date().getTime();
var end = start + (time * 1000);
while (new Date().getTime() <= end) {
// loop continuously, because Node can't sleep
child.execSync(nodeSleep + time);
}
}

Expand All @@ -31,11 +21,7 @@ try {
sleepFunc = sleep.sleep.bind(sleep);
exports.nativeExt = sleep.sleep.bind(sleep);
} catch (e) {
if (shell.which('sleep')) {
sleepFunc = execSleep;
} else {
sleepFunc = busyWait;
}
sleepFunc = execSleep;
}

function sleepImpl(options, waitTime) {
Expand All @@ -57,4 +43,3 @@ plugin.register('sleep', sleepImpl, {

exports.sleep = sleepImpl;
exports.execSleep = execSleep;
exports.busyWait = busyWait;
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
},
"license": "MIT",
"files": [
"index.js"
"index.js",
"sleepHelper.js"
],
"devDependencies": {
"eslint": "^3.1.1",
Expand All @@ -41,5 +42,8 @@
},
"optionalDependencies": {
"sleep": "^3.0.1"
},
"engines": {
"node": ">=4.0.0"
}
}
2 changes: 2 additions & 0 deletions sleepHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var timeToSleep = parseInt(process.argv[2], 10) * 1000;
setTimeout(process.exit, timeToSleep);
9 changes: 1 addition & 8 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,11 @@ describe('plugin-sleep', function () {
var start = new Date();
pluginSleep.execSleep(1);
var end = new Date();
assertApproxEqual(end - start, 1000);
assertApproxEqual(end - start, 1000, { epsilon: 300 });
} else {
// Cannot verify on Windows
console.warn('Unable to verify child_process.execSync');
}
});

it('can rely on a busy wait', function () {
var start = new Date();
pluginSleep.busyWait(1);
var end = new Date();
assertApproxEqual(end - start, 1000);
});
});
});