UNPKG

1.27 kBPlain TextView Raw
1import { ROARR_VERSION } from '../config';
2import { type RoarrGlobalState } from '../types';
3import { createNodeWriter } from './createNodeWriter';
4import compareSemver from 'semver-compare';
5
6export const createRoarrInitialGlobalState = (
7 currentState: any,
8): RoarrGlobalState => {
9 const versions = (currentState.versions || []).concat();
10
11 if (versions.length > 1) {
12 versions.sort(compareSemver);
13 }
14
15 const currentIsLatestVersion =
16 !versions.length ||
17 compareSemver(ROARR_VERSION, versions[versions.length - 1]) === 1;
18
19 if (!versions.includes(ROARR_VERSION)) {
20 versions.push(ROARR_VERSION);
21 }
22
23 versions.sort(compareSemver);
24
25 let newState = {
26 onceLog: new Set<string>(),
27 sequence: 0,
28 ...currentState,
29 versions,
30 };
31
32 if (currentIsLatestVersion || !newState.write) {
33 try {
34 // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
35 const AsyncLocalStorage = require('node:async_hooks').AsyncLocalStorage;
36
37 const asyncLocalStorage = new AsyncLocalStorage();
38
39 newState = {
40 ...newState,
41
42 asyncLocalStorage,
43 write: createNodeWriter(),
44 };
45 // eslint-disable-next-line no-empty
46 } catch {}
47 }
48
49 return newState;
50};