-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
package.ts
138 lines (129 loc) · 4 KB
/
package.ts
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import is from '@sindresorhus/is';
import equal from 'fast-deep-equal';
import { getPkgReleases } from '../../datasource';
import * as datasourceGithubTags from '../../datasource/github-tags';
import { logger } from '../../logger';
import { resolveFile } from '../../util';
import { isVersion, maxSatisfyingVersion } from '../../versioning/semver';
import { LookupUpdate, PackageUpdateConfig } from '../common';
interface NodeJsPolicies {
all: number[];
lts: number[];
active: number[];
lts_active: number[];
lts_latest: number[];
current: number[];
}
interface NodeJsSchedule {
lts: string;
maintenance: string;
end: string;
start: string;
}
type NodeJsData = Record<string, NodeJsSchedule>;
let policies: NodeJsPolicies;
let refreshDate: Date;
async function generatePolicies(): Promise<NodeJsData> {
const file = await resolveFile('data/node-js-schedule.json');
const nodeJsSchedule = (await import(file)) as NodeJsData;
policies = {
all: [],
lts: [],
active: [],
lts_active: [],
lts_latest: [],
current: [],
};
const now = new Date();
for (const [vRelease, data] of Object.entries(nodeJsSchedule)) {
const isAlive = new Date(data.start) < now && new Date(data.end) > now;
if (isAlive) {
const release = parseInt(vRelease.replace(/^v/, ''), 10);
policies.all.push(release);
const isMaintenance =
data.maintenance && new Date(data.maintenance) < now;
if (!isMaintenance) {
policies.active.push(release);
}
const isLts = data.lts && new Date(data.lts) < now;
if (isLts) {
policies.lts.push(release);
if (!isMaintenance) {
policies.lts_active.push(release);
}
}
}
}
policies.current.push(policies.active[policies.active.length - 1]);
policies.lts_latest.push(policies.lts[policies.lts.length - 1]);
return nodeJsSchedule;
}
async function checkPolicies(): Promise<void> {
if (policies && refreshDate > new Date()) {
return;
}
const nodeJsSchedule = await generatePolicies();
refreshDate = new Date('3000-01-01'); // y3k
const now = new Date();
for (const data of Object.values(nodeJsSchedule)) {
const fields = ['start', 'lts', 'maintenance', 'end'];
for (const field of fields) {
const fieldDate = new Date(data[field]);
if (fieldDate > now && fieldDate < refreshDate) {
refreshDate = fieldDate;
}
}
}
logger.debug('Node.js policies refresh date: ' + refreshDate);
}
export async function getPackageUpdates(
config: PackageUpdateConfig
): Promise<LookupUpdate[]> {
logger.trace('travis.getPackageUpdates()');
const { supportPolicy } = config;
if (!(supportPolicy && supportPolicy.length)) {
return [];
}
await checkPolicies();
for (const policy of supportPolicy) {
if (!Object.keys(policies).includes(policy)) {
logger.warn(`Unknown supportPolicy: ${policy}`);
return [];
}
}
logger.debug({ supportPolicy }, `supportPolicy`);
let newValue: any[] = (supportPolicy as (keyof NodeJsPolicies)[])
.map((policy) => policies[policy])
.reduce((result, policy) => result.concat(policy), [])
.sort((a, b) => a - b);
const newMajor: number = newValue[newValue.length - 1];
if (config.rangeStrategy === 'pin' || isVersion(config.currentValue[0])) {
const versions = (
await getPkgReleases({
...config,
datasource: datasourceGithubTags.id,
depName: 'nodejs/node',
})
).releases.map((release) => release.version);
newValue = newValue.map((value) =>
maxSatisfyingVersion(versions, `${value}`)
);
}
if (is.string(config.currentValue[0])) {
newValue = newValue.map((val) => `${val}`);
}
newValue.sort((a, b) => a - b);
// TODO: `config.currentValue` is a string!
(config.currentValue as any).sort((a, b) => a - b);
if (equal(config.currentValue, newValue)) {
return [];
}
return [
{
newValue: newValue.join(','),
newMajor,
isRange: true,
sourceUrl: 'https://github.com/nodejs/node',
},
];
}