15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
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
41
42
43
44
import os from 'os';
import path from 'path';
import fs from 'fs';
 
const homeDirectory = os.homedir();
const configDir =
  process.env.XDG_CONFIG_HOME ||
  path.join(homeDirectory, '.config', 'simple-update-notifier');
 
const getConfigFile = (packageName: string) => {
  return path.join(
    configDir,
    `${packageName.replace('@', '').replace('/', '__')}.json`
  );
};
 
export const createConfigDir = () => {
  if (!fs.existsSync(configDir)) {
    fs.mkdirSync(configDir, { recursive: true });
  }
};
 
export const getLastUpdate = (packageName: string) => {
  const configFile = getConfigFile(packageName);
 
  try {
    if (!fs.existsSync(configFile)) {
      return undefined;
    }
    const file = JSON.parse(fs.readFileSync(configFile, 'utf8'));
    return file.lastUpdateCheck as number;
  } catch {
    return undefined;
  }
};
 
export const saveLastUpdate = (packageName: string) => {
  const configFile = getConfigFile(packageName);
 
  fs.writeFileSync(
    configFile,
    JSON.stringify({ lastUpdateCheck: new Date().getTime() })
  );
};