You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.1 KiB
41 lines
1.1 KiB
3 months ago
|
import semver from 'semver';
|
||
|
import { createConfigDir, getLastUpdate, saveLastUpdate } from './cache';
|
||
|
import getDistVersion from './getDistVersion';
|
||
|
import { IUpdate } from './types';
|
||
|
|
||
|
const hasNewVersion = async ({
|
||
|
pkg,
|
||
|
updateCheckInterval = 1000 * 60 * 60 * 24,
|
||
|
distTag = 'latest',
|
||
|
alwaysRun,
|
||
|
debug,
|
||
|
}: IUpdate) => {
|
||
|
createConfigDir();
|
||
|
const lastUpdateCheck = getLastUpdate(pkg.name);
|
||
|
if (
|
||
|
alwaysRun ||
|
||
|
!lastUpdateCheck ||
|
||
|
lastUpdateCheck < new Date().getTime() - updateCheckInterval
|
||
|
) {
|
||
|
const latestVersion = await getDistVersion(pkg.name, distTag);
|
||
|
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;
|
||
|
};
|
||
|
|
||
|
export default hasNewVersion;
|