forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsri-update.mjs
More file actions
72 lines (63 loc) · 2.13 KB
/
Copy pathsri-update.mjs
File metadata and controls
72 lines (63 loc) · 2.13 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
/**
* SRI Update will update the sri-history.json file
* In the project root. The sri-history.json file contains
* SRI hashes of each released version. This can be used
* to validate that something is a known axe-core source
* file.
*
* When running `npm run release`, this script will execute and
* update sri-history.json with the SRIs of axe{.*}.js.
* @deprecated
*/
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { createRequire } from 'node:module';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const require = createRequire(import.meta.url);
const sriToolbox = require('sri-toolbox');
// Check if we should be validating or updating
const validate = process.argv.some(arg => arg === '--validate');
const root = path.join(__dirname, '..');
const axeVersion = JSON.parse(
fs.readFileSync(path.join(root, 'package.json'), 'utf8')
).version;
let axeHistory = JSON.parse(
fs.readFileSync(path.join(root, 'sri-history.json'), 'utf8')
);
if (typeof axeHistory[axeVersion] !== 'object') {
axeHistory[axeVersion] = {};
}
const versionSRIs = axeHistory[axeVersion];
// List all axe files (including minified and localized axe files)
const axeFiles = fs
.readdirSync(root)
.filter(file => file.match(/^axe(\.[a-z.-]+)?\.js$/));
axeFiles.forEach(axeFile => {
const axeSource = fs.readFileSync(path.join(root, axeFile), 'utf-8');
const axeIntegrity = sriToolbox.generate(
{ algorithms: ['sha256'] },
axeSource
);
if (!validate) {
// Update SRI
versionSRIs[axeFile] = axeIntegrity;
// Test if the SRI shouldn't be changed
} else if (versionSRIs[axeFile] && versionSRIs[axeFile] !== axeIntegrity) {
console.log(axeFile, 'did not match the SRI in sri-history.json');
process.exitCode = 1;
}
});
if (!validate) {
fs.writeFileSync(
path.join(root, './sri-history.json'),
JSON.stringify(axeHistory, null, '\t'),
'utf-8'
);
console.log('Updated sri-history.json ');
} else if (process.exitCode === 1) {
console.log(
'\nMake sure the package version and sri-history.json is updated ' +
'before publishing to NPM.\n'
);
}