diff --git a/lib/index.js b/lib/index.js index 27cc342..d4cee65 100644 --- a/lib/index.js +++ b/lib/index.js @@ -141,14 +141,11 @@ class Insight { this._save(); } - askPermission(msg, cb) { + askPermission(msg) { const defaultMsg = `May ${chalk.cyan(this.packageName)} anonymously report usage statistics to improve the tool over time?`; - cb = cb || (() => {}); - if (!process.stdout.isTTY || process.argv.indexOf('--no-insight') !== -1 || process.env.CI) { - setImmediate(cb, null, false); - return; + return Promise.resolve(); } const prompt = inquirer.prompt({ @@ -158,23 +155,29 @@ class Insight { default: true }); - // Add a 30 sec timeout before giving up on getting an answer - const permissionTimeout = setTimeout(() => { - // Stop listening for stdin - prompt.ui.close(); - - // Automatically opt out - this.optOut = true; - cb(null, false); - }, this._permissionTimeout * 1000); + // Set a 30 sec timeout before giving up on getting an answer + let permissionTimeout; + const timeoutPromise = new Promise(resolve => { + permissionTimeout = setTimeout(() => { + // Stop listening for stdin + prompt.ui.close(); + + // Automatically opt out + this.optOut = true; + resolve(false); + }, this._permissionTimeout * 1000); + }); - prompt.then(result => { + const promise = prompt.then(result => { // Clear the permission timeout upon getting an answer clearTimeout(permissionTimeout); this.optOut = !result.optIn; - cb(null, result.optIn); + return result.optIn; }); + + // Return the result of the prompt if it finishes first otherwise default to the timeout's value. + return Promise.race([promise, timeoutPromise]); } } diff --git a/readme.md b/readme.md index b5ace6c..18a6e05 100644 --- a/readme.md +++ b/readme.md @@ -186,13 +186,13 @@ Type: `integer` Event value: A numeric value associated with the event (e.g. 42). -#### .askPermission([message, callback]) +#### .askPermission([message]) Asks the user permission to opt-in to tracking and sets the `optOut` property in `config`. You can also choose to set `optOut` property in `config` manually. ![askPermission screenshot](screenshot-askpermission.png) -Optionally supply your own `message` and `callback`. If `message` is `null`, default message will be used. The callback will be called with the arguments `error` and `optIn` when the prompt is done and is useful for when you want to continue the execution while the prompt is running. +Optionally supply your own `message`. If `message` is `null`, default message will be used. This also resolves with the new value of `optIn` when the prompt is done and is useful for when you want to continue the execution while the prompt is running. #### .optOut diff --git a/test/fixtures/sub-process.js b/test/fixtures/sub-process.js index 6d80415..d302c00 100644 --- a/test/fixtures/sub-process.js +++ b/test/fixtures/sub-process.js @@ -11,6 +11,6 @@ if (process.env.permissionTimeout) { insight._permissionTimeout = process.env.permissionTimeout; } -insight.askPermission('', () => { +insight.askPermission('').then(() => { process.exit(145); });