forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-context.mjs
More file actions
61 lines (58 loc) · 1.83 KB
/
Copy pathbuild-context.mjs
File metadata and controls
61 lines (58 loc) · 1.83 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
import fs from 'node:fs';
import path from 'node:path';
import { globSync } from 'glob';
/**
* Filesystem + template helpers for the axe-core build (Grunt replacement).
* @param {string} rootDir Absolute path to repository root
*/
export function createBuildContext(rootDir) {
const root = path.resolve(rootDir);
return {
root,
readFile(relPath) {
return fs.readFileSync(path.join(root, relPath), 'utf8');
},
readJSON(relPath) {
return JSON.parse(fs.readFileSync(path.join(root, relPath), 'utf8'));
},
writeFile(relPath, content) {
const abs = path.join(root, relPath);
fs.mkdirSync(path.dirname(abs), { recursive: true });
fs.writeFileSync(abs, content, 'utf8');
},
exists(relPath) {
return fs.existsSync(path.join(root, relPath));
},
/**
* Returns matches alphabetically sorted by path so downstream consumers
* (rule/check ordering in generated artifacts, locale templates, etc.)
* get stable, deterministic output across platforms — matching the
* historical Grunt/`grunt.file.expand` behavior.
*
* @param {string} pattern Glob relative to repo root (forward slashes ok)
* @returns {string[]} Paths relative to root, POSIX-style
*/
expandGlob(pattern) {
return globSync(pattern, { cwd: root, nodir: true, posix: true }).sort();
}
};
}
/**
* Minimal `<%= dotted.path %>` substitution (Grunt-style delimiters).
* @param {string} str
* @param {Record<string, unknown>} data
*/
export function templateProcess(str, data) {
return str.replace(/<%=\s*([\s\S]*?)\s*%>/g, (_, expr) => {
const key = expr.trim();
if (!key) {
return '';
}
const parts = key.split('.');
let v = data;
for (const p of parts) {
v = v?.[p];
}
return v === undefined || v === null ? '' : String(v);
});
}