This repository has been archived by the owner on Jun 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
143 lines (131 loc) · 3 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const fs = require('fs');
const path = require('path');
const Script = function () {
this.text = '';
this.name = '';
this.getText = function () {
return this.text;
};
this.setText = function (str) {
this.text = str;
};
this.getName = function () {
return this.name;
};
this.setName = function (str) {
this.name = str;
};
};
var nameStr = '';
const commands = {};
var prefixStr = '';
var indentLevel = 0;
var script = null;
function getPrefix() {
var indentStr = '';
for (i = 0; i < indentLevel; i++) {
indentStr = indentStr + ' ';
}
return prefixStr + indentStr;
}
function write(str) {
if (!script || (script && !(script instanceof Script))) {
fs.appendFileSync(path.dirname(require.main.filename) + '/' + nameStr, getPrefix() + str + '\n');
} else {
script.setText(script.getText() + getPrefix() + str + '\n');
}
}
const commandsJson = require('./commands.json');
for (let x in commandsJson) {
commands[x] = function (...str) {
write(commandsJson[x].replace(new RegExp('%', 'g'), str.join(', ')));
};
}
const conditionsJson = require('./conditions.json');
for (let x in conditionsJson) {
commands[x] = function (...str) {
return conditionsJson[x].replace(new RegExp('%', 'g'), str.join(', '));
};
}
commands.on = function (key, callback) {
write(key + '::');
indentLevel++;
callback();
indentLevel--;
write('Return');
};
commands.If = function (condition, callback) {
if (indentLevel > 0) {
write('if (' + condition + ') {');
indentLevel++;
} else {
write('#If (' + condition + ')');
}
callback();
if (indentLevel > 0) {
indentLevel--;
write('}');
return {
Else: function (callback) {
write('else {');
indentLevel++;
callback();
indentLevel--;
write('}');
}
};
} else {
write('#If');
}
};
commands.set = function (key, value) {
write(key + ' := ' + value);
};
function createGet(current) {
var get = function (str) {
var term = current + '.' + str;
if (current.length < 1) {
term = str;
}
class Term {
constructor(term) {
this.get = createGet(term);
this.term = term;
}
run(...args) {
write(term + '(' + args.join(', ') + ')');
}
runInline(...args) {
return this.term + '(' + args.join(', ') + ')';
}
contents() {
return '%' + this.term + '%';
}
toString() {
return this.term;
}
}
return new Term(term);
};
return get;
}
commands.get = createGet('');
module.exports = {
init: function (name, scriptObj) {
script = null;
if (scriptObj && scriptObj instanceof Script) {
script = scriptObj;
}
indentLevel = 0;
prefixStr = '';
nameStr = name + '.ahk';
Object.assign(global, commands);
if (!scriptObj) {
fs.writeFileSync(path.dirname(require.main.filename) + '/' + nameStr, '');
} else {
script.setText('');
script.setName(nameStr);
}
},
Script: Script
};