From 144cfb17f3ec341e1f37014e30be8274c7dcc19f Mon Sep 17 00:00:00 2001 From: Nate Fischer Date: Tue, 30 May 2017 21:34:46 -0700 Subject: [PATCH 1/2] feat: better fallback for Windows This adds a better fallback approach for Windows and systems lacking the `sleep` CLI utility. This relies on spawning a Node.js process designed to sleep for the desired time. This has a tradeoff that it typically sleeps for longer than the desired time, but it's available on all systems and is more predictable than `timeout` for Windows. This makes busy-waiting obsolete, so that section and test case have been removed. This should still avoid blocking the CPU, which is a more critical concern. Fixes #1, #2 --- index.js | 27 ++++++--------------------- package.json | 6 +++++- sleepHelper.js | 2 ++ test/test.js | 9 +-------- 4 files changed, 14 insertions(+), 30 deletions(-) create mode 100644 sleepHelper.js diff --git a/index.js b/index.js index bce8c43..9f26d2a 100644 --- a/index.js +++ b/index.js @@ -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); } } @@ -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) { @@ -57,4 +43,3 @@ plugin.register('sleep', sleepImpl, { exports.sleep = sleepImpl; exports.execSleep = execSleep; -exports.busyWait = busyWait; diff --git a/package.json b/package.json index 6994afc..9a5b91e 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ }, "license": "MIT", "files": [ - "index.js" + "index.js", + "sleepHelper.js" ], "devDependencies": { "eslint": "^3.1.1", @@ -41,5 +42,8 @@ }, "optionalDependencies": { "sleep": "^3.0.1" + }, + "engines": { + "node": ">=0.11.0" } } diff --git a/sleepHelper.js b/sleepHelper.js new file mode 100644 index 0000000..900ac79 --- /dev/null +++ b/sleepHelper.js @@ -0,0 +1,2 @@ +var timeToSleep = parseInt(process.argv[2], 10) * 1000; +setTimeout(process.exit, timeToSleep); diff --git a/test/test.js b/test/test.js index 007bcb1..bf93e46 100644 --- a/test/test.js +++ b/test/test.js @@ -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); - }); }); }); From 5ad861311679715f1cf3c0864b0ac8dbccfa04c1 Mon Sep 17 00:00:00 2001 From: Nate Fischer Date: Tue, 30 May 2017 21:40:53 -0700 Subject: [PATCH 2/2] Bump engines value, since we didn't support v0.11 anyway --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9a5b91e..7e2e0da 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,6 @@ "sleep": "^3.0.1" }, "engines": { - "node": ">=0.11.0" + "node": ">=4.0.0" } }