-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathfix.js
More file actions
152 lines (130 loc) · 4.58 KB
/
fix.js
File metadata and controls
152 lines (130 loc) · 4.58 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
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
/* This file is a part of @mdn/browser-compat-data
* See LICENSE file for more information. */
import { readdir, readFile, stat, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { styleText } from 'node:util';
import esMain from 'es-main';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import dataFolders from '../scripts/lib/data-folders.js';
import fixBrowserOrder from './fixer/browser-order.js';
import fixCommonErrors from './fixer/common-errors.js';
import fixFeatureOrder from './fixer/feature-order.js';
import fixPropertyOrder from './fixer/property-order.js';
import fixStatementOrder from './fixer/statement-order.js';
import fixDescriptions from './fixer/descriptions.js';
import fixNotes from './fixer/notes.js';
import fixFlags from './fixer/flags.js';
import fixLinks from './fixer/links.js';
import fixMDNURLs from './fixer/mdn-urls.js';
import fixStatus from './fixer/status.js';
import fixMirror from './fixer/mirror.js';
import fixOverlap from './fixer/overlap.js';
import fixStandardTrackExceptions from './fixer/standard-track-exceptions.js';
import { IS_WINDOWS } from './utils.js';
/** @import {Stats} from 'node:fs' */
/** @import {LintOptions} from './types.js' */
const dirname = fileURLToPath(new URL('.', import.meta.url));
/** @type {Readonly<Record<string, function(string, string): Promise<string> | string>>} */
const FIXES = Object.freeze({
descriptions: fixDescriptions,
notes: fixNotes,
common_errors: fixCommonErrors,
flags: fixFlags,
links: fixLinks,
mdn_urls: fixMDNURLs,
status: fixStatus,
mirror: fixMirror,
overlap: fixOverlap,
browser_order: fixBrowserOrder,
feature_order: fixFeatureOrder,
property_order: fixPropertyOrder,
statement_order: fixStatementOrder,
standard_track_exceptions: fixStandardTrackExceptions,
});
/**
* Recursively load one or more files and/or directories passed as arguments and perform automatic fixes.
* @param {LintOptions} options The lint options
* @param {...string} files The files to load and perform fix upon
* @returns {Promise<void>}
*/
const load = async (options, ...files) => {
const fixes = Object.entries(FIXES)
.filter(([key]) => !options.only || options.only.includes(key))
.map(([, fix]) => fix);
for (let file of files) {
if (file.indexOf(dirname) !== 0) {
file = path.resolve(dirname, '..', file);
}
/** @type {Stats} */
let fsStats;
try {
fsStats = await stat(file);
} catch {
console.warn(
styleText('yellow', `File ${styleText('bold', file)} doesn't exist!`),
);
continue;
}
if (fsStats.isFile()) {
if (path.extname(file) === '.json' && !file.endsWith('.schema.json')) {
let initial = (await readFile(file, 'utf-8')).trim();
let expected = initial;
for (const fix of fixes) {
expected = await fix(file, expected);
}
if (IS_WINDOWS) {
// prevent false positives from git.core.autocrlf on Windows
initial = initial.replace(/\r/g, '');
expected = expected.replace(/\r/g, '');
}
if (initial !== expected) {
await writeFile(file, expected + '\n', 'utf-8');
}
}
} else {
const subFiles = (await readdir(file)).map((subfile) =>
path.join(file, subfile),
);
// Sort so files come before directories (e.g., meta.json before meta/).
// This ensures parent features are fixed before their sub-features.
subFiles.sort((a, b) => {
const aIsJson = a.endsWith('.json');
const bIsJson = b.endsWith('.json');
if (aIsJson !== bIsJson) {
return aIsJson ? -1 : 1;
}
return a.localeCompare(b);
});
await load(options, ...subFiles);
}
}
};
/**
* Fix any errors in specified file(s) and/or folder(s), or all of BCD
* @param {string[]} files The file(s) and/or folder(s) to fix. Leave undefined for everything.
* @param {LintOptions} options Lint options
* @returns {Promise<void>}
*/
const main = async (files, options) => {
await load(options, ...files);
};
if (esMain(import.meta)) {
const argv = yargs(hideBin(process.argv))
.command('$0 [files..]', false)
.positional('files', {
array: true,
description: 'The files to fix (leave blank to test everything)',
type: 'string',
})
.option('only', {
array: true,
description: 'The checks to run',
choices: Object.keys(FIXES),
})
.parseSync();
const { files = dataFolders, only } = argv;
await main(files, { only });
}
export default load;