-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemotion-transformer.js
42 lines (32 loc) · 992 Bytes
/
emotion-transformer.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
const fsP = require('fs').promises;
const MagicString = require('magic-string');
const emotionMapping = {
happy: '😃',
sad: '😢',
angry: '😠'
};
const runTransformation = async () => {
try {
const code = await fsP.readFile('input.js', 'utf8');
const s = new MagicString(code);
const pattern = /\b(happy|sad|angry)\b/g;
while ((match = pattern.exec(code))) {
const emotion = match[0];
const start = match.index;
const end = start + emotion.length;
s.overwrite(start, end, emotionMapping[emotion]);
}
const outputSourceMap = 'output.js.map';
const map = s.generateMap({
source: 'input.js',
file: outputSourceMap,
includeContent: true,
});
const sourceMapURL = '\n//# sourceMappingURL=' + outputSourceMap;
await fsP.writeFile('output.js', s.toString() + sourceMapURL);
await fsP.writeFile('output.js.map', map.toString());
} catch (err) {
throw err;
}
}
runTransformation();