增加配置文件变化自动重启服务
parent
39b6d0ff75
commit
f7d0a106b7
45
index.js
45
index.js
|
@ -5,14 +5,14 @@
|
|||
*/
|
||||
|
||||
const vsc = require('vscode')
|
||||
const { join } = require('path')
|
||||
const { join, basename } = require('path')
|
||||
const { parse } = require('url')
|
||||
const http = require('http')
|
||||
const fs = require('iofs')
|
||||
const { WebSocketServer } = require('ws')
|
||||
|
||||
const std = vsc.window.createOutputChannel('http.server')
|
||||
const decode = decodeURIComponent
|
||||
const statItem = vsc.window.createStatusBarItem(vsc.StatusBarAlignment.Left, 0)
|
||||
|
||||
const MIME_TYPES = {
|
||||
html: 'text/html;charset=utf-8',
|
||||
|
@ -49,6 +49,8 @@ const COMMON_HEADERS = {
|
|||
'X-Powered-By': 'VS Code simple.http'
|
||||
}
|
||||
|
||||
const CONFIG_FILE = '.httpserver'
|
||||
|
||||
MIME_TYPES.htm = MIME_TYPES.html
|
||||
MIME_TYPES.jpeg = MIME_TYPES.jpg
|
||||
MIME_TYPES.tif = MIME_TYPES.tiff
|
||||
|
@ -57,13 +59,9 @@ let root
|
|||
let enabled = false
|
||||
let port = 23333
|
||||
let baseUrl = 'http://127.0.0.1:' + port
|
||||
let server
|
||||
let ws = null
|
||||
|
||||
std.out = function (...args) {
|
||||
std.appendLine('[simple.http]: ' + args.join(' '))
|
||||
// console.log('[simple.http]: ' + args.join(' '))
|
||||
}
|
||||
|
||||
const HMR_SCRIPT = `
|
||||
<script>
|
||||
!(function http_hmr(){
|
||||
|
@ -115,12 +113,12 @@ class WebSocket {
|
|||
|
||||
function createServer() {
|
||||
if (port > 65535) {
|
||||
std.out('端口超出有效范围0-65535, 当前为: ' + port)
|
||||
enabled = false
|
||||
return
|
||||
}
|
||||
std.out(`尝试使用${port}端口...`)
|
||||
const server = http
|
||||
console.log(`尝试使用${port}端口...`)
|
||||
|
||||
server = http
|
||||
.createServer(function (req, res) {
|
||||
let pathname = parse(req.url).pathname.slice(1)
|
||||
let paths = pathname.split('/')
|
||||
|
@ -204,7 +202,7 @@ function createServer() {
|
|||
})
|
||||
.listen(port)
|
||||
.on('error', err => {
|
||||
std.out(`${port}端口被占用~~~`)
|
||||
console.log(`${port}端口被占用~~~`)
|
||||
port++
|
||||
createServer()
|
||||
})
|
||||
|
@ -212,18 +210,28 @@ function createServer() {
|
|||
server.on('listening', _ => {
|
||||
baseUrl = 'http://127.0.0.1:' + port
|
||||
ws = new WebSocket(server)
|
||||
std.out('启动成功, 请访问', baseUrl)
|
||||
statItem.text = `http.server (🟢)`
|
||||
statItem.show()
|
||||
})
|
||||
}
|
||||
|
||||
function __init__() {
|
||||
let folders = vsc.workspace.workspaceFolders
|
||||
|
||||
statItem.hide()
|
||||
|
||||
if (folders && folders.length) {
|
||||
root = folders[0].uri.fsPath
|
||||
}
|
||||
|
||||
if (server) {
|
||||
server.close()
|
||||
server.closeAllConnections()
|
||||
server = null
|
||||
}
|
||||
|
||||
if (root) {
|
||||
let file = join(root, '.httpserver')
|
||||
let file = join(root, CONFIG_FILE)
|
||||
//
|
||||
if (fs.isfile(file)) {
|
||||
let conf = JSON.parse(fs.cat(file).toString())
|
||||
|
@ -248,21 +256,25 @@ function __init__() {
|
|||
|
||||
createServer()
|
||||
} else {
|
||||
std.out('发现配置文件, 但服务为 关闭状态')
|
||||
statItem.text = `http.server (🔴)`
|
||||
statItem.show()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function deactivate() {}
|
||||
export function deactivate() {}
|
||||
|
||||
exports.activate = function (ctx) {
|
||||
export function activate(ctx) {
|
||||
__init__()
|
||||
|
||||
vsc.workspace.onDidSaveTextDocument(doc => {
|
||||
if (enabled) {
|
||||
let file = doc.fileName
|
||||
if (file.startsWith(root)) {
|
||||
if (basename(file) === CONFIG_FILE) {
|
||||
return __init__()
|
||||
}
|
||||
ws.send()
|
||||
}
|
||||
}
|
||||
|
@ -280,4 +292,3 @@ exports.activate = function (ctx) {
|
|||
})
|
||||
ctx.subscriptions.push(cmd)
|
||||
}
|
||||
exports.deactivate = deactivate
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
{
|
||||
"name": "simple-http",
|
||||
"displayName": "simple http",
|
||||
"displayName": "http server",
|
||||
"description": "🔥 简单的http服务器, 方便临时调试html",
|
||||
"version": "1.5.3",
|
||||
"version": "1.6.0",
|
||||
"publisher": "yutent",
|
||||
"author": "Yutent [@yutent]",
|
||||
"icon": "logo.png",
|
||||
|
@ -44,7 +44,7 @@
|
|||
"yutent"
|
||||
],
|
||||
"scripts": {
|
||||
"bundle": "esbuild index.js --bundle --outfile=out.js --external:vscode --format=cjs --platform=node",
|
||||
"bundle": "esbuild index.js --bundle --outfile=out.js --external:vscode --format=cjs --target=es2020 --platform=node",
|
||||
"start": "npm run bundle -- --watch",
|
||||
"pack": "npm run bundle -- --minify"
|
||||
},
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
{
|
||||
"enabled": true
|
||||
}
|
||||
"enabled": true,
|
||||
"port": 9090
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue