From 99b843b33b3c54897209a687a55c2dd3aec39dbc Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Tue, 13 Aug 2019 17:39:07 +0800 Subject: [PATCH] doric-cli --- doric-cli/package-lock.json | 5 +++++ doric-cli/package.json | 1 + doric-cli/scripts/server.js | 9 +++++---- doric-cli/scripts/watcher.js | 22 +++++++++++++++++++--- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/doric-cli/package-lock.json b/doric-cli/package-lock.json index 9bfe5d0b..aa300a7f 100644 --- a/doric-cli/package-lock.json +++ b/doric-cli/package-lock.json @@ -170,6 +170,11 @@ "resolved": "http://registry.npm.taobao.org/is-number/download/is-number-7.0.0.tgz", "integrity": "sha1-dTU0W4lnNNX4DE0GxQlVUnoU8Ss=" }, + "keypress": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/keypress/-/keypress-0.2.1.tgz", + "integrity": "sha1-HoBFQlABjbrUw/6USX1uZ7YmnHc=" + }, "minimatch": { "version": "3.0.4", "resolved": "http://registry.npm.taobao.org/minimatch/download/minimatch-3.0.4.tgz", diff --git a/doric-cli/package.json b/doric-cli/package.json index b852d370..0491a532 100644 --- a/doric-cli/package.json +++ b/doric-cli/package.json @@ -23,6 +23,7 @@ "child_process": "^1.0.2", "chokidar": "^3.0.2", "commander": "^2.20.0", + "keypress": "^0.2.1", "nodejs-websocket": "^1.7.2", "qrcode-terminal": "^0.12.0", "rollup": "^1.18.0", diff --git a/doric-cli/scripts/server.js b/doric-cli/scripts/server.js index 104c7502..da1db2d7 100644 --- a/doric-cli/scripts/server.js +++ b/doric-cli/scripts/server.js @@ -2,17 +2,18 @@ const ws = require('nodejs-websocket') const createServer = () => { let server = ws.createServer(connection => { + console.log('connected', connection.key) connection.on('text', function (result) { - console.log('发送消息', result) + console.log('text', result) }) connection.on('connect', function (code) { - console.log('开启连接', code) + console.log('connect', code) }) connection.on('close', function (code) { - console.log('关闭连接', code) + console.log('close', code) }) connection.on('error', function (code) { - console.log('异常关闭', code) + console.log('error', code) }) }) return server diff --git a/doric-cli/scripts/watcher.js b/doric-cli/scripts/watcher.js index 041dcda7..bd303ef4 100644 --- a/doric-cli/scripts/watcher.js +++ b/doric-cli/scripts/watcher.js @@ -12,9 +12,8 @@ setTimeout(() => { chokidar.watch(process.cwd() + "/bundle", { ignored: /(^|[\/\\])\../, }).on('change', (path) => { - console.log('path is ', path) fs.readFile(path, 'utf-8', (error, data) => { - console.log('send data ', data) + console.log('File change:', path) ws.connections.forEach(e => { e.sendText(JSON.stringify({ script: data, @@ -44,8 +43,25 @@ function getIPAdress() { const qrcode = require('qrcode-terminal'); const ips = getIPAdress() -console.log(`本机IP是${ips}`) ips.forEach(e => { console.log(`IP:${e}`) qrcode.generate(e, { small: true }); }) + +const keypress = require('keypress'); + +keypress(process.stdin); +process.stdin.on('keypress', function (ch, key) { + if (key && key.ctrl && key.name == 'r') { + ips.forEach(e => { + console.log(`IP:${e}`) + qrcode.generate(e, { small: true }); + }) + } + if (key && key.ctrl && key.name == 'c') { + process.stdin.pause(); + process.exit(0); + } +}); +process.stdin.setRawMode(true); +process.stdin.resume(); \ No newline at end of file