-
Notifications
You must be signed in to change notification settings - Fork 0
/
opm-install.js
executable file
·86 lines (71 loc) · 2.47 KB
/
opm-install.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
#!/usr/bin/env node
const program = require('commander');
const chalk = require('chalk');
const path = require('path');
const axios = require('axios');
const tough = require('tough-cookie');
const qs = require('qs');
const addCookieJar = require('@3846masa/axios-cookiejar-support');
const cheerio = require('cheerio');
addCookieJar(axios);
const cookieJar = new tough.CookieJar();
program
.option('-u, --username <username>', 'Oxide account username (required)')
.option('-p, --password <password>', 'Oxide account password (required)')
.option('-d, --dir [dir]', 'Path to plugins directory (default: ./serverfiles/oxide/plugins)')
.option('-m, --manifest [path]', 'Manifest file location (default: ./opm-manifest.json)')
.parse(process.argv)
const DEFAULT_PLUGIN_PATH = './serverfiles/oxide/plugins'
const DEFAULT_MANIFEST_PATH = './opm-manifest.json'
const TARGET_DIR = path.resolve(program.dir || DEFAULT_PLUGIN_PATH)
const MANIFEST_PATH = path.resolve(program.manifest || DEFAULT_MANIFEST_PATH)
const manifest = require(MANIFEST_PATH);
const login = program.username || process.env.OXIDE_LOGIN
const password = program.password || process.env.OXIDE_PASSWORD
function authenticate() {
console.log(chalk.gray('Authenticating with Oxide...'))
return axios.post('http://oxidemod.org/login/login', qs.stringify({
login,
password
}), {
jar: cookieJar,
withCredentials: true
})
}
function onAuthenticated(response) {
console.log(chalk.green('Successfully authenticated with Oxide website.'))
return installPlugins();
}
function installPlugin(pluginData) {
console.log(chalk.white(`- Installing ${pluginData.name} by ${pluginData.author}`))
return new Promise((resolve, reject) => {
getPluginPage(pluginData)
.then(res => {
const $ = cheerio.load(res.data);
const link = $('.downloadButton a').attr('href')
resolve(link);
})
.catch(e => reject(e));
});
}
function getPluginPage(plugin) {
return axios.get(`http://oxidemod.org/plugins/${plugin.resourceId}`, {
jar: cookieJar,
withCredentials: true
})
}
function onPluginFailed(err) {
console.log(err);
}
function installPlugins() {
return Promise.all(manifest.map(p => installPlugin(p).catch(onPluginFailed)))
}
console.log(chalk.bold.white('Installing Oxide Plugins...'))
authenticate()
.then(onAuthenticated)
.then(() => {
console.log(chalk.bold.green('Plugin installation complete!'))
})
.catch((err) => {
console.error(err.stack || err);
});