-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathapprove-rule.mjs
More file actions
83 lines (72 loc) · 2.95 KB
/
approve-rule.mjs
File metadata and controls
83 lines (72 loc) · 2.95 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
#!/usr/bin/env zx
import 'zx/globals';
import assert from 'assert';
import moment from 'moment';
import {
config,
cloneWcagActRules,
createOrCheckoutBranch,
commitAndPush
} from './commons.mjs';
const w3cDataFormat = 'D MMMM YYYY';
const isoDateFormat = 'YYYY-MM-DD';
assert(typeof argv.ruleId === 'string', 'Expected --ruleId to be set');
assert(argv.ruleId.length === 6, 'Expected --ruleId to be 6 characters long');
assert(typeof argv.branch === 'string', 'Expected --branch to be set');
if (!argv['skip-clone']) {
await cloneWcagActRules(config);
}
await createOrCheckoutBranch(config, argv.branch);
await generateApprovedRulePages(config, argv.ruleId);
await updateRuleVersionsYaml(config, argv.ruleId);
await approveexampleJson(config, argv.ruleId);
await commitAndPush(config, `Set ${argv.ruleId} to approved`);
async function generateApprovedRulePages({ tmpDir, rulesDir, glossaryDir, testAssetsDir }, ruleId) {
await $`node ./node_modules/act-tools/dist/cli/rule-transform.js \
--rulesDir "${rulesDir}" \
--glossaryDir "${glossaryDir}" \
--testAssetsDir "${testAssetsDir}" \
--outDir "${tmpDir}" \
--ruleIds "${ruleId}"
`;
}
async function updateRuleVersionsYaml({ tmpDir }, ruleId) {
const ruleVersionPath = `${tmpDir}_data/wcag-act-rules/rule-versions.yml`;
let ruleVersionsStr = fs.readFileSync(ruleVersionPath, 'utf8');
const ruleVersions = YAML.parse(ruleVersionsStr);
assert(
ruleVersions[ruleId] === undefined,
`RuleID ${ruleId} should not exists in rule-versions.yml. Was this rule approved before?`
);
const proposedText = fs.readFileSync(`${tmpDir}content/rules/${ruleId}/proposed.md`, 'utf8');
const proposedData = proposedText.match(/last_modified:\s+(.*)/)?.[1]
assert(proposedData, `Unable to find last_modified data in ${ruleId}/proposed.md`);
ruleVersions[ruleId] = [{
file: 'proposed.md',
url: `${ruleId}/proposed/`,
w3cDate: proposedData,
isoDate: moment(proposedData, w3cDataFormat).format(isoDateFormat)
}, {
file: 'index.md',
url: `${ruleId}/`,
w3cDate: moment().format(w3cDataFormat),
isoDate: moment().format(isoDateFormat)
}]
ruleVersionsStr = YAML.stringify(ruleVersions);
fs.writeFileSync(ruleVersionPath, ruleVersionsStr, 'utf8');
console.log(`Added ${ruleId} to rule-versions.yml`);
}
async function approveexampleJson({ tmpDir }, ruleId) {
let exampleCount = 0;
const exampleJsonPath = `${tmpDir}content-assets/wcag-act-rules/examples.json`;
const exampleJson = JSON.parse(fs.readFileSync(exampleJsonPath, 'utf8'));
exampleJson.examples.forEach((example, index) => {
if (example.ruleId === ruleId) {
// Override rather than update so that `approved` isn't at the bottom
exampleJson.examples[index] = { ruleId, approved: true, ...example }
exampleCount++
}
});
console.log(`Set ${exampleCount} examples of rule ${ruleId} to be approved in examples.json`);
fs.writeFileSync(exampleJsonPath, JSON.stringify(exampleJson, null, 2), 'utf8');
}