diff --git a/doric-cli/package-lock.json b/doric-cli/package-lock.json index 86c2dce5..0e0ddefb 100644 --- a/doric-cli/package-lock.json +++ b/doric-cli/package-lock.json @@ -178,6 +178,11 @@ "brace-expansion": "^1.1.7" } }, + "nodejs-websocket": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/nodejs-websocket/-/nodejs-websocket-1.7.2.tgz", + "integrity": "sha512-PFX6ypJcCNDs7obRellR0DGTebfUhw1SXGKe2zpB+Ng1DQJhdzbzx1ob+AvJCLzy2TJF4r8cCDqMQqei1CZdPQ==" + }, "normalize-path": { "version": "3.0.0", "resolved": "http://registry.npm.taobao.org/normalize-path/download/normalize-path-3.0.0.tgz", diff --git a/doric-cli/package.json b/doric-cli/package.json index 3b38558d..a1fffbcf 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", + "nodejs-websocket": "^1.7.2", "rollup": "^1.18.0", "shelljs": "^0.8.3", "typescript": "^3.5.3" diff --git a/doric-cli/scripts/server.js b/doric-cli/scripts/server.js new file mode 100644 index 00000000..104c7502 --- /dev/null +++ b/doric-cli/scripts/server.js @@ -0,0 +1,21 @@ +const ws = require('nodejs-websocket') + +const createServer = () => { + let server = ws.createServer(connection => { + connection.on('text', function (result) { + console.log('发送消息', result) + }) + connection.on('connect', function (code) { + console.log('开启连接', code) + }) + connection.on('close', function (code) { + console.log('关闭连接', code) + }) + connection.on('error', function (code) { + console.log('异常关闭', code) + }) + }) + return server +} + +module.exports = createServer() \ No newline at end of file diff --git a/doric-cli/scripts/watcher.js b/doric-cli/scripts/watcher.js index fa815e10..4b9df594 100644 --- a/doric-cli/scripts/watcher.js +++ b/doric-cli/scripts/watcher.js @@ -1,10 +1,22 @@ -var chokidar = require('chokidar') +const chokidar = require('chokidar') +const ws = require('./server') +const fs = require("fs") + require('shelljs/global') exec('npm run dev', () => { }) -chokidar.watch(process.cwd() + "/build", { +ws.listen(7777) +chokidar.watch(process.cwd() + "/bundle", { ignored: /(^|[\/\\])\../, }).on('all', (event, path) => { - console.log(event, path) + console.log('path is ', path) + if (event === 'add' || event === 'change') { + fs.readFile(path, 'utf-8', (error, data) => { + console.log('send data ', data) + ws.connections.forEach(e => { + e.sendText(data) + }) + }) + } });