@longzai-intelligence-css/gate 0.0.0-stage → 0.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 +21 -2
- package/dist/index.d.ts +197 -0
- package/dist/index.js +6 -0
- package/package.json +35 -4
package/README.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @longzai-intelligence-css/gate
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Longzai Intelligence CSS 边界门禁引擎核:`lzi-css` CLI 的引擎包。
|
|
4
|
+
|
|
5
|
+
## 简介
|
|
6
|
+
|
|
7
|
+
扫描仓内 `.css` 与 `.md` 文件,抽取 `@import`、`@reference`、`@source` 与 `source()`
|
|
8
|
+
的路径基元,以「自文件向上定位最近 package.json」确定所在包根,相对路径解析后
|
|
9
|
+
越出包根即违例,裸包名与协议地址合法。文件处理阶段以显式常量上限的有界并发池执行,
|
|
10
|
+
在飞峰值随结果导出供测试守护。
|
|
11
|
+
|
|
12
|
+
## 能力
|
|
13
|
+
|
|
14
|
+
- 指令抽取:块注释剔除保持行结构,at-rule 与 `source()` 函数两类形态词法扫描
|
|
15
|
+
- 文档同适用:markdown 的 css 围栏代码块与行内代码段参与扫描,散文正文不参与
|
|
16
|
+
- 包边界判定:相对路径越出包根即违例,无论指向 `src` 还是 `dist`
|
|
17
|
+
- 门禁友好:text 与 json 双格式报告,违例退出码可直接接入质量门禁
|
|
18
|
+
|
|
19
|
+
## 双通道消费
|
|
20
|
+
|
|
21
|
+
- 可编程 API:`check(options)` 返回违例列表与扫描汇总
|
|
22
|
+
- CLI 命令:`lzi-css check`(引擎经 bin 封装)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CSS 边界门禁引擎内部类型定义
|
|
3
|
+
*
|
|
4
|
+
* 这些类型为引擎实现与消费方编排所用,配置契约由 `@longzai-intelligence-css/config` 提供
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* CSS 路径类指令基元的类别
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* `source` 为 `@source` at-rule 声明,`source-fn` 为独立 `source()` 函数调用,
|
|
11
|
+
* 两者在 CSS 语法上分属声明与函数,故分列判定类别
|
|
12
|
+
*/
|
|
13
|
+
type PathDirectiveKind = 'import' | 'reference' | 'source' | 'source-fn';
|
|
14
|
+
/**
|
|
15
|
+
* 提取出的单个路径类指令基元
|
|
16
|
+
*/
|
|
17
|
+
type PathDirective = {
|
|
18
|
+
/**
|
|
19
|
+
* 指令类别
|
|
20
|
+
*/
|
|
21
|
+
kind: PathDirectiveKind;
|
|
22
|
+
/**
|
|
23
|
+
* 路径参数原文(未解析,保留引号内内容与 glob 通配符)
|
|
24
|
+
*/
|
|
25
|
+
value: string;
|
|
26
|
+
/**
|
|
27
|
+
* 指令所在文本内的行号(1 起算,markdown 段已含段基线偏移)
|
|
28
|
+
*/
|
|
29
|
+
line: number;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* CSS 边界门禁违例
|
|
33
|
+
*/
|
|
34
|
+
type Violation = {
|
|
35
|
+
/**
|
|
36
|
+
* 违例文件相对扫描根的路径(posix 风格分隔)
|
|
37
|
+
*/
|
|
38
|
+
file: string;
|
|
39
|
+
/**
|
|
40
|
+
* 指令所在行号(1 起算)
|
|
41
|
+
*/
|
|
42
|
+
line: number;
|
|
43
|
+
/**
|
|
44
|
+
* 指令类别
|
|
45
|
+
*/
|
|
46
|
+
kind: PathDirectiveKind;
|
|
47
|
+
/**
|
|
48
|
+
* 路径参数原文
|
|
49
|
+
*/
|
|
50
|
+
value: string;
|
|
51
|
+
/**
|
|
52
|
+
* 违例说明
|
|
53
|
+
*/
|
|
54
|
+
message: string;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* 扫描结果汇总
|
|
58
|
+
*/
|
|
59
|
+
type ScanSummary = {
|
|
60
|
+
/**
|
|
61
|
+
* 扫描的文件总数(css 与 md 合计)
|
|
62
|
+
*/
|
|
63
|
+
scannedFiles: number;
|
|
64
|
+
/**
|
|
65
|
+
* 提取的路径类指令基元总数
|
|
66
|
+
*/
|
|
67
|
+
directives: number;
|
|
68
|
+
/**
|
|
69
|
+
* 文件处理阶段的在飞并发峰值
|
|
70
|
+
*/
|
|
71
|
+
peakConcurrency: number;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* 扫描顶层结果
|
|
75
|
+
*/
|
|
76
|
+
type ScanResult = {
|
|
77
|
+
/**
|
|
78
|
+
* 全部违例(按文件扫描顺序)
|
|
79
|
+
*/
|
|
80
|
+
violations: Violation[];
|
|
81
|
+
/**
|
|
82
|
+
* 扫描汇总
|
|
83
|
+
*/
|
|
84
|
+
summary: ScanSummary;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* 爬取所需的文件系统接口(可注入,便于测试)
|
|
88
|
+
*/
|
|
89
|
+
type CrawlFs = {
|
|
90
|
+
/**
|
|
91
|
+
* 读取目录直接子条目
|
|
92
|
+
*
|
|
93
|
+
* @param dirPath - 目录绝对路径
|
|
94
|
+
* @returns 子条目名数组(不含 . 与 ..)
|
|
95
|
+
*/
|
|
96
|
+
readdir: (dirPath: string) => Promise<string[]>;
|
|
97
|
+
/**
|
|
98
|
+
* 判断路径是否为目录
|
|
99
|
+
*
|
|
100
|
+
* @param filePath - 路径绝对路径
|
|
101
|
+
* @returns 是否为目录
|
|
102
|
+
*/
|
|
103
|
+
isDirectory: (filePath: string) => Promise<boolean>;
|
|
104
|
+
/**
|
|
105
|
+
* 读取文件文本内容
|
|
106
|
+
*
|
|
107
|
+
* @param filePath - 文件绝对路径
|
|
108
|
+
* @returns 文件文本内容
|
|
109
|
+
*/
|
|
110
|
+
readFile: (filePath: string) => Promise<string>;
|
|
111
|
+
/**
|
|
112
|
+
* 判断路径是否存在
|
|
113
|
+
*
|
|
114
|
+
* @param filePath - 路径绝对路径
|
|
115
|
+
* @returns 是否存在
|
|
116
|
+
*/
|
|
117
|
+
exists: (filePath: string) => Promise<boolean>;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* check 的入参
|
|
121
|
+
*/
|
|
122
|
+
type CheckOptions = {
|
|
123
|
+
/**
|
|
124
|
+
* 扫描根目录(仓根绝对路径,相对路径与违例定位均相对它)
|
|
125
|
+
*/
|
|
126
|
+
root: string;
|
|
127
|
+
/**
|
|
128
|
+
* 额外排除的相对路径前缀(来自 lzi-css.config.ts 的 css.exclude)
|
|
129
|
+
*/
|
|
130
|
+
exclude?: string[];
|
|
131
|
+
/**
|
|
132
|
+
* 注入的文件系统抽象,省略时用 node fs 组装默认实现
|
|
133
|
+
*/
|
|
134
|
+
fs?: CrawlFs;
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* 将违例列表格式化为 text 报告
|
|
138
|
+
*
|
|
139
|
+
* 格式:
|
|
140
|
+
*
|
|
141
|
+
* ✖ <file>:<line> <kind> '<value>'
|
|
142
|
+
* <message>
|
|
143
|
+
* ...
|
|
144
|
+
* ✔ no css boundary violations found (无违例时)
|
|
145
|
+
* ✖ N violation(s) (有违例时的汇总行)
|
|
146
|
+
*
|
|
147
|
+
* @param violations - 违例列表
|
|
148
|
+
* @param root - 扫描根目录(违例文件定位转相对路径)
|
|
149
|
+
* @returns 报告文本
|
|
150
|
+
*/
|
|
151
|
+
declare const formatText: (violations: Violation[], root: string) => string;
|
|
152
|
+
/**
|
|
153
|
+
* 将违例列表格式化为 json 报告
|
|
154
|
+
*
|
|
155
|
+
* 输出违例对象的 JSON 数组(file 相对扫描根),供机器消费
|
|
156
|
+
*
|
|
157
|
+
* @param violations - 违例列表
|
|
158
|
+
* @param root - 扫描根目录
|
|
159
|
+
* @returns json 报告文本
|
|
160
|
+
*/
|
|
161
|
+
declare const formatJson: (violations: Violation[], root: string) => string;
|
|
162
|
+
/**
|
|
163
|
+
* 文件处理阶段的在飞并发上限
|
|
164
|
+
*
|
|
165
|
+
* 显式常量即负载天花板,峰值由测试断言不超过该值,禁止调大绕过
|
|
166
|
+
*/
|
|
167
|
+
declare const MAX_CONCURRENCY = 8;
|
|
168
|
+
/**
|
|
169
|
+
* 基于 node fs 构造 CrawlFs 实现
|
|
170
|
+
*
|
|
171
|
+
* @returns CrawlFs 实例
|
|
172
|
+
*/
|
|
173
|
+
declare const createNodeCrawlFs: () => CrawlFs;
|
|
174
|
+
/**
|
|
175
|
+
* 执行 CSS 边界门禁扫描
|
|
176
|
+
*
|
|
177
|
+
* 不自行加载配置(配置由 CLI 经两级加载注入)。流程:
|
|
178
|
+
*
|
|
179
|
+
* 1. 从扫描根递归收集 `.css` 与 `.md` 文件(内置排除面 + 配置追加)
|
|
180
|
+
* 2. 有界并发池逐文件处理:读文本、抽指令基元、逐基元包边界判定
|
|
181
|
+
* 3. 汇总违例与扫描统计
|
|
182
|
+
*
|
|
183
|
+
* @param options - 扫描选项(root 扫描根、exclude 追加排除、fs 注入)
|
|
184
|
+
* @returns 扫描结果(违例列表与汇总)
|
|
185
|
+
*/
|
|
186
|
+
declare const check: (options: CheckOptions) => Promise<ScanResult>;
|
|
187
|
+
/**
|
|
188
|
+
* 把违例文件路径转为相对扫描根的 posix 风格路径
|
|
189
|
+
*
|
|
190
|
+
* 供报告层与消费方统一定位口径
|
|
191
|
+
*
|
|
192
|
+
* @param violations - 违例列表
|
|
193
|
+
* @param root - 扫描根目录
|
|
194
|
+
* @returns 相对化后的违例列表
|
|
195
|
+
*/
|
|
196
|
+
declare const relativizeViolations: (violations: Violation[], root: string) => Violation[];
|
|
197
|
+
export { type CheckOptions, type CrawlFs, MAX_CONCURRENCY, type PathDirective, type PathDirectiveKind, type ScanResult, type ScanSummary, type Violation, check, createNodeCrawlFs, formatJson, formatText, relativizeViolations };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import{readFileSync as e,readdirSync as t,realpathSync as n,statSync as r}from"node:fs";import{isAbsolute as i,relative as a,resolve as o,sep as s}from"node:path";const c=8,l=[`node_modules`,`dist`,`.turbo`,`.git`],u=[`.css`,`.md`],d=(e,t)=>e.split(`/`).some(e=>l.includes(e))?!0:t.some(t=>e.startsWith(t)),f=(e,t)=>a(t,e).split(s).join(`/`),p=async(e,t,n)=>{let r=[],i=async a=>{let o=await t.readdir(a);for(let s of o){let o=`${a}/${s}`,c=f(o,e);d(c,n)||(await t.isDirectory(o)?await i(o):u.some(e=>o.endsWith(e))&&r.push(o))}};return await i(e),{files:r}},m=async(e,t,n)=>{let r=0,i=0,a=0,o=async()=>{for(;r<e.length;){let t=e[r];if(t===void 0)break;r+=1,i+=1,a=Math.max(a,i);try{await n(t)}finally{--i}}},s=Array.from({length:Math.min(t,e.length)},()=>o());return await Promise.all(s),a},h=/@(import|reference|source)[ \t]+([^;{}]+)/g,g=/(?:^|[^\w:])source\(([^)]*)\)/g,_=(e,t)=>{let n=``;for(let r=0;r<t;r+=1){let t=e[r];if(n!==``){if(t===`\\`){r+=1;continue}t===n&&(n=``)}else(t===`'`||t===`"`)&&(n=t)}return n!==``},v=e=>{let t=[...e],n=0;for(;n<t.length-1;)if(t[n]===`/`&&t[n+1]===`*`&&!_(e,n))for(n+=2;n<t.length;){if(t[n]===`*`&&t[n+1]===`/`){n+=2;break}t[n]!==`
|
|
2
|
+
`&&(t[n]=` `),n+=1}else n+=1;return t.join(``)},y=e=>{let t=e.trim();if(t===``)return null;let n=/^url\(\s*(['"]?)(.*?)\1\s*\)/.exec(t);if(n!==null){let e=n[2]??``;return e===``?null:e}let r=/^(['"])(.*?)\1/.exec(t);if(r!==null){let e=r[2]??``;return e===``?null:e}let i=/^[^\s;]+/.exec(t);return i===null?null:i[0]},b=(e,t)=>{let n=1;for(let r=0;r<t;r+=1)e[r]===`
|
|
3
|
+
`&&(n+=1);return n},x=e=>{let t=v(e),n=[],r={import:`import`,reference:`reference`,source:`source`};for(let e of t.matchAll(h)){let i=e[1];if(i===void 0)continue;let a=r[i];if(a===void 0)continue;let o=y(e[2]??``);o!==null&&n.push({kind:a,value:o,line:b(t,e.index??0)})}for(let e of t.matchAll(g)){let r=y(e[1]??``);r!==null&&n.push({kind:`source-fn`,value:r,line:b(t,e.index??0)})}return n},S=e=>(e.trim().split(/\s+/)[0]??``).toLowerCase()===`css`,C=e=>{let t=e.split(`
|
|
4
|
+
`),n=[],r=null,i=[],a=0;for(let e=0;e<t.length;e+=1){let o=t[e];if(o===void 0)continue;let s=/^ {0,3}(`{3,}|~{3,})/.exec(o);if(r===null){if(s!==null)r=(s[1]?.[0]??"`").repeat(3),a=e+2,i=[],S(o.slice(s[0].length))||(r=`${r}skip`);else for(let t of o.matchAll(/`([^`\n]+)`/g))n.push({text:t[1]??``,baseLine:e+1})}else s!==null&&s[1]?.[0]===r[0]&&(s[1]?.length??0)>=3?(r.length===3&&n.push({text:i.join(`
|
|
5
|
+
`),baseLine:a}),r=null,i=[]):i.push(o)}return n},w=/^[a-zA-Z][a-zA-Z0-9+.-]*:/,T=e=>e.startsWith(`./`)||e.startsWith(`../`)||e===`.`||e===`..`,E=e=>e===``||w.test(e)?!0:!T(e)&&!i(e),D=async(e,t)=>{let n=e.slice(0,Math.max(0,e.lastIndexOf(s)));for(;;){let e=`${n}${s}package.json`;if(await t.exists(e))return n;let r=n.slice(0,Math.max(0,n.lastIndexOf(s)));if(r===n||r===``)return null;n=r}},O=async(e,t,n)=>{if(E(t.value))return null;let r=await D(e,n);if(r===null)return null;let c=e.slice(0,Math.max(0,e.lastIndexOf(s))),l=o(c,t.value),u=a(r,l).split(s).join(`/`);return u.startsWith(`..`)||i(u)?{file:e,line:t.line,kind:t.kind,value:t.value,message:`相对路径越出所在包根,属跨包物理穿越禁入形态`}:null},k=(e,t)=>{if(e.length===0)return`✔ no css boundary violations found`;let n=[];for(let r of e){let e=a(t,r.file).split(s).join(`/`);n.push(`✖ ${e}:${r.line} ${r.kind} '${r.value}'`),n.push(` ${r.message}`)}return n.push(`✖ ${e.length} violation(s)`),n.join(`
|
|
6
|
+
`)},A=(e,t)=>{let n=e.map(e=>({...e,file:a(t,e.file).split(s).join(`/`)}));return JSON.stringify(n,null,2)},j=()=>({readdir:async e=>t(e),isDirectory:async e=>r(e).isDirectory(),readFile:async t=>e(t,`utf8`),exists:async e=>{try{return n(e),!0}catch{return!1}}}),M=e=>{let t=[];for(let n of C(e))for(let e of x(n.text))t.push({kind:e.kind,value:e.value,line:e.line+n.baseLine-1});return t},N=async e=>{let t=e.fs??j(),{files:n}=await p(e.root,t,e.exclude??[]),r=[],i=0,a=await m(n,8,async e=>{let n=await t.readFile(e),a=e.endsWith(`.md`)?M(n):x(n);i+=a.length;for(let n of a){let i=await O(e,n,t);i!==null&&r.push({...i,file:e})}});return{violations:r,summary:{scannedFiles:n.length,directives:i,peakConcurrency:a}}},P=(e,t)=>e.map(e=>({...e,file:f(e.file,t)}));export{c as MAX_CONCURRENCY,N as check,j as createNodeCrawlFs,A as formatJson,k as formatText,P as relativizeViolations};
|
package/package.json
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@longzai-intelligence-css/gate",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Longzai Intelligence CSS 边界门禁引擎核:指令抽取、包边界判定与违例报告",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"boundary",
|
|
7
|
+
"css",
|
|
8
|
+
"gate",
|
|
9
|
+
"lint",
|
|
10
|
+
"longzai-intelligence"
|
|
11
|
+
],
|
|
12
|
+
"license": "UNLICENSED",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"type": "module",
|
|
17
|
+
"main": "./dist/index.js",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"import": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "lzi-builder",
|
|
27
|
+
"build:prod": "NODE_ENV=production bun run build",
|
|
28
|
+
"prepublishOnly": "bun run build:prod",
|
|
29
|
+
"typecheck": "lzi-typecheck",
|
|
30
|
+
"lint": "lzi-lint lint",
|
|
31
|
+
"lint:fix": "lzi-lint fix",
|
|
32
|
+
"test": "lzi-bun test",
|
|
33
|
+
"test:watch": "lzi-bun test --watch",
|
|
34
|
+
"test:coverage": "lzi-bun test --coverage",
|
|
35
|
+
"clean": "lzi-clean"
|
|
36
|
+
}
|
|
37
|
+
}
|