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