123456789101112131415161718192021222324252627282930313233343536373839404142 |
- const fs = require('fs');
- const os = require('os');
- function getIPAdress(flag = false) {
- const interfaces = os.networkInterfaces();
- for (const devName in interfaces) {
- const iface = interfaces[devName];
- for (let i = 0; i < iface.length; i++) {
- const alias = iface[i];
- const isIPV4 = !flag;
- if (isIPV4) {
- if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
- return alias.address;
- }
- } else if (alias.address !== '127.0.0.1' && !alias.internal) {
- if (alias.family === 'IPv6') {
- return alias.address;
- }
- }
- }
- }
- }
- const IPv4 = getIPAdress();
- const envFile = fs.readFileSync('.env.development', 'utf8');
- const envVars = envFile.split('\n').reduce((result, line) => {
- const parts = line.split('=');
- if (parts.length === 2) {
- result[parts[0].trim()] = parts[1].trim();
- }
- return result;
- }, {});
- envVars.VUE_APP_BASE_URL = `http://${IPv4}:8081`;
- const newEnvFile = Object.keys(envVars)
- .map(key => `${key}=${envVars[key]}`)
- .join('\n');
- fs.writeFileSync('.env.development', newEnvFile);
|