Skip to content

Commit

Permalink
Add debug flag to simpleUpdateNotifier() (#15)
Browse files Browse the repository at this point in the history
Fixes #14
  • Loading branch information
voxpelli authored Nov 24, 2022
1 parent cc8d44e commit 2daeb9f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Type: `string`

Type: `number`\
Default: `1000 * 60 * 60 * 24` _(1 day)_

How often to check for updates.

#### shouldNotifyInNpmScript
Expand All @@ -65,3 +66,17 @@ Type: `string`\
Default: `'latest'`

Which [dist-tag](https://docs.npmjs.com/adding-dist-tags-to-packages) to use to find the latest version.

#### alwaysRun

Type: `boolean`\
Default: `false`

When set, `updateCheckInterval` will not be respected and a check for an update will always be performed.

#### debug

Type: `boolean`\
Default: `false`

When set, logs explaining the decision will be output to `stderr` whenever the module opts to not print an update notification
11 changes: 11 additions & 0 deletions src/hasNewVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const hasNewVersion = async ({
updateCheckInterval = 1000 * 60 * 60 * 24,
distTag = 'latest',
alwaysRun,
debug,
}: IUpdate) => {
createConfigDir();
const lastUpdateCheck = getLastUpdate(pkg.name);
Expand All @@ -20,7 +21,17 @@ const hasNewVersion = async ({
saveLastUpdate(pkg.name);
if (semver.gt(latestVersion, pkg.version)) {
return latestVersion;
} else if (debug) {
console.error(
`Latest version (${latestVersion}) not newer than current version (${pkg.version})`
);
}
} else if (debug) {
console.error(
`Too recent to check for a new update. simpleUpdateNotifier() interval set to ${updateCheckInterval}ms but only ${
new Date().getTime() - lastUpdateCheck
}ms since last check.`
);
}

return false;
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const simpleUpdateNotifier = async (args: IUpdate) => {
!args.alwaysRun &&
(!process.stdout.isTTY || (isNpmOrYarn && !args.shouldNotifyInNpmScript))
) {
if (args.debug) {
console.error('Opting out of running simpleUpdateNotifier()');
}
return;
}

Expand All @@ -20,8 +23,11 @@ Current Version: ${args.pkg.version}
Latest Version: ${latestVersion}`)
);
}
} catch {
} catch (err) {
// Catch any network errors or cache writing errors so module doesn't cause a crash
if (args.debug && err instanceof Error) {
console.error('Unexpected error in simpleUpdateNotifier():', err);
}
}
};

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface IUpdate {
shouldNotifyInNpmScript?: boolean;
distTag?: string;
alwaysRun?: boolean;
debug?: boolean;
}

0 comments on commit 2daeb9f

Please sign in to comment.