From a768ca4d44a73258f5379896a0b27b682609d525 Mon Sep 17 00:00:00 2001 From: sky Date: Wed, 10 Jun 2020 14:55:09 +0800 Subject: [PATCH] fix: https://github.com/easy-team/easywebpack/issues/76 --- package.json | 2 +- test/utils.test.js | 21 +++++++++++++++++++++ utils/utils.js | 27 +++++++++++---------------- 3 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 test/utils.test.js diff --git a/package.json b/package.json index 4b167af..e9308ab 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "easywebpack", - "version": "4.12.7", + "version": "4.12.8", "description": "基于 Webpack 的前端构建工程化解决方案", "keywords": [ "webpack", diff --git a/test/utils.test.js b/test/utils.test.js new file mode 100644 index 0000000..79abbc7 --- /dev/null +++ b/test/utils.test.js @@ -0,0 +1,21 @@ +'use strict'; +const expect = require('chai').expect; +const utils = require('../utils/utils'); + +describe('utils.test.js', () => { + before(() => {}); + + after(() => {}); + + beforeEach(() => {}); + + afterEach(() => {}); + + describe('#utils test', () => { + it('should ip test', () => { + const ip = utils.getIp(); + expect(ip).to.be.not.equal('127.0.0.1'); + expect(ip.split('.').length).to.be.equal(4); + }); + }); +}); \ No newline at end of file diff --git a/utils/utils.js b/utils/utils.js index 2bb96ec..27a2414 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -331,28 +331,23 @@ utils.loadNodeModules = isCache => { utils.getIp = position => { const interfaces = os.networkInterfaces(); const ips = []; - - if (interfaces.en0) { - for (let i = 0; i < interfaces.en0.length; i++) { - if (interfaces.en0[i].family === 'IPv4') { - ips.push(interfaces.en0[i].address); - } - } - } - if (interfaces.en1) { - for (let i = 0; i < interfaces.en1.length; i++) { - if (interfaces.en1[i].family === 'IPv4') { - ips.push(interfaces.en1[i].address); - } + const ens = [interfaces.en0, interfaces.en1, interfaces.eth0, interfaces.eth1]; + ens.forEach(en => { + if (Array.isArray(en)) { + en.forEach(item => { + if (item.family === 'IPv4') { + ips.push(item.address); + } + }); } - } + }); if (position > 0 && position <= ips.length) { return ips[position - 1]; - } else if (ips.length) { + } + if (ips.length) { return ips[0]; } return '127.0.0.1'; - }; utils.getHost = port => {