-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwrite-rules.js
172 lines (139 loc) · 4.63 KB
/
write-rules.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
const fs = require('node:fs');
const stylelint = require('stylelint');
const COMMENT = '<!-- RULES:START -->';
const README = 'README.md';
const getUrl = (rule) => {
const parts = rule.split('/');
if (parts.length === 1) {
return `https://stylelint.io/user-guide/rules/list/${rule}/`;
}
const [group, ruleName] = parts;
const nonStylelintRules = {
'order': () =>
`https://github.com/hudochenkov/stylelint-order/blob/master/rules/${ruleName}/README.md`,
'scale-unlimited': () =>
'https://github.com/AndyOGo/stylelint-declaration-strict-value',
'scss': () =>
`https://github.com/stylelint-scss/stylelint-scss/blob/master/src/rules/${ruleName}/README.md`,
};
return (nonStylelintRules[group] || (() => null))();
};
/**
* Attempts to parse the config value to return a pretty formatted version.
* If the value becomes too long it will instead direct the viewer to an external link.
*
* @param {array} config
* @param {object?} options
* @returns {string}
*/
const getPrettyConfig = (
config,
{ defaultValue = 'Enabled', maxLength = 75 } = {},
) => {
if (!Array.isArray(config)) return defaultValue;
const configItems = config.filter((item) => typeof item !== 'boolean');
const prettyConfig = configItems.map(JSON.stringify).join(', ');
if (prettyConfig.length === 0) return defaultValue;
if (prettyConfig.length > maxLength) {
return `${defaultValue} - [see Config][config]`;
}
return `\`${prettyConfig}\``;
};
/**
* Prepares a reasonably formatted markdown table based on the spec
* https://github.github.com/gfm/#tables-extension-
*
* Ensuring that column widths remain readable while viewing the markdown raw.
*
* If a rule URL can be resolved, the rule will be added with a link to it, otherwise
* the rule will be just shown as is.
*
* Rule configuration is not yet included in the table.
*
* @param {Object} rules
* @returns {String}
*/
const createMarkdownTable = (rules) => {
const enabledRules = Object.entries(rules)
.filter(([, value]) => value !== null)
.sort(([ruleA], [ruleB]) => ruleA.localeCompare(ruleB));
const maxRuleLength =
enabledRules.reduce(
(maxLength, [rule]) =>
rule.length > maxLength ? rule.length : maxLength,
0,
) + 7; // adding padding + room for links
const prettyConfigs = enabledRules.map(([, config]) =>
getPrettyConfig(config),
);
const maxConfigLength = prettyConfigs.reduce(
(maxLength, prettyConfig) =>
prettyConfig.length > maxLength ? prettyConfig.length : maxLength,
0,
);
const tableRows = enabledRules.map(([rule], index) => {
const ruleBackticks = `\`${rule}\``;
const url = getUrl(rule);
const ruleFormatted = url ? `[${ruleBackticks}][${index}]` : ruleBackticks;
return [
`| ${ruleFormatted.padEnd(maxRuleLength + 1, ' ')} | ${prettyConfigs[
index
].padEnd(maxConfigLength, ' ')} |`,
url && `[${index}]: ${url}`,
];
});
const tableHeader = [
`| ${'Rule'.padEnd(maxRuleLength, ' ')} | ${'Config'.padEnd(
maxConfigLength,
' ',
)} |`,
`| ${''.padEnd(maxRuleLength, '-')}- | ${''.padEnd(
maxConfigLength,
'-',
)} |`,
].join('\n');
return `
${tableHeader}
${tableRows.map(([row]) => row).join('\n')}
[config]: https://github.com/wagtail/stylelint-config-wagtail/blob/main/index.js
${tableRows
.map(([, link]) => link)
.filter(Boolean)
.join('\n')}
`;
};
/**
* Reads the rules configuration from stylelint and writes an updated table
* to the README.md file.
*
* If run with `--check`, no content will be written and instead an exit
* code will fire to flag a CI error.
*/
const writeRules = async () => {
const { rules } = await stylelint.resolveConfig('.stylelintrc');
const markdownTable = createMarkdownTable(rules);
fs.readFile(README, (fileReadError, buf) => {
if (fileReadError) throw fileReadError;
const content = buf.toString();
const startOfTable = content.indexOf(COMMENT) + COMMENT.length;
const newContent = content.substring(0, startOfTable).concat(markdownTable);
// if in check mode, only compare contents and do not write to the file
const args = process.argv.slice(2) || [];
const isCheck = args.includes('--check');
if (isCheck) {
if (newContent !== content) {
process.stderr.write(
'Rules have not been written, run `npm run write-rules`\n',
);
process.exit(1);
}
process.exit(0);
} else {
// if not checking - ok to write to the file
fs.writeFile(README, newContent, (fileWriteError) => {
if (fileWriteError) throw fileWriteError;
});
}
});
};
writeRules();