forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path-prefix
executable file
·38 lines (32 loc) · 998 Bytes
/
path-prefix
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
#!/usr/bin/env node
// converts relative file paths at the beginning of each input line to absolute file paths
const path = require('path');
const fs = require('fs');
const rl = require('readline');
const REMOVE_COLORS = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
const dir = process.argv[2];
if (!dir) {
throw new Error(`usage: path-prefix DIR`);
}
const reldir = path.relative(process.cwd(), dir);
const ifc = rl.createInterface(process.stdin);
ifc.on('line', line => {
line = line.toString();
const [ relative, ...rest ] = line.split(':');
const rel = relative.replace(REMOVE_COLORS, '');
const absolute = path.join(dir, rel);
if (relative && fs.existsSync(absolute)) {
process.stdout.write(path.join(reldir, rel) + ':' + rest.join(':') + '\n');
} else {
process.stdout.write(line + '\n');
}
});
process.stdin.resume();
function exists(p) {
try {
fs.readFileSync(p);
return true;
} catch (e) {
return false;
}
}