-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
51 lines (40 loc) · 1.46 KB
/
index.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
45
46
47
48
49
50
51
#!/usr/bin/env node
const fs = require('fs');
const babel = require('@babel/core');
const parse = require('./parse');
const transform = require('./transform');
const commander = require('commander');
commander
.version('0.1.0')
.usage('<file> [options]')
.option('-i, --interactive', 'Ask about each type before adding it', {
isDefault: true
})
.option('-a, --auto', 'Pick the types for me')
.option('-o, --output [output]', 'Output file. Defaults to stdout')
.parse(process.argv);
if (process.argv.length < 3) throw new Error('Expected one input file.');
if (commander.interactive && commander.auto)
throw new Error('Cannot be both interactive and autonomous');
const interactive = commander.interactive || !commander.auto;
const fileName = process.argv[2];
let variableToTypeMap = [];
let functionToTypeMap = [];
let argumentToTypeMap = [];
function getMaps() {
return [variableToTypeMap, functionToTypeMap, argumentToTypeMap];
}
function setMaps([_variableToTypeMap, _functionToTypeMap, _argumentToTypeMap]) {
variableToTypeMap = _variableToTypeMap;
functionToTypeMap = _functionToTypeMap;
argumentToTypeMap = _argumentToTypeMap;
}
const src = fs.readFileSync(fileName).toString();
let { code } = babel.transform(src, {
plugins: [parse(setMaps)]
});
code = babel.transform(code, {
plugins: [transform(interactive, getMaps)]
}).code;
if (commander.output) fs.writeFileSync(commander.output, code);
else console.log(code);