-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·75 lines (53 loc) · 1.62 KB
/
Copy pathmain.js
File metadata and controls
executable file
·75 lines (53 loc) · 1.62 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env node
const fs = require('fs')
const chalk = require('chalk')
if (process.argv.length < 3) {
console.log(chalk.red('Please provide a source file'))
return
}
let sourcePath = process.argv[2]
const caseClassRegex = /(case)\s+(class)\s+(\w+)\s+\(\s*(\s*\w+\s*:\s*\w+\s*,?)*\s*\)/g
const propertiesRegex = /\w+\s*:\s*\w+/g
if (!fs.existsSync(sourcePath)) {
console.log(chalk.red('Could not find provided source file'))
return
}
let typeMap = {
Int: 'number',
String: 'string',
Double: 'number',
Long: 'number',
Float: 'number',
Boolean: 'boolean',
Char: 'char'
}
function getType(key) {
// TODO: Check for Optional and List
return typeMap[key] || 'any';
}
data = fs.readFileSync(sourcePath, 'utf8')
results = Array.from(data.matchAll(caseClassRegex), x => x[0])
results.forEach(caseClass => {
caseClassRegex.lastIndex = 0
let tokens = caseClassRegex.exec(caseClass)
let className = tokens[3]
console.log(`Generating TypeScript for class: ${chalk.yellow(className)}`)
let tsClass = `
// Typerift v1.0.3
// Generated from ${sourcePath} on ${new Date()}
// Do not modify this file directly!
interface ${className} {\n`
let properties = caseClass.match(propertiesRegex)
properties.forEach(property => {
let pair = property.split(':')
pair = pair.map(x => x.trim())
let type = getType(pair[1])
tsClass += ` ${pair[0]}: ${type}; // ${pair[1]}\n`
})
tsClass += '}\n'
fs.writeFile(`${className}.ts`, tsClass, (err, file) => {
if (err) {
console.log(chalk.red(err))
}
})
})