source: node_modules/simple-update-notifier/src/cache.ts@ 2d1ec46

main
Last change on this file since 2d1ec46 was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago

Initial commit

  • Property mode set to 100644
File size: 1.0 KB
Line 
1import os from 'os';
2import path from 'path';
3import fs from 'fs';
4
5const homeDirectory = os.homedir();
6const configDir =
7 process.env.XDG_CONFIG_HOME ||
8 path.join(homeDirectory, '.config', 'simple-update-notifier');
9
10const getConfigFile = (packageName: string) => {
11 return path.join(
12 configDir,
13 `${packageName.replace('@', '').replace('/', '__')}.json`
14 );
15};
16
17export const createConfigDir = () => {
18 if (!fs.existsSync(configDir)) {
19 fs.mkdirSync(configDir, { recursive: true });
20 }
21};
22
23export const getLastUpdate = (packageName: string) => {
24 const configFile = getConfigFile(packageName);
25
26 try {
27 if (!fs.existsSync(configFile)) {
28 return undefined;
29 }
30 const file = JSON.parse(fs.readFileSync(configFile, 'utf8'));
31 return file.lastUpdateCheck as number;
32 } catch {
33 return undefined;
34 }
35};
36
37export const saveLastUpdate = (packageName: string) => {
38 const configFile = getConfigFile(packageName);
39
40 fs.writeFileSync(
41 configFile,
42 JSON.stringify({ lastUpdateCheck: new Date().getTime() })
43 );
44};
Note: See TracBrowser for help on using the repository browser.