Skip to content

Commit eae9f63

Browse files
committed
feat: add http server and how to use doc
1 parent 1bde147 commit eae9f63

File tree

5 files changed

+185
-56
lines changed

5 files changed

+185
-56
lines changed

README.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,87 @@
2020
[download-image]: https://img.shields.io/npm/dm/node-tool-utils.svg?style=flat-square
2121
[download-url]: https://npmjs.org/package/node-tool-utils
2222

23-
Node Tool Utils
23+
Node Cross-Platform Tool Library
2424

2525
## Featues
2626

27+
```bash
28+
npm install node-tool-utils --save
29+
```
30+
31+
## Usage
32+
33+
```js
34+
const tool = require('node-tool-utils');
35+
```
36+
37+
### Get Local IP Address
38+
39+
```js
40+
const ip = tool.getIP();
41+
```
42+
43+
### Get Local Host
44+
45+
```js
46+
const host = tool.getHost(7001);
47+
// http://100.10.196.1:7001
48+
```
49+
50+
### Kill the occupied port
51+
52+
```js
53+
tool.kill(7001);
54+
tool.kill([7001,7002]);
55+
```
56+
57+
### Check port is available
58+
59+
```js
60+
// return true or false
61+
const isUsed = tool.checkPortUsed(7001);
62+
```
63+
64+
### Get an available port
65+
66+
When 7001 is occupied, it will automatically detect whether 7002 is occupied. If it is not occupied, it will return. Otherwise, it will continue to increment detection. The default check is 10 times.
67+
68+
```js
69+
const port = tool.getPort(7001);
70+
```
71+
72+
### Delete File
73+
74+
```js
75+
const dir = path.join(__dirname, 'dist/index.html');
76+
tool.deleteFile(dir);
77+
```
78+
79+
### Delete Dir
80+
81+
```js
82+
const dir = path.join(__dirname, 'dist');
83+
tool.rm(dir);
84+
```
85+
86+
### Open Browser Or Window
87+
88+
Open the Window or Finder or Browser of the specified path
89+
90+
```js
91+
tool.open('.'); // open Window or Finder
92+
tool.openBrowser(); // open Browser
93+
```
94+
95+
### Start Web Http Server
96+
97+
Default check HTML file as homepage
98+
99+
``js
100+
const dist = path.join(__dirname, 'dist');
101+
tool.httpserver({ port: 8088, dist },() => {});
102+
```
103+
27104
## License
28105
29106
[MIT](LICENSE)

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
'use strict';
22
module.exports = require('./lib/tool');
3+
exports.chalk = require('chalk');
4+
exports.shell = require('shelljs');
5+
exports.glob = require('glob');
6+
exports.httpserver = require('node-http-server');
7+
exports.opn = require('opn');

lib/tool.js

Lines changed: 95 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,102 @@
11
'use strict';
2-
const path = require('path');
32
const os = require('os');
43
const fs = require('fs');
4+
const path = require('path');
5+
const chalk = require('chalk');
56
const shell = require('shelljs');
67
const opn = require('opn');
78
const killer = require('cross-port-killer');
9+
const httpserver = require('node-http-server');
10+
const glob = require('glob');
11+
12+
13+
exports.resolve = (filename, baseDir) => {
14+
baseDir = baseDir || process.cwd();
15+
if (filename) {
16+
return path.isAbsolute(filename) ? filename : path.resolve(baseDir, filename);
17+
}
18+
return baseDir;
19+
};
20+
21+
exports.getIp = position => {
22+
const interfaces = os.networkInterfaces();
23+
const ips = [];
24+
25+
if (interfaces.en0) {
26+
for (let i = 0; i < interfaces.en0.length; i++) {
27+
if (interfaces.en0[i].family === 'IPv4') {
28+
ips.push(interfaces.en0[i].address);
29+
}
30+
}
31+
}
32+
if (interfaces.en1) {
33+
for (let i = 0; i < interfaces.en1.length; i++) {
34+
if (interfaces.en1[i].family === 'IPv4') {
35+
ips.push(interfaces.en1[i].address);
36+
}
37+
}
38+
}
39+
if (position > 0 && position <= ips.length) {
40+
return ips[position - 1];
41+
} else if (ips.length) {
42+
return ips[0];
43+
}
44+
return '127.0.0.1';
45+
};
46+
47+
exports.getHost = port => {
48+
const ip = exports.getIp();
49+
if (port) {
50+
return `http://${ip}:${port}`;
51+
}
52+
return `http://${ip}`;
53+
};
54+
55+
exports.httpServer = (cfg, callback) => {
56+
const port = cfg.port || 8080;
57+
const root = exports.resolve(cfg.dist);
58+
let index = cfg.index;
59+
if (!index) {
60+
const files = glob.sync('*.html', { cwd: root, root });
61+
if (files.length > 0) {
62+
index = files[0];
63+
}
64+
}
65+
const options = {
66+
port,
67+
root,
68+
server: {
69+
index
70+
}
71+
};
72+
httpserver.deploy(options, server => {
73+
const url = `http://127.0.0.1:${server.config.port}`;
74+
const host = exports.getHost(server.config.port);
75+
console.log(chalk.green(`Http server ${chalk.yellow(url)} or ${chalk.yellow(host)} is serve ${chalk.blue(root)}\r\n`));
76+
callback && callback(server);
77+
});
78+
};
79+
80+
exports.exec = cmd => {
81+
return shell.exec(cmd);
82+
};
883

984
exports.rm = filepath => {
1085
const dirs = Array.isArray(filepath) ? filepath : [filepath];
1186
dirs.forEach(dir => {
12-
/* istanbul ignore next */
13-
if (os.platform() === 'win32') {
14-
exports.deleteFile(dir);
15-
console.log(`remove [ ${dir} ] success`);
16-
} else {
17-
const result = shell.exec(`rm -rf ${dir}`);
18-
if (result.code === 0) {
87+
if (fs.existsSync(dir)) {
88+
/* istanbul ignore next */
89+
if (os.platform() === 'win32') {
90+
exports.deleteFile(dir);
1991
console.log(`remove [ ${dir} ] success`);
2092
} else {
21-
/* istanbul ignore next */
22-
exports.deleteFile(dir);
93+
const result = shell.exec(`rm -rf ${dir}`);
94+
if (result.code === 0) {
95+
console.log(`remove [ ${dir} ] success`);
96+
} else {
97+
/* istanbul ignore next */
98+
exports.deleteFile(dir);
99+
}
23100
}
24101
}
25102
});
@@ -42,7 +119,7 @@ exports.deleteFile = filepath => {
42119
fs.unlinkSync(filepath);
43120
}
44121
}
45-
}
122+
};
46123

47124
/* istanbul ignore next */
48125
exports.open = filepath => {
@@ -63,12 +140,11 @@ exports.open = filepath => {
63140
}
64141
};
65142

66-
exports.openBrowser = (url, port) => {
67-
if (!url && port) {
68-
const ip = utils.getIp();
69-
url = `http://${ip}:${port}`;
143+
exports.openBrowser = (port, url) => {
144+
if (!url) {
145+
url = exports.getHost(port);
70146
}
71-
open(url);
147+
opn(url);
72148
};
73149

74150
exports.checkPortUsed = port => {
@@ -98,7 +174,7 @@ exports.checkPortUsed = port => {
98174
exports.getPort = (port, count = 10) => {
99175
let newPort = port;
100176
let checkPort = port;
101-
while(checkPort < port + count) {
177+
while (checkPort < port + count) {
102178
const isUsed = exports.checkPortUsed(checkPort);
103179
if (!isUsed) {
104180
newPort = checkPort;
@@ -110,10 +186,9 @@ exports.getPort = (port, count = 10) => {
110186
};
111187

112188
/* istanbul ignore next */
113-
exports.kill = function (port) {
189+
exports.kill = function(port) {
114190
if (port) {
115-
port = String(port);
116-
const ports = port.split(',');
191+
const ports = Array.isArray(port) ? port : String(port).split(',');
117192
ports.forEach(p => {
118193
killer.kill(p).then(() => {
119194
console.log(`kill port ${p} success`);
@@ -122,35 +197,4 @@ exports.kill = function (port) {
122197
});
123198
});
124199
}
125-
};
126-
127-
exports.getIp = position => {
128-
const interfaces = os.networkInterfaces();
129-
const ips = [];
130-
131-
if (interfaces.en0) {
132-
for (let i = 0; i < interfaces.en0.length; i++) {
133-
if (interfaces.en0[i].family === 'IPv4') {
134-
ips.push(interfaces.en0[i].address);
135-
}
136-
}
137-
}
138-
if (interfaces.en1) {
139-
for (let i = 0; i < interfaces.en1.length; i++) {
140-
if (interfaces.en1[i].family === 'IPv4') {
141-
ips.push(interfaces.en1[i].address);
142-
}
143-
}
144-
}
145-
if (position > 0 && position <= ips.length) {
146-
return ips[position - 1];
147-
} else if (ips.length) {
148-
return ips[0];
149-
}
150-
return '127.0.0.1';
151-
};
152-
153-
exports.getHost = port => {
154-
const ip = exports.getIp();
155-
return `http://${ip}:${port}`;
156200
};

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "node-tool-utils",
3-
"version": "0.1.0",
4-
"description": "node normal tool and utils",
3+
"version": "1.1.0",
4+
"description": "node cross-platform tool library",
55
"keywords": [
66
"npm",
77
"npm package",
@@ -13,9 +13,12 @@
1313
"changelog"
1414
],
1515
"dependencies": {
16+
"chalk": "^2.0.1",
1617
"cross-port-killer": "^1.0.1",
1718
"opn": "^5.4.0",
18-
"shelljs": "^0.8.2"
19+
"shelljs": "^0.8.2",
20+
"node-glob": "^1.2.0",
21+
"node-http-server": "^8.1.2"
1922
},
2023
"devDependencies": {
2124
"chai": "^4.1.1",

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
const tool = require('../lib/tool');
22
const port = tool.getPort(9000);
3-
tool.kill(7001);
3+
console.log(port);

0 commit comments

Comments
 (0)