-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·143 lines (137 loc) · 5.01 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
#!/usr/bin/env node
const ArgumentParser = require('argparse').ArgumentParser;
const which = require('which');
const child_process = require('child_process');
const argparse = require('argparse');
const assets = require('./assets');
const xml2json = require('xml2js');
const readline = require('readline-sync');
const axios = require('axios');
const fs = require('fs');
const paperspigot_maven_metadata_url = "https://papermc.io/repo/repository/maven-public/com/destroystokyo/paper/paper-api/maven-metadata.xml";
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.split(search).join(replacement);
};
var parser = new ArgumentParser({
version: '0.0.1',
addHelp: true,
description: 'Minecraft Plugin Skeleton Builder'
});
// parser.addArgument(
// ['--project'], {
// help: 'Project Name'
// }
// );
// parser.addArgument(
// ['--package'], {
// help: 'Package Name'
// }
// );
// parser.addArgument(
// ['--main'], {
// help: 'Main Method Name',
// defaultValue: 'main'
// }
// );
// parser.addArgument(
// ['--select-version'], {
// help: 'PaperSpigot API Version'
// }
// );
const args = parser.parseArgs();
const deleteall = (path) => {
var files = [];
if(fs.existsSync(path)) {
files = fs.readdirSync(path);
files.forEach(function(file, index) {
var curPath = path + "/" + file;
if(fs.statSync(curPath).isDirectory()) {
deleteall(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
const core = () => {
const project_name = readline.question('project name: ')
if (fs.existsSync(project_name)) {
const bool = readline.question(`Project [${project_name}] Exists, Override it ? (Y/N) : `)
let answer = bool.toUpperCase().toString();
if (answer === 'Y' || answer === '') {
deleteall(project_name);
} else {
process.exit();
}
}
const package_name = readline.question('package name: ');
const main = readline.question('main method: (main): ');
var main_method = '';
if (main.length == 0) {
main_method = 'main'
} else {
main_method = main;
}
try {
const src_folder = package_name.replaceAll('.','/');
const source_folder_full_path = `${project_name}/src/main/java/${src_folder}`;
fs.mkdirSync(`${source_folder_full_path}`, {
recursive: true
});
if (which.sync('git', {nothrow: true})) {
const init_git = readline.question(`use Git? (Y/N) : `).toUpperCase();
if (init_git == "Y" || init_git == "") {
let result = child_process.execSync('git init', {
cwd: `${project_name}`
});
console.log(result.toString());
fs.appendFileSync(`${project_name}/.gitignore`, assets.git_ignore);
}
}
axios.get(paperspigot_maven_metadata_url).then((res) => {
xml2json.parseString(res.data, (error, result) => {
var select_version;
const version = result.metadata.versioning[0];
const latest_version = version['latest'][0];
console.log("Version List: ");
const version_list = version['versions'][0]['version'];
version_list.forEach((ver, index) => {
console.log(`${index+1} - ${ver}`);
})
while (true) {
select_version = readline.question(`select PaperSpigot API version: [${latest_version}]: `);
if (select_version == '') {
select_version = latest_version;
break;
}
if (version_list[select_version -1] == undefined) {
console.log('incorrect version number, please select again');
continue;
}
select_version = version_list[select_version -1];
break;
}
fs.appendFileSync(`${project_name}/pom.xml`, assets.pom_xml_file.
replaceAll(`{{package_name}}`,package_name).
replaceAll(`{{project_name}}`, project_name).
replaceAll(`{{paperspigot_version}}`, select_version));
console.log(`SUCCESS`);
})
}).catch((err) => {
console.log(err)
process.exit();
})
fs.appendFileSync(`${project_name}/src/config.yml`, '')
fs.appendFileSync(`${project_name}/src/plugin.yml`, assets.plugin_yml_code.
replaceAll(`{{project_name}}`,project_name).
replaceAll(`{{main_method}}`, `${package_name}.${main_method}`));
fs.appendFileSync(`${source_folder_full_path}/${main_method}.java`, assets.main_method_code.
replaceAll(`{{package_name}}`, package_name).
replaceAll(`{{main_method_name}}`,main_method));
} catch (error) {
console.log(error);
}
}
core();