-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhasNewVersion.ts
More file actions
40 lines (37 loc) · 1.12 KB
/
hasNewVersion.ts
File metadata and controls
40 lines (37 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;