/* eslint-disable prettier/prettier */
const { networkInterfaces } = require('os');

const nets = networkInterfaces();
export const IPs = Object.create(null); // Or just '{}', an empty object

for (const name of Object.keys(nets)) {
    for (const net of nets[name]) {
        // Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
        // 'IPv4' is in Node <= 17, from 18 it's a number 4 or 6
        const familyV4Value = typeof net.family === 'string' ? 'IPv4' : 4
        if (net.family === familyV4Value && !net.internal) {
            if (!IPs[name]) {
                IPs[name] = [];
            }
            IPs[name].push(net.address);
        }
    }
}
 
// Short hand
if (IPs['en0']) {
  IPs['v4'] = IPs['en0'][0];
} else {
  IPs['v4'] = IPs['Ethernet'][0];
}

//// IPs
// {
//     "en0": [
//       "192.168.1.101"
//     ],
//     "eth0": [
//       "10.0.0.101"
//     ],
//     "<network name>": [
//       "<ip>",
//       "<ip alias>",
//       "<ip alias>",
//       ...
//     ]
//   }

//// IPs["en0"][0]
//"192.168.1.101"
 