-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate-i-shims.js
More file actions
executable file
·54 lines (48 loc) · 1.82 KB
/
Copy pathgenerate-i-shims.js
File metadata and controls
executable file
·54 lines (48 loc) · 1.82 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
#!/usr/bin/env node
const { requireEnv: _hmeRequireEnv } = require('../proxy/shared/load_env.js');
'use strict';
const fs = require('fs');
const path = require('path');
const root = _hmeRequireEnv('PROJECT_ROOT');
const registry = JSON.parse(fs.readFileSync(path.join(root, 'tools/HME/i_registry.json'), 'utf8'));
const iDir = path.join(root, 'tools', 'HME', 'i');
const check = process.argv.includes('--check');
function bodyFor(name) {
return `#!/usr/bin/env bash
# generated by tools/HME/scripts/generate-i-shims.js; edit tools/HME/i_registry.json or tools/HME/scripts/hme-i-dispatch.js
exec "$(cd "$(dirname "$0")/.." && pwd)/scripts/hme-i-dispatch.js" "${name}" "$@"
`;
}
const commands = Object.keys(registry.commands || {}).sort();
const expected = new Map(commands.map((name) => [name, bodyFor(name)]));
const actualFiles = fs.readdirSync(iDir).filter((name) => {
if (name.startsWith('.')) return false;
return fs.statSync(path.join(iDir, name)).isFile();
}).sort();
if (check) {
const issues = [];
for (const file of actualFiles) {
if (!expected.has(file)) issues.push(`extra i/${file}`);
}
for (const [name, body] of expected) {
const target = path.join(iDir, name);
if (!fs.existsSync(target)) {
issues.push(`missing i/${name}`);
continue;
}
const current = fs.readFileSync(target, 'utf8');
if (current !== body) issues.push(`stale i/${name}`);
if ((fs.statSync(target).mode & 0o111) === 0) issues.push(`not executable i/${name}`);
}
if (issues.length) {
console.error(issues.join('\n'));
process.exit(1);
}
console.log(`i/ shims match ${path.relative(root, path.join(root, 'tools/HME/i_registry.json'))}`);
process.exit(0);
}
for (const [name, body] of expected) {
const target = path.join(iDir, name);
fs.writeFileSync(target, body);
fs.chmodSync(target, 0o755);
}