@yongdall/live-server 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/live-server.mjs +60 -0
- package/cli/live-server.mjs.map +1 -0
- package/cli/yongdall.yml +4 -0
- package/index.d.mts +6 -0
- package/index.mjs +40 -0
- package/index.mjs.map +1 -0
- package/package.json +21 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import * as path from "node:path";
|
|
2
|
+
import liveServer from "live-server";
|
|
3
|
+
import root from "@yongdall/root";
|
|
4
|
+
import { middleware } from "#index";
|
|
5
|
+
|
|
6
|
+
//#region cli/live-server/cli/live-server.mjs
|
|
7
|
+
/** @import { IncomingMessage, ServerResponse } from 'node:http' */
|
|
8
|
+
/** @import { Cli } from '@yongdall/cli-parse' */
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* @param {Cli.Env} env
|
|
12
|
+
*/
|
|
13
|
+
async function live_server_default({ development, options, parameters }) {
|
|
14
|
+
let server = `http://127.0.0.1:8888`;
|
|
15
|
+
let port = 0;
|
|
16
|
+
for (const s of options.server || []) {
|
|
17
|
+
if (!s) continue;
|
|
18
|
+
let port = 0;
|
|
19
|
+
if (/^\d(_?\d)*$/.test(s)) port = parseInt(s.replace(/_/g, ""));
|
|
20
|
+
else if (/^0x[a-f\d](_?[a-f\d]+)$/i.test(s)) port = parseInt(s.replace(/_/g, "").slice(2), 16);
|
|
21
|
+
if (port > 0 && port <= 65535) {
|
|
22
|
+
server = `http://127.0.0.1:${port}`;
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
if (/^(\[[:.a-f\d]+\]|[\.a-z\d-]+)(:\d+)?$/i.test(s)) {
|
|
26
|
+
server = `http://${s}`;
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
const url = new URL(s);
|
|
31
|
+
if (!["http:", "https:"].includes(url.protocol.toLowerCase())) continue;
|
|
32
|
+
server = url.origin;
|
|
33
|
+
break;
|
|
34
|
+
} catch {}
|
|
35
|
+
}
|
|
36
|
+
for (const s of options.port || []) {
|
|
37
|
+
if (!s) continue;
|
|
38
|
+
let v = 0;
|
|
39
|
+
if (/^\d(_?\d)*$/.test(s)) v = parseInt(s.replace(/_/g, ""));
|
|
40
|
+
else if (/^0x[a-f\d](_?[a-f\d]+)$/i.test(s)) v = parseInt(s.replace(/_/g, "").slice(2), 16);
|
|
41
|
+
if (v > 0 && v <= 65535) {
|
|
42
|
+
port = v;
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
liveServer.start({
|
|
47
|
+
host: process.env.IP,
|
|
48
|
+
port,
|
|
49
|
+
open: true,
|
|
50
|
+
mount: [["/assets", path.resolve(root, "assets")], ["/", path.resolve(root, "public")]],
|
|
51
|
+
proxy: ["/api/", "/fileCabinets/"].map((path) => [path, `${server}${path}`]),
|
|
52
|
+
middleware: [middleware()],
|
|
53
|
+
logLevel: 2,
|
|
54
|
+
file: "assets/index.html"
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
//#endregion
|
|
59
|
+
export { live_server_default as default };
|
|
60
|
+
//# sourceMappingURL=live-server.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"live-server.mjs","names":[],"sources":["../../../cli/live-server/cli/live-server.mjs"],"sourcesContent":["/** @import { IncomingMessage, ServerResponse } from 'node:http' */\n/** @import { Cli } from '@yongdall/cli-parse' */\nimport * as path from 'node:path';\n// @ts-ignore\nimport liveServer from 'live-server';\nimport root from '@yongdall/root';\nimport {middleware} from '#index';\n\n\n\n/**\n * \n * @param {Cli.Env} env \n */\nexport default async function ({ development, options, parameters }) {\n\tlet server = `http://127.0.0.1:8888`;\n\tlet port = 0;\n\tfor (const s of options.server || []) {\n\t\tif (!s) { continue; }\n\t\tlet port = 0;\n\t\tif (/^\\d(_?\\d)*$/.test(s)) {\n\t\t\tport = parseInt(s.replace(/_/g, ''));\n\t\t} else if (/^0x[a-f\\d](_?[a-f\\d]+)$/i.test(s)) {\n\t\t\tport = parseInt(s.replace(/_/g, '').slice(2), 16);\n\t\t}\n\t\tif (port > 0 && port <= 0xffff) {\n\t\t\tserver = `http://127.0.0.1:${port}`;\n\t\t\tbreak;\n\t\t}\n\t\tif (/^(\\[[:.a-f\\d]+\\]|[\\.a-z\\d-]+)(:\\d+)?$/i.test(s)) {\n\t\t\tserver = `http://${s}`;\n\t\t\tbreak;\n\t\t}\n\t\ttry {\n\t\t\tconst url = new URL(s);\n\t\t\tif (!['http:', 'https:'].includes(url.protocol.toLowerCase())) { continue; }\n\t\t\tserver = url.origin;\n\t\t\tbreak;\n\t\t} catch {}\n\t}\n\tfor (const s of options.port || []) {\n\t\tif (!s) { continue; }\n\t\tlet v = 0;\n\t\tif (/^\\d(_?\\d)*$/.test(s)) {\n\t\t\tv = parseInt(s.replace(/_/g, ''));\n\t\t} else if (/^0x[a-f\\d](_?[a-f\\d]+)$/i.test(s)) {\n\t\t\tv = parseInt(s.replace(/_/g, '').slice(2), 16);\n\t\t}\n\t\tif (v > 0 && v <= 0xffff) {\n\t\t\tport = v;\n\t\t\tbreak;\n\t\t}\n\t}\n\tliveServer.start({\n\t\thost: process.env.IP,\n\t\tport,\n\t\topen: true,\n\t\tmount: [['/assets', path.resolve(root, 'assets')], ['/', path.resolve(root, 'public')]],\n\t\t// @ts-ignore\n\t\tproxy: ['/api/', '/fileCabinets/'].map(path => [path, `${server}${path}`]),\n\t\tmiddleware: [middleware()],\n\t\tlogLevel: 2,\n\t\tfile: 'assets/index.html',\n\t});\n\n}\n"],"mappings":";;;;;;;;;;;;AAcA,mCAA+B,EAAE,aAAa,SAAS,cAAc;CACpE,IAAI,SAAS;CACb,IAAI,OAAO;AACX,MAAK,MAAM,KAAK,QAAQ,UAAU,EAAE,EAAE;AACrC,MAAI,CAAC,EAAK;EACV,IAAI,OAAO;AACX,MAAI,cAAc,KAAK,EAAE,CACxB,QAA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import * as fs from "node:fs/promises";
|
|
4
|
+
import * as amaro from "amaro";
|
|
5
|
+
|
|
6
|
+
//#region cli/live-server/index.mjs
|
|
7
|
+
/** @import { IncomingMessage, ServerResponse } from 'node:http' */
|
|
8
|
+
function middleware() {
|
|
9
|
+
/**
|
|
10
|
+
* 一个用于 live-server 的中间件,用于处理 .ts 和 .mts 文件
|
|
11
|
+
* @param {IncomingMessage} req
|
|
12
|
+
* @param {ServerResponse} res
|
|
13
|
+
* @param {() => void} next
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
async function middleware(req, res, next) {
|
|
17
|
+
if (req.method !== "GET") return next();
|
|
18
|
+
const url = req.url?.split("#", 1)[0].split("?", 1)[0];
|
|
19
|
+
if (!url) return next();
|
|
20
|
+
if (!url.endsWith(".ts") && !url.endsWith(".mts")) return next();
|
|
21
|
+
const filePath = path.join(root, url.startsWith("/assets/") ? "" : "public", url);
|
|
22
|
+
const rawCode = await fs.readFile(filePath, "utf-8").catch(() => null);
|
|
23
|
+
if (rawCode === null) return next();
|
|
24
|
+
try {
|
|
25
|
+
const { code } = amaro.transformSync(rawCode, { mode: "strip-only" });
|
|
26
|
+
res.setHeader("Content-Type", "application/javascript");
|
|
27
|
+
res.end(code);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
console.error(`Error processing ${url}:`, error);
|
|
30
|
+
res.statusCode = 500;
|
|
31
|
+
res.setHeader("Content-Type", "text/plain");
|
|
32
|
+
res.end(`Error processing TypeScript file: ${error.message}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return middleware;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
export { middleware };
|
|
40
|
+
//# sourceMappingURL=index.mjs.map
|
package/index.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../cli/live-server/index.mjs"],"sourcesContent":["/** @import { IncomingMessage, ServerResponse } from 'node:http' */\nimport * as path from 'node:path';\nimport * as fs from 'node:fs/promises';\nimport * as amaro from 'amaro';\nimport root from '@yongdall/root';\n\nexport function middleware() {\n\n\t/**\n\t * 一个用于 live-server 的中间件,用于处理 .ts 和 .mts 文件 \n\t * @param {IncomingMessage} req \n\t * @param {ServerResponse} res \n\t * @param {() => void} next \n\t * @returns \n\t */\n\tasync function middleware(req, res, next) {\n\t\tif (req.method !== 'GET') { return next(); }\n\n\t\tconst url = req.url?.split('#', 1)[0].split('?', 1)[0];\n\t\tif (!url) { return next(); }\n\n\t\tif (!url.endsWith('.ts') && !url.endsWith('.mts')) { return next(); }\n\n\n\t\tconst filePath = path.join(root, url.startsWith('/assets/') ? '' : 'public', url);\n\n\t\tconst rawCode = await fs.readFile(filePath, 'utf-8').catch(() => null);\n\t\tif (rawCode === null) { return next(); }\n\t\ttry {\n\t\t\tconst { code } = amaro.transformSync(rawCode, { mode: 'strip-only' });\n\t\t\tres.setHeader('Content-Type', 'application/javascript');\n\n\t\t\tres.end(code);\n\t\t} catch (error) {\n\t\t\tconsole.error(`Error processing ${url}:`, error);\n\t\t\tres.statusCode = 500;\n\t\t\tres.setHeader('Content-Type', 'text/plain');\n\t\t\t// @ts-ignore\n\t\t\tres.end(`Error processing TypeScript file: ${error.message}`);\n\t\t}\n\t}\n\treturn middleware;\n}\n"],"mappings":";;;;;;;AAMA,SAAgB,aAAa;;;;;;;;CAS5B,eAAe,WAAW,KAAK,KAAK,MAAM;AACzC,MAAI,IAAI,WAAW,MAAS,QAAO,MAAM;EAEzC,MAAM,MAAM,IAAI,KAAK,MAAM,KAAK,EAAE,CAAC,GAAG,MAAM,KAAK,EAAE,CAAC;AACpD,MAAI,CAAC,IAAO,QAAO,MAAM;AAEzB,MAAI,CAAC,IAAI,SAAS,MAAM,IAAI,CAAC,IAAI,SAAS,OAAO,CAAI,QAAO,MAAM;EAGlE,MAAM,WAAW,KAAK,KAAK,MAAM,IAAI,WAAW,WAAW,GAAG,KAAK,UAAU,IAAI;EAEjF,MAAM,UAAU,MAAM,GAAG,SAAS,UAAU,QAAQ,CAAC,YAAY,KAAK;AACtE,MAAI,YAAY,KAAQ,QAAO,MAAM;AACrC,MAAI;GACH,MAAM,EAAE,SAAS,MAAM,cAAc,SAAS,EAAE,MAAM,cAAc,CAAC;AACrE,OAAI,UAAU,gBAAgB,yBAAyB;AAEvD,OAAI,IAAI,KAAK;WACL,OAAO;AACf,WAAQ,MAAM,oBAAoB,IAAI,IAAI,MAAM;AAChD,OAAI,aAAa;AACjB,OAAI,UAAU,gBAAgB,aAAa;AAE3C,OAAI,IAAI,qCAAqC,MAAM,UAAU;;;AAG/D,QAAO"}
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yongdall/live-server",
|
|
3
|
+
"version": "0.5.4",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./index.mjs",
|
|
6
|
+
"imports": {
|
|
7
|
+
"#index": "./index.mjs"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@yongdall/root": "^0.5.0",
|
|
11
|
+
"amaro": "^1.1.8",
|
|
12
|
+
"live-server": "^1.2.2"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@types/live-server": "^1.2.3",
|
|
16
|
+
"@yongdall/cli-parse": "^0.5.0"
|
|
17
|
+
},
|
|
18
|
+
"exports": {
|
|
19
|
+
".": "./index.mjs"
|
|
20
|
+
}
|
|
21
|
+
}
|