-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (86 loc) · 3.92 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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const mkdirp = require('mkdirp');
var source;
var world;
if(process.argv[2] == "i") {
if(!process.argv[3]) {
console.log("Please provide a function to install!")
}
source = process.argv[3];
world = process.argv[4] || process.cwd();
}
if(process.argv[2] == "h" || process.argv[2] == "help") {
console.log("Help:" + "\n" +
" fnex i <input> [folder] - installs the function tp the current folder" + "\n" +
" fnex h/help - show help")
}
if(source && world) {
fs.readFile(source, 'utf-8', function(err, data) {
if (err) throw err;
var author;
var project;
var files = {};
var array = data.toString().split("\n");
if(data.toString().includes("\r")) {
array = data.toString().split("\r\n");
}
var scanningfunc;
var scanning = false;
for(linen in array) {
line = array[linen];
if(scanning == true) {
if(line.startsWith("}")) {
scanning = false;
} else {
if(line.trim().startsWith("var")) {
var varcommand = line.trim().split(" ").splice(0);
if(varcommand[2] == "=") {
formattedline = "scoreboard players set @a "+varcommand[1]+" "+varcommand[3]+"\n";
files[scanningfunc] = files[scanningfunc] + formattedline;
} else {
console.log("Variable not defined correctly at line "+linen)
throw Error;
}
} else if(line.trim().startsWith("ifvar")) {
var varcommand = line.trim().split(" ").splice(0);
if(varcommand[2] == "==") {
var clength = varcommand[0].length + varcommand[1].length + varcommand[2].length + varcommand[3].length + 3;
formattedline = "execute @a[score_"+varcommand[1]+"="+varcommand[3]+",score_"+varcommand[1]+"_min="+varcommand[3]+"] ~ ~ ~"+line.trim().substring(clength);
files[scanningfunc] = files[scanningfunc] + formattedline;
} else if(varcommand[2] == ">") {
} else if(varcommand[2] == "<") {
} else {
console.log("if statement not properly constructed at line "+linen)
throw Error;
}
} else {
formattedline = line.trim().replace("//", "#") + "\n";
files[scanningfunc] = files[scanningfunc] + formattedline;
}
}
} else if(line.startsWith("@")) {
var propertyarray = line.split(" ");
if(propertyarray[0] == "@author") {
author = propertyarray[1];
} else if(propertyarray[0] == "@project") {
project = propertyarray[1];
}
} else if(line.startsWith("func")) {
var functionarray = line.split(" ").splice(1);
scanningfunc = functionarray[0];
files[scanningfunc] = "####################################\n# Auto Generated By FunctionEX #\n####################################\n\n";
scanning = true;
}
}
if(author && project) {
mkdirp(path.join(world, author.toString(), project.toString()), function (err) {
for(var filename in files) {
if (err) throw err;
fs.writeFileSync(path.join(world, author.toString(), project.toString(), filename.toString()+".mcfunction"), files[filename])
}
});
}
});
}