@longzai-intelligence-git/gitignore-formatter 0.0.1 → 0.1.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/dist/index.d.ts CHANGED
@@ -121,8 +121,8 @@ declare function normalizeGitignore(content: string, dedupe?: boolean): string;
121
121
  * 流程:
122
122
  * 1. 归一化现有内容 → 现有模式集合 P
123
123
  * 2. 解析启用的 preset → 期望模式集合 S + preset 分组定义
124
- * 3. inject:S 中不在 P 的模式 → 补充进对应 preset 分组(只增不删)
125
- * 4. 合并 P ∪ S,按"preset 分组 → 自定义 groups → misc"顺序编排输出
124
+ * 3. inject(默认仅根文件,见 options.injectMode):S 中不在 P 的模式 → 补充进对应 preset 分组(只增不删)
125
+ * 4. 合并 P ∪(inject 时 S),按"preset 分组 → 自定义 groups → misc"顺序编排输出
126
126
  *
127
127
  * 输出约定:
128
128
  * - 分组头自动补 `# ` 前缀(comment 为纯文本,不含 #)
@@ -154,6 +154,22 @@ declare function formatGroupHeader(comment: string): string;
154
154
  * @returns 有序的 preset 分组定义数组
155
155
  */
156
156
  declare function resolveEnabledPresets(presets: Record<string, boolean> | undefined): GitignoreGroup[];
157
+ /**
158
+ * 单文件格式化上下文。
159
+ *
160
+ * 由 CLI 根据文件位置填入:与 `lzi-git.config.ts` 同目录的 `.gitignore` 为「根文件」
161
+ * (`isRoot: true`),其余子目录 `.gitignore` 为非根文件(`isRoot: false`)。
162
+ * 直接调用格式化器不传时按根文件处理,以保持单文件格式化的向后兼容。
163
+ */
164
+ interface GitignoreFileContext {
165
+ /**
166
+ * 当前文件是否为配置根 `.gitignore`。
167
+ *
168
+ * 仅在 `options.injectMode === 'root-only'` 时影响行为:根文件注入缺失模式,
169
+ * 非根文件只做分类归组不注入。
170
+ */
171
+ readonly isRoot?: boolean;
172
+ }
157
173
  /**
158
174
  * .gitignore 格式化入口。
159
175
  *
@@ -162,8 +178,9 @@ declare function resolveEnabledPresets(presets: Record<string, boolean> | undefi
162
178
  *
163
179
  * @param content - 原始 .gitignore 内容
164
180
  * @param config - lzi-git.config.ts 解析得到的配置(可为 undefined,表示无配置文件)
181
+ * @param fileContext - 单文件上下文(isRoot 标识是否配置根文件,影响 root-only 注入范围)
165
182
  * @returns 格式化后的内容(以单个 \n 结尾)
166
183
  */
167
- declare function formatGitignore(content: string, config?: LziGitConfig): string;
184
+ declare function formatGitignore(content: string, config?: LziGitConfig, fileContext?: GitignoreFileContext): string;
168
185
  //#endregion
169
- export { classifyEquivalent, classifyPattern, formatGitignore, formatGroupHeader, getNegationBody, isBlankLine, isCommentLine, isNegationPattern, normalizeGitignore, resolveEnabledPresets, resolveOptions, stripTrailingWhitespace };
186
+ export { GitignoreFileContext, classifyEquivalent, classifyPattern, formatGitignore, formatGroupHeader, getNegationBody, isBlankLine, isCommentLine, isNegationPattern, normalizeGitignore, resolveEnabledPresets, resolveOptions, stripTrailingWhitespace };
package/dist/index.js CHANGED
@@ -3,10 +3,10 @@ import{DEFAULT_GITIGNORE_OPTIONS as e,GITIGNORE_PRESETS as t}from"@longzai-intel
3
3
  `).split(`
4
4
  `).map(e=>c(e))}function u(e,t){if(!t)return e;let i=new Set,a=[];for(let t of e){if(n(t)||r(t)){a.push(t);continue}i.has(t)||(i.add(t),a.push(t))}return a}function d(e){let t=[];for(let r of e){if(n(r)){t.length>0&&!n(t[t.length-1]??``)&&t.push(``);continue}t.push(r)}for(;t.length>0&&n(t[t.length-1]??``);)t.pop();return t}function f(e,t=!0){let n=d(u(l(e),t));return n.length===0?``:`${n.join(`
5
5
  `)}\n`}function p(t){return{...e,...t}}function m(e,t){return e<t?-1:+(e>t)}function h(e){let t=f(e,!0);return t===``?[]:t.split(`
6
- `).filter(e=>!n(e)&&!r(e))}function g(e){let t=e.trim();return t.startsWith(`#`)?t:`# ${t}`}function _(e){if(e===void 0)return[];let n=Object.keys(t),r=[];for(let i of n){let n=t[i];n!==void 0&&(e[i]??n.default)&&r.push({comment:n.comment,patterns:[...n.patterns]})}return r}function v(e,t){return t===void 0||t.length===0?e:[...e,...t]}function y(e,t,n){let r=`
7
- `.repeat(Math.max(0,n.blankLinesBetweenGroups)+1),i=[],a=new Set;for(let r of t){if(r===void 0)continue;let t=[];for(let n of e)a.has(n)||r.patterns.some(e=>o(n,e))&&(t.push(n),a.add(n));for(let n of r.patterns)e.some(e=>o(e,n))||t.push(n);n.sortWithinGroup&&t.sort(m);let s=g(r.comment);i.push(t.length>0?[s,...t].join(`
8
- `):s)}let s=e.filter(e=>!a.has(e));return s.length>0&&(n.sortWithinGroup&&s.sort(m),i.push([g(n.miscGroupComment),...s].join(`
9
- `))),i.length===0?``:`${i.join(r)}\n`}function b(e,t){let i=f(e,t.dedupe);if(i===``)return``;let a=i.split(`
6
+ `).filter(e=>!n(e)&&!r(e))}function g(e){let t=e.trim();return t.startsWith(`#`)?t:`# ${t}`}function _(e){if(e===void 0)return[];let n=Object.keys(t),r=[];for(let i of n){let n=t[i];n!==void 0&&(e[i]??n.default)&&r.push({comment:n.comment,patterns:[...n.patterns]})}return r}function v(e,t){return t===void 0||t.length===0?e:[...e,...t]}function y(e,t,n,r){let i=`
7
+ `.repeat(Math.max(0,n.blankLinesBetweenGroups)+1),a=[],s=new Set;for(let i of t){if(i===void 0)continue;let t=[];for(let n of e)s.has(n)||i.patterns.some(e=>o(n,e))&&(t.push(n),s.add(n));if(r)for(let n of i.patterns)e.some(e=>o(e,n))||t.push(n);if(t.length===0){r&&a.push(g(i.comment));continue}n.sortWithinGroup&&t.sort(m),a.push([g(i.comment),...t].join(`
8
+ `))}let c=e.filter(e=>!s.has(e));return c.length>0&&(n.sortWithinGroup&&c.sort(m),a.push([g(n.miscGroupComment),...c].join(`
9
+ `))),a.length===0?``:`${a.join(i)}\n`}function b(e,t){let i=f(e,t.dedupe);if(i===``)return``;let a=i.split(`
10
10
  `),o=[],s={header:null,patterns:[]},c=!1;for(let e of a)n(e)||(r(e)?(c&&o.push(s),s={header:e,patterns:[]},c=!0):(s.patterns.push(e),c=!0));if(c&&o.push(s),t.sortWithinGroup)for(let e of o)e.patterns.sort(m);let l=`
11
11
  `.repeat(Math.max(0,t.blankLinesBetweenGroups)+1),u=o.map(e=>{let t=[];return e.header!==null&&t.push(e.header),t.push(...e.patterns),t.join(`
12
- `)}).filter(e=>e.length>0);return u.length===0?``:`${u.join(l)}\n`}function x(e,t){let n=t?.gitignore;if(n===void 0)return f(e,!0);let r=p(n.options);return r.groupByConfig?y(h(e),v(_(n.presets),n.groups),r):b(e,r)}export{o as classifyEquivalent,s as classifyPattern,x as formatGitignore,g as formatGroupHeader,a as getNegationBody,n as isBlankLine,r as isCommentLine,i as isNegationPattern,f as normalizeGitignore,_ as resolveEnabledPresets,p as resolveOptions,c as stripTrailingWhitespace};
12
+ `)}).filter(e=>e.length>0);return u.length===0?``:`${u.join(l)}\n`}function x(e,t,n){let r=t?.gitignore;if(r===void 0)return f(e,!0);let i=p(r.options);if(i.groupByConfig){let t=n?.isRoot??!0,a=i.injectMode===`always`||t;return y(h(e),v(_(r.presets),r.groups),i,a)}return b(e,i)}export{o as classifyEquivalent,s as classifyPattern,x as formatGitignore,g as formatGroupHeader,a as getNegationBody,n as isBlankLine,r as isCommentLine,i as isNegationPattern,f as normalizeGitignore,_ as resolveEnabledPresets,p as resolveOptions,c as stripTrailingWhitespace};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@longzai-intelligence-git/gitignore-formatter",
3
- "version": "0.0.1",
3
+ "version": "0.1.1",
4
4
  "description": "Longzai Intelligence Git - .gitignore 格式化业务逻辑",
5
5
  "files": [
6
6
  "dist"
@@ -30,7 +30,7 @@
30
30
  "clean": "lzi-dev-cli clean"
31
31
  },
32
32
  "dependencies": {
33
- "@longzai-intelligence-git/config": "0.0.1"
33
+ "@longzai-intelligence-git/config": "0.1.1"
34
34
  },
35
35
  "devDependencies": {},
36
36
  "peerDependencies": {}