Skip to content

Commit

Permalink
Promisify askPermission (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghmeier authored Jul 29, 2021
1 parent 41d6b38 commit 8525aa1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
35 changes: 19 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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]);
}
}

Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/sub-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ if (process.env.permissionTimeout) {
insight._permissionTimeout = process.env.permissionTimeout;
}

insight.askPermission('', () => {
insight.askPermission('').then(() => {
process.exit(145);
});

0 comments on commit 8525aa1

Please sign in to comment.