Socket
Socket
Sign inDemoInstall

node-tool-utils

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-tool-utils - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

5

index.js
'use strict';
module.exports = require('./lib/tool');
exports.chalk = require('chalk');
exports.shell = require('shelljs');
exports.glob = require('glob');
exports.httpserver = require('node-http-server');
exports.opn = require('opn');

146

lib/tool.js
'use strict';
const path = require('path');
const os = require('os');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const shell = require('shelljs');
const opn = require('opn');
const killer = require('cross-port-killer');
const httpserver = require('node-http-server');
const glob = require('glob');
exports.resolve = (filename, baseDir) => {
baseDir = baseDir || process.cwd();
if (filename) {
return path.isAbsolute(filename) ? filename : path.resolve(baseDir, filename);
}
return baseDir;
};
exports.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);
}
}
}
if (position > 0 && position <= ips.length) {
return ips[position - 1];
} else if (ips.length) {
return ips[0];
}
return '127.0.0.1';
};
exports.getHost = port => {
const ip = exports.getIp();
if (port) {
return `http://${ip}:${port}`;
}
return `http://${ip}`;
};
exports.httpServer = (cfg, callback) => {
const port = cfg.port || 8080;
const root = exports.resolve(cfg.dist);
let index = cfg.index;
if (!index) {
const files = glob.sync('*.html', { cwd: root, root });
if (files.length > 0) {
index = files[0];
}
}
const options = {
port,
root,
server: {
index
}
};
httpserver.deploy(options, server => {
const url = `http://127.0.0.1:${server.config.port}`;
const host = exports.getHost(server.config.port);
console.log(chalk.green(`Http server ${chalk.yellow(url)} or ${chalk.yellow(host)} is serve ${chalk.blue(root)}\r\n`));
callback && callback(server);
});
};
exports.exec = cmd => {
return shell.exec(cmd);
};
exports.rm = filepath => {
const dirs = Array.isArray(filepath) ? filepath : [filepath];
dirs.forEach(dir => {
/* istanbul ignore next */
if (os.platform() === 'win32') {
exports.deleteFile(dir);
console.log(`remove [ ${dir} ] success`);
} else {
const result = shell.exec(`rm -rf ${dir}`);
if (result.code === 0) {
if (fs.existsSync(dir)) {
/* istanbul ignore next */
if (os.platform() === 'win32') {
exports.deleteFile(dir);
console.log(`remove [ ${dir} ] success`);
} else {
/* istanbul ignore next */
exports.deleteFile(dir);
const result = shell.exec(`rm -rf ${dir}`);
if (result.code === 0) {
console.log(`remove [ ${dir} ] success`);
} else {
/* istanbul ignore next */
exports.deleteFile(dir);
}
}

@@ -45,3 +122,3 @@ }

}
}
};

@@ -67,8 +144,7 @@ /* istanbul ignore next */

exports.openBrowser = (url, port) => {
if (!url && port) {
const ip = utils.getIp();
url = `http://${ip}:${port}`;
exports.openBrowser = (port, url) => {
if (!url) {
url = exports.getHost(port);
}
open(url);
opn(url);
};

@@ -103,3 +179,3 @@

let checkPort = port;
while(checkPort < port + count) {
while (checkPort < port + count) {
const isUsed = exports.checkPortUsed(checkPort);

@@ -116,6 +192,5 @@ if (!isUsed) {

/* istanbul ignore next */
exports.kill = function (port) {
exports.kill = function(port) {
if (port) {
port = String(port);
const ports = port.split(',');
const ports = Array.isArray(port) ? port : String(port).split(',');
ports.forEach(p => {

@@ -129,33 +204,2 @@ killer.kill(p).then(() => {

}
};
exports.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);
}
}
}
if (position > 0 && position <= ips.length) {
return ips[position - 1];
} else if (ips.length) {
return ips[0];
}
return '127.0.0.1';
};
exports.getHost = port => {
const ip = exports.getIp();
return `http://${ip}:${port}`;
};
{
"name": "node-tool-utils",
"version": "1.0.0",
"description": "node normal tool and utils",
"version": "1.1.0",
"description": "node cross-platform tool library",
"keywords": [

@@ -16,5 +16,8 @@ "npm",

"dependencies": {
"chalk": "^2.0.1",
"cross-port-killer": "^1.0.1",
"opn": "^5.4.0",
"shelljs": "^0.8.2"
"shelljs": "^0.8.2",
"node-glob": "^1.2.0",
"node-http-server": "^8.1.2"
},

@@ -21,0 +24,0 @@ "devDependencies": {

@@ -23,8 +23,85 @@ # node-tool-utils

Node Tool Utils
Node Cross-Platform Tool Library
## Featues
```bash
npm install node-tool-utils --save
```
## Usage
```js
const tool = require('node-tool-utils');
```
### Get Local IP Address
```js
const ip = tool.getIP();
```
### Get Local Host
```js
const host = tool.getHost(7001);
// http://100.10.196.1:7001
```
### Kill the occupied port
```js
tool.kill(7001);
tool.kill([7001,7002]);
```
### Check port is available
```js
// return true or false
const isUsed = tool.checkPortUsed(7001);
```
### Get an available port
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.
```js
const port = tool.getPort(7001);
```
### Delete File
```js
const dir = path.join(__dirname, 'dist/index.html');
tool.deleteFile(dir);
```
### Delete Dir
```js
const dir = path.join(__dirname, 'dist');
tool.rm(dir);
```
### Open Browser Or Window
Open the Window or Finder or Browser of the specified path
```js
tool.open('.'); // open Window or Finder
tool.openBrowser(); // open Browser
```
### Start Web Http Server
Default check HTML file as homepage
``js
const dist = path.join(__dirname, 'dist');
tool.httpserver({ port: 8088, dist },() => {});
```
## License
[MIT](LICENSE)
const tool = require('../lib/tool');
const port = tool.getPort(9000);
tool.kill(7001);
console.log(port);
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc