-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path2016youtubetv_setup.js
89 lines (73 loc) · 2.76 KB
/
2016youtubetv_setup.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
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const { exec } = require('child_process');
const settingsPath = path.join(__dirname, './back/settings.json');
if (!fs.existsSync(path.join(__dirname, 'back'))) {
fs.mkdirSync(path.join(__dirname, 'back'));
}
function getLocalIp() {
return new Promise((resolve, reject) => {
const platform = process.platform;
if (platform === 'linux' || platform === 'darwin') {
exec("hostname -I | awk '{print $1}'", (error, stdout, stderr) => {
if (error) {
reject(`Error getting IP: ${stderr}`);
} else {
resolve(stdout.trim());
}
});
} else if (platform === 'win32') {
exec("ipconfig | findstr /R /C:\"IPv4\" ", (error, stdout, stderr) => {
if (error) {
reject(`Error getting IP: ${stderr}`);
} else {
const ipMatch = stdout.match(/(?:\d{1,3}\.){3}\d{1,3}/);
resolve(ipMatch ? ipMatch[0] : 'localhost');
}
});
} else {
reject('Unsupported platform');
}
});
}
function promptUser(query) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise((resolve) => {
rl.question(query, (answer) => {
rl.close();
resolve(answer);
});
});
}
async function setup() {
try {
let currentIp = await getLocalIp();
console.log(`Current local IP address: ${currentIp}`);
let settings;
if (!fs.existsSync(settingsPath)) {
const defaultSettings = {
serverIp: 'localhost',
expBrowse: false
};
fs.writeFileSync(settingsPath, JSON.stringify(defaultSettings, null, 4));
console.log("Created settings.json with default serverIp = localhost and expBrowse = false.");
settings = defaultSettings;
} else {
settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
console.log(`Current settings in settings.json: ${JSON.stringify(settings, null, 4)}`);
}
const newIp = await promptUser(`Current server IP is: ${settings.serverIp}. Do you want to set a custom IP? (default: localhost): `);
if (newIp && newIp.trim() !== '') {
settings.serverIp = newIp.trim();
}
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 4));
console.log(`Updated settings.json with serverIp: ${settings.serverIp}`);
} catch (err) {
console.error('Error during setup:', err);
}
}
setup();