createRoarrInititialGlobalState.js.flow
914 Bytes
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
// @flow
import environmentIsNode from 'detect-node';
import cmp from 'semver-compare';
import {
version,
} from '../../package.json';
import type {
RoarrGlobalStateType,
} from '../types';
import createNodeWriter from './createNodeWriter';
// eslint-disable-next-line flowtype/no-weak-types
export default (currentState: Object): RoarrGlobalStateType => {
const versions = (currentState.versions || []).concat();
versions.sort(cmp);
const currentIsLatestVersion = !versions.length || cmp(version, versions[versions.length - 1]) === 1;
if (!versions.includes(version)) {
versions.push(version);
}
versions.sort(cmp);
let newState = {
sequence: 0,
...currentState,
versions,
};
if (environmentIsNode) {
if (currentIsLatestVersion || !newState.write) {
newState = {
...newState,
...createNodeWriter(),
};
}
}
return newState;
};