forked from adamlaska/browser-compat-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-browser-order.js
More file actions
104 lines (89 loc) · 2.37 KB
/
fix-browser-order.js
File metadata and controls
104 lines (89 loc) · 2.37 KB
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
#!/usr/bin/env node
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
/**
* Return a new "support_block" object whose first-level properties
* (browser names) have been ordered according to Array.prototype.sort,
* and so will be stringified in that order as well. This relies on
* guaranteed "own" property ordering, which is insertion order for
* non-integer keys (which is our case).
*
* @param {string} key The key in the object
* @param {*} value The value of the key
*
* @returns {*} The new value
*/
'use strict';
const fs = require('fs');
const path = require('path');
const { platform } = require('os');
/** Determines if the OS is Windows */
const IS_WINDOWS = platform() === 'win32';
const orderSupportBlock = (key, value) => {
if (key === '__compat') {
value.support = Object.keys(value.support)
.sort()
.reduce((result, key) => {
result[key] = value.support[key];
return result;
}, {});
}
return value;
};
/**
* @param {string} filename
*/
const fixBrowserOrder = filename => {
let actual = fs.readFileSync(filename, 'utf-8').trim();
let expected = JSON.stringify(JSON.parse(actual, orderSupportBlock), null, 2);
if (IS_WINDOWS) {
// prevent false positives from git.core.autocrlf on Windows
actual = actual.replace(/\r/g, '');
expected = expected.replace(/\r/g, '');
}
if (actual !== expected) {
fs.writeFileSync(filename, expected + '\n', 'utf-8');
}
};
if (require.main === module) {
/**
* @param {string[]} files
*/
function load(...files) {
for (let file of files) {
if (file.indexOf(__dirname) !== 0) {
file = path.resolve(__dirname, '..', file);
}
if (!fs.existsSync(file)) {
continue; // Ignore non-existent files
}
if (fs.statSync(file).isFile()) {
if (path.extname(file) === '.json') {
fixBrowserOrder(file);
}
continue;
}
const subFiles = fs.readdirSync(file).map(subfile => {
return path.join(file, subfile);
});
load(...subFiles);
}
}
if (process.argv[2]) {
load(process.argv[2]);
} else {
load(
'api',
'css',
'html',
'http',
'svg',
'javascript',
'mathml',
'test',
'webdriver',
'webextensions',
);
}
}
module.exports = fixBrowserOrder;