nv-cli-nonnest-mcr-rplc 1.0.1
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/README.md +18 -0
- package/TEST/cases.js +4 -0
- package/TEST/final.js +2 -0
- package/TEST/macro.js +10 -0
- package/TEST/prepare.js +2 -0
- package/TEST/tst.sh +0 -0
- package/cli.js +50 -0
- package/index.js +84 -0
- package/package.json +25 -0
- package/srv.js +53 -0
package/README.md
ADDED
package/TEST/cases.js
ADDED
package/TEST/final.js
ADDED
package/TEST/macro.js
ADDED
package/TEST/prepare.js
ADDED
package/TEST/tst.sh
ADDED
|
File without changes
|
package/cli.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
ppend_fmt_sary,
|
|
7
|
+
apend_fmt_sary
|
|
8
|
+
} = require("nv-string-basic");
|
|
9
|
+
|
|
10
|
+
const nvison = require("nvison");
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
const creat_argv = require("nv-cli-basic");
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
const handle = require("./index")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
const cfg = {
|
|
20
|
+
alias: {
|
|
21
|
+
'input' :'i',
|
|
22
|
+
'output' :'o',
|
|
23
|
+
'help' :'h',
|
|
24
|
+
},
|
|
25
|
+
description: {
|
|
26
|
+
'input' :'input string ,default stdin',
|
|
27
|
+
'output' :'output string,default stdout',
|
|
28
|
+
//'ngx_user' :'nginx user name, default www-data',
|
|
29
|
+
'help':"usage",
|
|
30
|
+
},
|
|
31
|
+
defaults: {
|
|
32
|
+
'ngx_user':'www-data'
|
|
33
|
+
},
|
|
34
|
+
boolean: ["help"]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
var argv = creat_argv(cfg);
|
|
41
|
+
////
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
if(argv.h) {
|
|
45
|
+
var _usage = argv.usage("nv_cli_nonnest_mcr_rplc");
|
|
46
|
+
console.log(_usage)
|
|
47
|
+
} else {
|
|
48
|
+
var handler = (src)=>handle(src)
|
|
49
|
+
argv.rwstream(handler,argv.i,argv.o);
|
|
50
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
|
|
2
|
+
const _fs = require('fs');
|
|
3
|
+
const _path = require('path');
|
|
4
|
+
const _rplc = require("nv-string-qlike-replace");
|
|
5
|
+
const {parse_query_like} = require("nv-string-qlike-replace");
|
|
6
|
+
|
|
7
|
+
function applyIndent(text, depth, indentSize) {
|
|
8
|
+
if (depth === 0) return text;
|
|
9
|
+
|
|
10
|
+
const indent = ' '.repeat(depth * indentSize);
|
|
11
|
+
const lines = text.split('\n');
|
|
12
|
+
return lines.map(line => indent + line).join('\n');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
module.exports = (src_str, indent = 4)=> {
|
|
17
|
+
//const src_str = _fs.readFileSync(src_fn, 'utf-8');
|
|
18
|
+
//const src_dir = _path.dirname(_path.resolve(src_fn));
|
|
19
|
+
src_str = src_str.trim();
|
|
20
|
+
const CALLBACK = (segment, index, isQuoted) => {
|
|
21
|
+
// 此时按照默认配置 ' " 《》 【】 () 都是引号对
|
|
22
|
+
// 我们忽略 ' 和 "
|
|
23
|
+
// 只置换 【】 《》 () 中的宏标记
|
|
24
|
+
if (!isQuoted) {
|
|
25
|
+
return segment;
|
|
26
|
+
} else {
|
|
27
|
+
if(segment[0] === '"' || segment[0] === "'"){
|
|
28
|
+
return segment;
|
|
29
|
+
} else {
|
|
30
|
+
// 只置换 【】 《》 () 中的宏标记
|
|
31
|
+
// 去掉首尾的引号符号,获取内部内容
|
|
32
|
+
const content = segment.slice(1, -1);
|
|
33
|
+
const result = parse_query_like(content);
|
|
34
|
+
|
|
35
|
+
if (!result) {
|
|
36
|
+
return `/*${segment} : 解析失败,格式错误 */`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const [description, params] = result;
|
|
40
|
+
|
|
41
|
+
// 处理 depth 参数(转换为数字)
|
|
42
|
+
let depth = 0;
|
|
43
|
+
if (params.depth !== undefined && params.depth !== null) {
|
|
44
|
+
const parsed = parseInt(params.depth);
|
|
45
|
+
depth = isNaN(parsed) ? 0 : parsed;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 检查是否有 path 参数
|
|
49
|
+
if (!params.path) {
|
|
50
|
+
return `/*${segment} : 缺少 path 参数 */`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 解析路径(相对于源文件目录)
|
|
54
|
+
const resolvedPath = _path.resolve(params.path);
|
|
55
|
+
|
|
56
|
+
// 检查文件是否存在
|
|
57
|
+
if (!_fs.existsSync(resolvedPath)) {
|
|
58
|
+
return `/*${segment} : 路径 ${resolvedPath} 不存在 */`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// 读取文件内容
|
|
62
|
+
try {
|
|
63
|
+
const fileContent = _fs.readFileSync(resolvedPath, 'utf-8').trim();
|
|
64
|
+
|
|
65
|
+
// 构建结果
|
|
66
|
+
let output = `\n////<${description}>\n${fileContent}\n////</${description}>\n`;
|
|
67
|
+
|
|
68
|
+
// 应用缩进
|
|
69
|
+
output = applyIndent(output, depth, indent);
|
|
70
|
+
|
|
71
|
+
return output;
|
|
72
|
+
} catch (err) {
|
|
73
|
+
return `/*${segment} : 读取文件失败 - ${err.message} */`;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// 使用置换函数处理
|
|
80
|
+
const result = _rplc(src_str, CALLBACK);
|
|
81
|
+
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"dependencies": {
|
|
3
|
+
"nv-cli-basic": "^1.0.18",
|
|
4
|
+
"nv-cli-jfetch": "^1.0.2",
|
|
5
|
+
"nv-cli-user": "1.0.1",
|
|
6
|
+
"nv-facutil-surl": "^1.0.2",
|
|
7
|
+
"nv-file-sync-reader": "^1.0.4",
|
|
8
|
+
"nv-server-simple-stream": "^1.0.36",
|
|
9
|
+
"nv-string-basic": "^1.0.51",
|
|
10
|
+
"nv-string-qlike-replace": "^1.0.1",
|
|
11
|
+
"nvison": "^1.0.28"
|
|
12
|
+
},
|
|
13
|
+
"name": "nv-cli-nonnest-mcr-rplc",
|
|
14
|
+
"description": "",
|
|
15
|
+
"version": "1.0.1",
|
|
16
|
+
"main": "index.js",
|
|
17
|
+
"scripts": {
|
|
18
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
19
|
+
},
|
|
20
|
+
"bin": {
|
|
21
|
+
"nv_cli_nonnest_mcr_rplc": "cli.js"
|
|
22
|
+
},
|
|
23
|
+
"author": "",
|
|
24
|
+
"license": "ISC"
|
|
25
|
+
}
|
package/srv.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const _path = require("path");
|
|
4
|
+
const http = require("http");
|
|
5
|
+
const chown_with_name = require("nv-cli-user").chown_with_name;
|
|
6
|
+
const _surl = require("nv-facutil-surl");
|
|
7
|
+
const S = require("nv-server-simple-stream").http.simple;
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
const USOCK = _path.join(__dirname,"___nv_cli_nonnest_mcr_rplc___");
|
|
11
|
+
const KEY = "mykey";
|
|
12
|
+
const NGX_CFG = `proxy_pass http://unix:___nv_cli_nonnest_mcr_rplc___;`;
|
|
13
|
+
|
|
14
|
+
const creat = async (ngx_uname) => {
|
|
15
|
+
let srv = await S.creat(USOCK,async(req,res)=>{
|
|
16
|
+
let D = await S.extract_req(req);
|
|
17
|
+
let ptrn="";
|
|
18
|
+
if(D.method.toUpperCase() === 'POST') {
|
|
19
|
+
let s = D.body.toString();
|
|
20
|
+
let j = JSON.parse(s);
|
|
21
|
+
ptrn = String(j[KEY])
|
|
22
|
+
} else {
|
|
23
|
+
let url = "nvchenyu://none"+D.path ;
|
|
24
|
+
url = decodeURIComponent(url);
|
|
25
|
+
let urld = _surl.parse(url);
|
|
26
|
+
ptrn = String(urld.q[KEY])
|
|
27
|
+
}
|
|
28
|
+
/////
|
|
29
|
+
{
|
|
30
|
+
if(ptrn) {
|
|
31
|
+
let rslt;
|
|
32
|
+
/*
|
|
33
|
+
rslt = ... <your impl>
|
|
34
|
+
*/
|
|
35
|
+
let r = await S.reply_with_json(res,rslt);
|
|
36
|
+
return(r)
|
|
37
|
+
} else {
|
|
38
|
+
let r = await S.reply_with_json(res,[]);
|
|
39
|
+
return(r)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
chown_with_name(USOCK,ngx_uname);
|
|
44
|
+
return(srv)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
module.exports = {
|
|
49
|
+
KEY,
|
|
50
|
+
USOCK,
|
|
51
|
+
NGX_CFG,
|
|
52
|
+
creat
|
|
53
|
+
}
|