-
Notifications
You must be signed in to change notification settings - Fork 101
/
transform.js
44 lines (41 loc) · 893 Bytes
/
transform.js
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
var falafel = require('falafel');
process.stdin.setEncoding('utf8');
var src = "";
process.stdin.on('readable', function() {
var chunk = process.stdin.read();
if (chunk !== null) {
src += chunk;
}
});
process.stdin.on('end', function() {
var to = process.argv[2];
var from = src
.match(/\(function\((\w+,\s*)+(\w+)/g)[0]
.replace('(function(', '')
.split(',')
.map(
function(s) {
return s.trim();
}
);
var pairs = {};
for (var i = 0; i < from.length; i++) {
pairs[from[i]] = to[i];
}
var output = falafel(src, function(node) {
var name = (
node.type === 'Identifier' ?
node.name :
node.type === 'Literal' ? node.value : null
);
if (name && pairs[name]) {
if (node.type === 'Literal') {
node.update(node.raw.replace(name, pairs[name]));
}
else {
node.update(pairs[name]);
}
}
});
console.log(output.toString());
});