-
Notifications
You must be signed in to change notification settings - Fork 0
/
minem.js
executable file
·91 lines (78 loc) · 2.76 KB
/
minem.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
#!/usr/bin/env node
'use strict';
const winston = require('winston');
const chalk = require('chalk');
const cli = require('commander');
const Promise = require('bluebird');
const os = require('os');
const path = require('path');
const fs = Promise.promisifyAll(require('fs'));
const logger = winston.createLogger({
transports: new winston.transports.Console(),
format: winston.format.printf(log => {
switch (log.level) {
case 'info':
return chalk.bgBlack(`${chalk.bold.yellow(log.level)}: ${log.message}`);
case 'error':
return chalk.bgBlack(`${chalk.bold.redBright(log.level)}: ${chalk.underline(log.message)}`);
default:
return chalk.bgBlack(`${log.level}: ${log.message}`);
}
})
});
const defaultConfig = `{
"serverFile": "server.jar",
"serverDir": "server",
"mem": {
"min": "1G",
"max": "2G"
}
}`;
const globalConfigPath = path.join(os.homedir(), '.minem.json');
if (!fs.existsSync(globalConfigPath)) {
fs.writeFileSync(globalConfigPath, '{}');
}
cli.version('1.1.6', '-v, --version');
cli
.command('init')
.description('creates a minem.json at the current directory')
.action(() => {
logger.log('info', 'creating minem.json');
fs.writeFileAsync('minem.json', defaultConfig)
.then(() => {
logger.log('info', 'creating server directory');
if (!fs.existsSync('server')) return fs.mkdirAsync('server');
})
.then(() => {
logger.log('info', 'creating eula.txt');
logger.log('info', '(you agree to this): https://account.mojang.com/documents/minecraft_eula');
return fs.writeFileAsync(path.join('server', 'eula.txt'), 'eula=true');
})
.then(() => {
return fs.readFileAsync(globalConfigPath);
})
.then(file => {
const globalConfig = JSON.parse(file);
if (!globalConfig.servers) globalConfig.servers = [];
const name = path.basename(process.cwd());
if (!globalConfig.servers.some(s => s.name === name)) {
globalConfig.servers.push({name, path: process.cwd(), status: 'offline'});
return fs.writeFileAsync(globalConfigPath, JSON.stringify(globalConfig));
}
})
.catch(e => logger.log('error', e));
});
cli
.command('download <version>')
.alias('get')
.description('downloads minecraft server version <version>, use \'latest\' as version to download the latest version and \'latest-snapshot\' for the latest snapshot version')
.action(require('./download')(logger));
cli
.command('start [name]')
.description('starts a minecraft server in the current directory or starts [name]')
.action(require('./start')(logger));
cli
.command('server')
.description('manages a global list of servers')
.action(require('./server')(logger));
cli.parse(process.argv);