Skip to content

Commit

Permalink
feat: add getLocalIPV4 function
Browse files Browse the repository at this point in the history
  • Loading branch information
sishen654 committed Feb 14, 2023
1 parent 2cc685c commit fe808f1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
8 changes: 8 additions & 0 deletions __test__/getLocalIp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import getLocalIPV4 from "@src/getLocalIPV4"

describe("getLocalIPV4", () => {
test("it works normal", () => {
expect(getLocalIPV4()).toBeTypeOf("string")
expect(getLocalIPV4().split(".").length).toEqual(4)
})
})
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@
},
"types": "dist/index.d.ts",
"scripts": {
"watch": "cross-env NODE_ENV=development gulp",
"watch": "dotenv NODE_ENV=development gulp",
"w": "yarn build --watch",
"build": "rimraf dist & cross-env NODE_ENV=production rollup -c",
"build": "rimraf dist & dotenv NODE_ENV=production rollup -c",
"test": "vitest run",
"test:watch": "vitest",
"coverage": "vitest run --coverage",
"p": "standard-version",
"c": " git push --tags",
"push": "git checkout main & git push gitee-dotenv main & git push github-dotenv main"
"push": "git checkout main & git push gitee-node-util main & git push github-node-util main"
},
"files": [
"dist/"
],
"devDependencies": {
"@mazp/dotenv": "1.2.2",
"@rollup/plugin-commonjs": "23.0.4",
"@rollup/plugin-node-resolve": "15.0.1",
"@types/fs-extra": "9.0.13",
Expand Down
18 changes: 18 additions & 0 deletions src/getLocalIPV4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os from "os"

export default function getLocalIPV4() {
const interfaces = os.networkInterfaces()
let IPAdress = '';
for (let devName in interfaces) {
let iface = interfaces[devName];
if (iface) {
for (let i = 0; i < iface.length; i++) {
let alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
IPAdress = alias.address;
}
}
}
}
return IPAdress
};
4 changes: 0 additions & 4 deletions src/getLocalIp.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getLocalIp from "./getLocalIp"
import getLocalIp from "./getLocalIPV4"

export default {
getLocalIp
Expand Down

0 comments on commit fe808f1

Please sign in to comment.