Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
caoli committed Sep 27, 2018
0 parents commit 23c6c60
Show file tree
Hide file tree
Showing 11 changed files with 369 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
test/fixtures
coverage
public
test/web
node_modules
.spec.js
coverage
config
.nyc_output
*.test.js
47 changes: 47 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"extends": "eslint",
"env": {
"node": true,
"es6": true,
"mocha": true
},
"parserOptions": {
"ecmaVersion": 6
},
"rules": {
"indent": [
"error",
2,
{ "SwitchCase": 1 }
],
"no-underscore-dangle": "off",
"consistent-return": "off",
"no-cond-assign": "off",
"lines-around-comment": "off",
"arrow-body-style": "off",
"no-confusing-arrow": "off",
"class-methods-use-this": "off",
"newline-after-var": "off",
"no-invalid-this": "off",
"no-nested-ternary": "off",
"no-unused-vars": "off",
"eol-last": "off",
"quotes": [
"error",
"single"
],
"guard-for-in": "off",
"no-console": "off",
"no-undefined": "off",
"array-bracket-spacing": "off",
"no-unused-expressions": "off",
"func-style": [
"error",
"expression"
],
"comma-dangle": [
"error",
"never"
]
}
}
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.js linguist-language=javascript
*.css linguist-language=javascript
*.html linguist-language=javascript
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
logs/
npm-debug.log
node_modules/
coverage/
.idea/
run/
.DS_Store
*.swp
.vscode
*.iml
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
sudo: false
language: node_js
node_js:
- '6'
- '8'
- '9'
script:
- npm test
after_success:
- npm run ci
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# npm-package-template

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/npm-package-template.svg?style=flat-square
[npm-url]: https://npmjs.org/package/npm-package-template
[travis-image]: https://img.shields.io/travis/hubcarl/npm-package-template.svg?style=flat-square
[travis-url]: https://travis-ci.org/hubcarl/npm-package-template
[codecov-image]: https://img.shields.io/codecov/c/github/hubcarl/npm-package-template.svg?style=flat-square
[codecov-url]: https://codecov.io/github/hubcarl/npm-package-template?branch=master
[david-image]: https://img.shields.io/david/hubcarl/npm-package-template.svg?style=flat-square
[david-url]: https://david-dm.org/hubcarl/npm-package-template
[snyk-image]: https://snyk.io/test/npm/npm-package-template/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/npm-package-template
[download-image]: https://img.shields.io/npm/dm/npm-package-template.svg?style=flat-square
[download-url]: https://npmjs.org/package/npm-package-template

NPM package template, It's easy for you to write a npm package by this NPM package template.

## Featues

- [x] NPM Package Code Tempalate
- [x] ESlint Rule Template [ESlint](http://eslint.cn/)
- [x] NPM Unit Test Template [chai](http://chaijs.com/api/bdd/)
- [x] Code Coverage Template [codecov](https://codecov.io/gh)
- [x] CI Build Config Template [travis](https://travis-ci.org/)
- [x] Changelog Create Template [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog)


## License

[MIT](LICENSE)
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
module.exports = require('./lib/tool');
156 changes: 156 additions & 0 deletions lib/tool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
'use strict';
const path = require('path');
const os = require('os');
const fs = require('fs');
const shell = require('shelljs');
const opn = require('opn');
const killer = require('cross-port-killer');

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) {
console.log(`remove [ ${dir} ] success`);
} else {
/* istanbul ignore next */
exports.deleteFile(dir);
}
}
});
};

exports.deleteFile = filepath => {
if (fs.existsSync(filepath)) {
if (fs.statSync(filepath).isDirectory()) {
const files = fs.readdirSync(filepath);
files.forEach((file, index) => {
const curPath = path.join(filepath, file);
if (fs.statSync(curPath).isDirectory()) {
exports.deleteFile(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(filepath);
} else {
fs.unlinkSync(filepath);
}
}
}

/* istanbul ignore next */
exports.open = filepath => {
const folder = filepath;
switch (os.platform()) {
case 'win32':
shell.exec(`explorer ${folder}`);
break;
case 'darwin':
shell.exec(`open ${folder} -a finder`);
break;
case 'linux':
shell.exec(`nautilus ${folder}`);
break;
default:
opn(folder);
break;
}
};

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

exports.checkPortUsed = port => {
let cmd = '';
switch (os.platform()) {
case 'win32':
cmd = `netstat -ano | findstr ${port}`;
break;
case 'darwin':
cmd = `netstat -anp tcp | grep ${port}`;
break;
case 'linux':
cmd = `netstat -apn | grep ${port}`;
break;
default:
cmd = `netstat -apn | grep ${port}`;
break;
}
try {
const result = shell.exec(cmd, { silent: true });
return !!result.stdout;
} catch (err) {
return false;
}
};

exports.getPort = (port, count = 10) => {
let newPort = port;
let checkPort = port;
while(checkPort < port + count) {
const isUsed = exports.checkPortUsed(checkPort);
if (!isUsed) {
newPort = checkPort;
break;
}
checkPort++;
}
return newPort;
};

/* istanbul ignore next */
exports.kill = function (port) {
if (port) {
port = String(port);
const ports = port.split(',');
ports.forEach(p => {
killer.kill(p).then(() => {
console.log(`kill port ${p} success`);
}).catch(() => {
console.log(`kill port ${p} failed`);
});
});
}
};

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}`;
};
61 changes: 61 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "node-tool-utils",
"version": "0.1.0",
"description": "node normal tool and utils",
"keywords": [
"npm",
"npm package",
"unit test",
"eslint",
"travis",
"travis-ci",
"code coverage",
"changelog"
],
"dependencies": {
"cross-port-killer": "^1.0.1",
"opn": "^5.4.0",
"shelljs": "^0.8.2"
},
"devDependencies": {
"chai": "^4.1.1",
"codecov": "^3.0.0",
"conventional-changelog-cli": "^1.3.5",
"cross-env": "^5.0.5",
"eslint": "^4.5.0",
"eslint-config-eslint": "^4.0.0",
"mocha": "^3.5.0",
"nyc": "^11.1.0"
},
"engines": {
"node": ">=6.0.0"
},
"scripts": {
"lint": "eslint .",
"fix": "eslint --fix .",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"test": "nyc --reporter=html --reporter=text mocha --timeout=1000000",
"cov": "nyc report --reporter=lcov && codecov --token=e971d1e7-39e2-44b1-b1ee-f4345b0adbee",
"ci": "npm run lint && npm run cov",
"ii": "npm install --registry https://registry.npm.taobao.org"
},
"nyc": {
"exclude": [
"**/*.spec.js",
"test/*.test.js"
]
},
"ci": {
"version": "6, 8, 9"
},
"repository": {
"type": "git",
"url": "git+https://github.com/hubcarl/node-tool-utils.git"
},
"bugs": {
"url": "https://github.com/hubcarl/node-tool-utils/issues"
},
"homepage": "https://github.com/hubcarl/node-tool-utils#readme",
"author": "hubcarl@126.com",
"license": "MIT"
}
3 changes: 3 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const tool = require('../lib/tool');
const port = tool.getPort(9000);
tool.kill(7001);
30 changes: 30 additions & 0 deletions test/lib.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';
const path = require('path');
const expect = require('chai').expect;
// http://chaijs.com/api/bdd/
const lib = require('../lib');
describe('lib.test.js', () => {
before(() => {
});

after(() => {
});

beforeEach(() => {
});

afterEach(() => {
});

describe('#expect lib test', () => {
it('should unit api test', () => {
expect(true).to.be.true;
expect(false).to.be.false;
expect(undefined).to.be.undefined;
expect([1,2,3]).to.have.property(1);
expect(['.js','.jsx','.vue']).to.include.members(['.js','.jsx']);
expect({id: 1, name: 'sky'}).to.include.all.keys(['id', 'name']);
expect(lib).to.have.property('test');
});
});
});

0 comments on commit 23c6c60

Please sign in to comment.