forked from browserslist/browserslist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-db.js
363 lines (336 loc) · 10.1 KB
/
update-db.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
var childProcess = require('child_process')
var escalade = require('escalade/sync')
var pico = require('picocolors')
var path = require('path')
var fs = require('fs')
var BrowserslistError = require('./error')
function detectLockfile() {
var packageDir = escalade('.', function (dir, names) {
return names.indexOf('package.json') !== -1 ? dir : ''
})
if (!packageDir) {
throw new BrowserslistError(
'Cannot find package.json. ' +
'Is this the right directory to run `npx browserslist --update-db` in?'
)
}
var lockfileNpm = path.join(packageDir, 'package-lock.json')
var lockfileShrinkwrap = path.join(packageDir, 'npm-shrinkwrap.json')
var lockfileYarn = path.join(packageDir, 'yarn.lock')
var lockfilePnpm = path.join(packageDir, 'pnpm-lock.yaml')
if (fs.existsSync(lockfilePnpm)) {
return { mode: 'pnpm', file: lockfilePnpm }
} else if (fs.existsSync(lockfileNpm)) {
return { mode: 'npm', file: lockfileNpm }
} else if (fs.existsSync(lockfileYarn)) {
var lock = { mode: 'yarn', file: lockfileYarn }
lock.content = fs.readFileSync(lock.file).toString()
lock.version = /# yarn lockfile v1/.test(lock.content) ? 1 : 2
return lock
} else if (fs.existsSync(lockfileShrinkwrap)) {
return { mode: 'npm', file: lockfileShrinkwrap }
}
throw new BrowserslistError(
'No lockfile found. Run "npm install", "yarn install" or "pnpm install"'
)
}
function getLatestInfo(lock) {
if (lock.mode === 'yarn') {
if (lock.version === 1) {
return JSON.parse(
childProcess.execSync('yarn info caniuse-lite --json').toString()
).data
} else {
return JSON.parse(
childProcess.execSync('yarn npm info caniuse-lite --json').toString()
)
}
}
return JSON.parse(
childProcess.execSync('npm show caniuse-lite --json').toString()
)
}
function getBrowsersList() {
return childProcess
.execSync('npx browserslist')
.toString()
.trim()
.split('\n')
.map(function (line) {
return line.trim().split(' ')
})
.reduce(function (result, entry) {
if (!result[entry[0]]) {
result[entry[0]] = []
}
result[entry[0]].push(entry[1])
return result
}, {})
}
function diffBrowsersLists(old, current) {
var browsers = Object.keys(old).concat(
Object.keys(current).filter(function (browser) {
return old[browser] === undefined
})
)
return browsers
.map(function (browser) {
var oldVersions = old[browser] || []
var currentVersions = current[browser] || []
var intersection = oldVersions.filter(function (version) {
return currentVersions.indexOf(version) !== -1
})
var addedVersions = currentVersions.filter(function (version) {
return intersection.indexOf(version) === -1
})
var removedVersions = oldVersions.filter(function (version) {
return intersection.indexOf(version) === -1
})
return removedVersions
.map(function (version) {
return pico.red('- ' + browser + ' ' + version)
})
.concat(
addedVersions.map(function (version) {
return pico.green('+ ' + browser + ' ' + version)
})
)
})
.reduce(function (result, array) {
return result.concat(array)
}, [])
.join('\n')
}
function updateNpmLockfile(lock, latest) {
var metadata = { latest: latest, versions: [] }
var content = deletePackage(JSON.parse(lock.content), metadata)
metadata.content = JSON.stringify(content, null, ' ')
return metadata
}
function deletePackage(node, metadata) {
if (node.dependencies) {
if (node.dependencies['caniuse-lite']) {
var version = node.dependencies['caniuse-lite'].version
metadata.versions[version] = true
delete node.dependencies['caniuse-lite']
}
for (var i in node.dependencies) {
node.dependencies[i] = deletePackage(node.dependencies[i], metadata)
}
}
return node
}
var yarnVersionRe = /version "(.*?)"/
function updateYarnLockfile(lock, latest) {
var blocks = lock.content.split(/(\n{2,})/).map(function (block) {
return block.split('\n')
})
var versions = {}
blocks.forEach(function (lines) {
if (lines[0].indexOf('caniuse-lite@') !== -1) {
var match = yarnVersionRe.exec(lines[1])
versions[match[1]] = true
if (match[1] !== latest.version) {
lines[1] = lines[1].replace(
/version "[^"]+"/,
'version "' + latest.version + '"'
)
lines[2] = lines[2].replace(
/resolved "[^"]+"/,
'resolved "' + latest.dist.tarball + '"'
)
lines[3] = latest.dist.integrity
? lines[3].replace(
/integrity .+/,
'integrity ' + latest.dist.integrity
)
: ''
}
}
})
var content = blocks
.map(function (lines) {
return lines.join('\n')
})
.join('')
return { content: content, versions: versions }
}
function updatePnpmLockfile(lock, latest) {
var versions = {}
var lines = lock.content.split('\n')
var i
var j
var lineParts
for (i = 0; i < lines.length; i++) {
if (lines[i].indexOf('caniuse-lite:') >= 0) {
lineParts = lines[i].split(/:\s?/, 2)
if (lineParts[1].indexOf('/') >= 0) {
var sublineParts = lineParts[1].split(/([/:])/)
for (j = 0; j < sublineParts.length; j++) {
if (sublineParts[j].indexOf('caniuse-lite') >= 0) {
versions[sublineParts[j + 2]] = true
sublineParts[j + 2] = latest.version
break
}
}
lineParts[1] = sublineParts.join('')
} else {
versions[lineParts[1]] = true
}
lines[i] = lineParts[0] + ': ' + latest.version
} else if (lines[i].indexOf('/caniuse-lite') >= 0) {
lineParts = lines[i].split(/([/:])/)
for (j = 0; j < lineParts.length; j++) {
if (lineParts[j].indexOf('caniuse-lite') >= 0) {
versions[lineParts[j + 2]] = true
lineParts[j + 2] = latest.version
break
}
}
lines[i] = lineParts.join('')
for (i = i + 1; i < lines.length; i++) {
if (lines[i].indexOf('integrity: ') !== -1) {
lines[i] = lines[i].replace(
/integrity: .+/,
'integrity: ' + latest.dist.integrity
)
} else if (lines[i].indexOf(' /') !== -1) {
break
}
}
}
}
return { content: lines.join('\n'), versions: versions }
}
function updateLockfile(lock, latest) {
if (!lock.content) lock.content = fs.readFileSync(lock.file).toString()
if (lock.mode === 'npm') {
return updateNpmLockfile(lock, latest)
} else if (lock.mode === 'yarn') {
return updateYarnLockfile(lock, latest)
}
return updatePnpmLockfile(lock, latest)
}
function updatePackageManually(print, lock, latest) {
var lockfileData = updateLockfile(lock, latest)
var caniuseVersions = Object.keys(lockfileData.versions).sort()
if (caniuseVersions.length === 1 && caniuseVersions[0] === latest.version) {
print(
'Installed version: ' +
pico.bold(pico.green(latest.version)) +
'\n' +
pico.bold(pico.green('caniuse-lite is up to date')) +
'\n'
)
return
}
if (caniuseVersions.length === 0) {
caniuseVersions[0] = 'none'
}
print(
'Installed version' +
(caniuseVersions.length === 1 ? ': ' : 's: ') +
pico.bold(pico.red(caniuseVersions.join(', '))) +
'\n' +
'Removing old caniuse-lite from lock file\n'
)
fs.writeFileSync(lock.file, lockfileData.content)
var install = lock.mode === 'yarn' ? 'yarn add -W' : lock.mode + ' install'
print(
'Installing new caniuse-lite version\n' +
pico.yellow('$ ' + install + ' caniuse-lite') +
'\n'
)
try {
childProcess.execSync(install + ' caniuse-lite')
} catch (e) /* istanbul ignore next */ {
print(
pico.red(
'\n' +
e.stack +
'\n\n' +
'Problem with `' +
install +
' caniuse-lite` call. ' +
'Run it manually.\n'
)
)
process.exit(1)
}
var del = lock.mode === 'yarn' ? 'yarn remove -W' : lock.mode + ' uninstall'
print(
'Cleaning package.json dependencies from caniuse-lite\n' +
pico.yellow('$ ' + del + ' caniuse-lite') +
'\n'
)
childProcess.execSync(del + ' caniuse-lite')
}
module.exports = function updateDB(print) {
var lock = detectLockfile()
var latest = getLatestInfo(lock)
var browsersListRetrievalError
var oldBrowsersList
try {
oldBrowsersList = getBrowsersList()
} catch (e) {
browsersListRetrievalError = e
}
print('Latest version: ' + pico.bold(pico.green(latest.version)) + '\n')
if (lock.mode === 'yarn' && lock.version !== 1) {
var update = 'yarn up -R'
print(
'Updating caniuse-lite version\n' +
pico.yellow('$ ' + update + ' caniuse-lite') +
'\n'
)
try {
childProcess.execSync(update + ' caniuse-lite')
} catch (e) /* istanbul ignore next */ {
print(
pico.red(
'\n' +
e.stack +
'\n\n' +
'Problem with `' +
update +
' caniuse-lite` call. ' +
'Run it manually.\n'
)
)
process.exit(1)
}
} else {
updatePackageManually(print, lock, latest)
}
print('caniuse-lite has been successfully updated\n')
var currentBrowsersList
if (!browsersListRetrievalError) {
try {
currentBrowsersList = getBrowsersList()
} catch (e) /* istanbul ignore next */ {
browsersListRetrievalError = e
}
}
if (browsersListRetrievalError) {
print(
pico.red(
'\n' +
browsersListRetrievalError.stack +
'\n\n' +
'Problem with browser list retrieval.\n' +
'Target browser changes won’t be shown.\n'
)
)
} else {
var targetBrowserChanges = diffBrowsersLists(
oldBrowsersList,
currentBrowsersList
)
if (targetBrowserChanges) {
print('\nTarget browser changes:\n')
print(targetBrowserChanges + '\n')
} else {
print('\n' + pico.green('No target browser changes') + '\n')
}
}
}