diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..5e10b81 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +/node_modules +/npm-debug.log +.DS_Store +.todo.md +.idea/** +.vscode/ +package-lock.json \ No newline at end of file diff --git a/app.js b/app.js new file mode 100644 index 0000000..133efc2 --- /dev/null +++ b/app.js @@ -0,0 +1,46 @@ + +var os = require("os"); +var exec = require('child_process').exec; +var express = require('express'); +var app = express(); + +var progressList = []; + +app.get('/', function(req, res){ + res.sendFile(__dirname + '/index.html') +}) + +app.get('/device',function(req, res){ + res.send({ + cpus: os.cpus(), + memory: os.totalmem(), + process: progressList + }) +}) + +app.listen(8087,function(){ + console.log('Server listen on 8087!') +}) + +const cmd = process.platform == 'win32' ? 'tasklist' : 'ps aux'; + +exec(cmd, function(error, stdout, stderr){ + if(error){ + console.log(`执行的错误:${error}`) + return; + } + stdout.split('\n').filter(function(line) { + let p = line.trim().split(/\s+/), + user = p[0], + pid = p[1], + pmem = p[4]; + + if (Number(pid) > 0) { + progressList.push([user, pid, pmem]); + } + }); + progressList.sort(function(a, b) { + return Number(b[2]) - Number(a[2]); + }); + console.log(`stderr: ${stderr}`); +}) \ No newline at end of file diff --git a/bin/hotloader b/bin/hotloader new file mode 100644 index 0000000..74cb274 --- /dev/null +++ b/bin/hotloader @@ -0,0 +1,47 @@ +var fs = require('fs'); +var path = require("path"); +var spawn = require('child_process').spawn; + +class HotLoader { + constructor(args, extName, launcher) { + this.args = args; + this.extName = extName; + this.launcher = launcher; + this.passedArguments = this.args.slice(2); + } + run(){ + fs.watch( + path.join(process.cwd(), this.passedArguments[0]), + (event, filename) => { + this.restartProcess(); + } + ); + return this.startProgress(); + } + startProgress() { + console.log('文件启动===') + this.process = spawn(this.launcher, this.passedArguments, { + env: process.env + }); + this.process.stdout.on("data", data => { + console.log(data.toString()); + }); + this.process.stderr.on("data", function(data) { + const errmessage = '----error----'+ data.toString(); + console.log(errmessage) + }); + } + restartProcess() { + if (this.process !== null) { + try { + this.process.kill("SIGKILL"); + } catch (error) { + console.log("Exception: " + error.message, "bad"); + } + } + console.log('文件重启====') + this.startProgress(); + } +} + +module.exports = HotLoader; \ No newline at end of file diff --git a/bin/hotnode b/bin/hotnode new file mode 100644 index 0000000..0202f3f --- /dev/null +++ b/bin/hotnode @@ -0,0 +1,21 @@ +#!/usr/bin/env node +var HotLoader, VERSION, args, loader, path; +path = require('path'); +HotLoader = require('./hotloader'); +VERSION = "0.0.1"; +args = process.argv; +if (args.length > 2) { + if (args[2] === "-v" || args[2] === "--version") { + console.log("v" + VERSION); + } else { + loader = new HotLoader(args, 'js', 'node'); + loader.run(); + } +} else { + console.log("\nHotnode - Hot node is watching script for Nodejs\n"); + console.log("Usage:"); + console.log("hotcoffee [options] script.coffee [arguments] "); + console.log("Hotnode arguments:") + console.log(" -v=[--version] View version number of Hotnode") + console.log(" *.js Watch script Updates") +} \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..f778fbb --- /dev/null +++ b/index.html @@ -0,0 +1,18 @@ + + +
+ + + +