tazuna 0.17.0 → 0.19.0

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.
@@ -0,0 +1,610 @@
1
+ /**
2
+ * tazuna init が書く include / exclude を、実際にあるファイルから決める(generic 構成。v0.18.0)。
3
+ *
4
+ * 入力は git が管理しているファイルの一覧(パックと同じ既定の除外を済ませたもの)か、git の外では
5
+ * extraRoots にするディレクトリの中のファイル。拡張子とファイル名で種類を分け、次のようにする:
6
+ *
7
+ * - include に書く: 言語の分かるソース・設定とビルドのファイル・文書(拡張子ごとの `**\/*.<拡張子>`、
8
+ * 拡張子の無い既知の名前は `**\/<名前>`、shebang のある拡張子なしのスクリプトはそのパス)
9
+ * - include に書かない: データ(.csv・ノートブックなど。個人情報を含み得る・大きい)・鍵や認証情報の
10
+ * ファイル(.pem・.npmrc など。中身は伏せるが、ファイルごと出さない)・種類の分からないもの
11
+ * - exclude に書く: include に一致するもののうち、他から取り込んだもの(vendor/ など)・圧縮済み
12
+ * (.min.js など)・生成コード(先頭に「generated」と「DO NOT EDIT」などの印があるもの)・
13
+ * include の拡張子と同じ名前の鍵や認証情報のファイル(credentials.json など)
14
+ *
15
+ * 決めたものは設定ファイルに書くので、利用者に見え、書き換えられる(推測で黙って対象を決めない)。
16
+ * 純粋関数(ファイルシステムに触れない。先頭の読み出しは呼び出し側の関数)。
17
+ */
18
+ import * as path from "node:path";
19
+ import { globToRegExp } from "./globMatcher.js";
20
+ /** 拡張子(小文字・ドットなし) → 言語の表示名 */
21
+ const LANGUAGE_BY_EXTENSION = {
22
+ py: "Python",
23
+ pyi: "Python",
24
+ pyw: "Python",
25
+ js: "JavaScript",
26
+ jsx: "JavaScript",
27
+ mjs: "JavaScript",
28
+ cjs: "JavaScript",
29
+ ts: "TypeScript",
30
+ tsx: "TypeScript",
31
+ mts: "TypeScript",
32
+ cts: "TypeScript",
33
+ java: "Java",
34
+ kt: "Kotlin",
35
+ kts: "Kotlin",
36
+ scala: "Scala",
37
+ groovy: "Groovy",
38
+ cs: "C#",
39
+ csx: "C#",
40
+ vb: "VB.NET",
41
+ fs: "F#",
42
+ fsi: "F#",
43
+ fsx: "F#",
44
+ go: "Go",
45
+ rs: "Rust",
46
+ c: "C",
47
+ h: "C / C++",
48
+ cc: "C++",
49
+ cpp: "C++",
50
+ cxx: "C++",
51
+ hpp: "C++",
52
+ hh: "C++",
53
+ hxx: "C++",
54
+ m: "Objective-C",
55
+ mm: "Objective-C",
56
+ swift: "Swift",
57
+ rb: "Ruby",
58
+ rake: "Ruby",
59
+ gemspec: "Ruby",
60
+ php: "PHP",
61
+ phtml: "PHP",
62
+ pl: "Perl",
63
+ pm: "Perl",
64
+ sh: "シェル",
65
+ bash: "シェル",
66
+ zsh: "シェル",
67
+ ksh: "シェル",
68
+ ps1: "PowerShell",
69
+ psm1: "PowerShell",
70
+ psd1: "PowerShell",
71
+ bat: "バッチ",
72
+ cmd: "バッチ",
73
+ sql: "SQL",
74
+ r: "R",
75
+ dart: "Dart",
76
+ lua: "Lua",
77
+ ex: "Elixir",
78
+ exs: "Elixir",
79
+ erl: "Erlang",
80
+ hrl: "Erlang",
81
+ hs: "Haskell",
82
+ clj: "Clojure",
83
+ cljs: "Clojure",
84
+ cljc: "Clojure",
85
+ jl: "Julia",
86
+ vue: "Vue",
87
+ svelte: "Svelte",
88
+ astro: "Astro",
89
+ html: "HTML",
90
+ htm: "HTML",
91
+ xhtml: "HTML",
92
+ css: "CSS",
93
+ scss: "CSS",
94
+ sass: "CSS",
95
+ less: "CSS",
96
+ styl: "CSS",
97
+ cbl: "COBOL",
98
+ cob: "COBOL",
99
+ cpy: "COBOL",
100
+ pas: "Delphi",
101
+ dpr: "Delphi",
102
+ bas: "VB6 / VBA",
103
+ cls: "VB6 / VBA",
104
+ frm: "VB6 / VBA",
105
+ asp: "ASP",
106
+ aspx: "ASP.NET",
107
+ ascx: "ASP.NET",
108
+ master: "ASP.NET",
109
+ cshtml: "ASP.NET",
110
+ vbhtml: "ASP.NET",
111
+ razor: "ASP.NET",
112
+ jsp: "JSP",
113
+ ejs: "テンプレート",
114
+ hbs: "テンプレート",
115
+ handlebars: "テンプレート",
116
+ mustache: "テンプレート",
117
+ njk: "テンプレート",
118
+ jinja: "テンプレート",
119
+ jinja2: "テンプレート",
120
+ j2: "テンプレート",
121
+ liquid: "テンプレート",
122
+ twig: "テンプレート",
123
+ erb: "テンプレート",
124
+ haml: "テンプレート",
125
+ slim: "テンプレート",
126
+ pug: "テンプレート",
127
+ proto: "Protocol Buffers",
128
+ graphql: "GraphQL",
129
+ gql: "GraphQL",
130
+ tf: "Terraform",
131
+ hcl: "Terraform",
132
+ prisma: "Prisma",
133
+ tpl: "テンプレート",
134
+ tmpl: "テンプレート",
135
+ gohtml: "テンプレート",
136
+ vbs: "VBScript",
137
+ vba: "VB6 / VBA",
138
+ xaml: "XAML",
139
+ axaml: "XAML",
140
+ bzl: "Bazel",
141
+ nix: "Nix",
142
+ zig: "Zig",
143
+ ml: "OCaml",
144
+ mli: "OCaml",
145
+ elm: "Elm",
146
+ sol: "Solidity",
147
+ };
148
+ /** 設定とビルドのファイルの拡張子 */
149
+ const CONFIG_EXTENSIONS = new Set([
150
+ "json",
151
+ "jsonc",
152
+ "json5",
153
+ "yaml",
154
+ "yml",
155
+ "toml",
156
+ "ini",
157
+ "cfg",
158
+ "conf",
159
+ "properties",
160
+ "xml",
161
+ "xsd",
162
+ "xsl",
163
+ "xslt",
164
+ "config",
165
+ "gradle",
166
+ "plist",
167
+ "cmake",
168
+ "mk",
169
+ "mak",
170
+ "sln",
171
+ "csproj",
172
+ "vbproj",
173
+ "fsproj",
174
+ "props",
175
+ "targets",
176
+ "resx",
177
+ "settings",
178
+ ]);
179
+ /** 文書の拡張子 */
180
+ const DOC_EXTENSIONS = new Set(["md", "markdown", "mdx", "rst", "txt", "adoc", "asciidoc"]);
181
+ /** データの拡張子(個人情報を含み得る・大きい・変更の対象になりにくい) */
182
+ const DATA_EXTENSIONS = {
183
+ csv: "データ",
184
+ tsv: "データ",
185
+ psv: "データ",
186
+ jsonl: "データ",
187
+ ndjson: "データ",
188
+ geojson: "データ",
189
+ log: "ログ",
190
+ dump: "データ",
191
+ bak: "バックアップ",
192
+ dat: "データ",
193
+ ipynb: "ノートブック(出力を含み大きい)",
194
+ parquet: "データ",
195
+ avro: "データ",
196
+ sqlite: "データベース",
197
+ sqlite3: "データベース",
198
+ db: "データベース",
199
+ mdb: "データベース",
200
+ accdb: "データベース",
201
+ dbf: "データベース",
202
+ har: "通信の記録",
203
+ };
204
+ /** 鍵・証明書・認証情報のファイルの拡張子(中身は伏せるが、ファイルごと出さない) */
205
+ const SECRET_EXTENSIONS = new Set([
206
+ "pem",
207
+ "key",
208
+ "crt",
209
+ "cer",
210
+ "der",
211
+ "p7b",
212
+ "p8",
213
+ "p12",
214
+ "pfx",
215
+ "jks",
216
+ "keystore",
217
+ "ppk",
218
+ "snk",
219
+ "pvk",
220
+ "gpg",
221
+ "pgp",
222
+ "asc",
223
+ "kdbx",
224
+ "ovpn",
225
+ "tfvars",
226
+ "tfstate",
227
+ ]);
228
+ /** 鍵・認証情報のファイル名(小文字) */
229
+ const SECRET_FILE_NAMES = new Set([
230
+ "id_rsa",
231
+ "id_dsa",
232
+ "id_ecdsa",
233
+ "id_ed25519",
234
+ ".npmrc",
235
+ ".yarnrc",
236
+ ".yarnrc.yml",
237
+ ".pypirc",
238
+ ".netrc",
239
+ "_netrc",
240
+ ".git-credentials",
241
+ ".htpasswd",
242
+ ".pgpass",
243
+ ".dockercfg",
244
+ ".s3cfg",
245
+ ".boto",
246
+ "credentials",
247
+ "terraform.tfstate.backup",
248
+ ]);
249
+ /** この名前(拡張子を除いて小文字)の設定・文書のファイルは認証情報とみなす */
250
+ const SECRET_FILE_STEMS = new Set([
251
+ "secret",
252
+ "secrets",
253
+ "credential",
254
+ "credentials",
255
+ "client_secret",
256
+ "client_secrets",
257
+ "service-account",
258
+ "service_account",
259
+ "serviceaccount",
260
+ "serviceaccountkey",
261
+ ]);
262
+ /** 拡張子の無い(またはドットで始まる)既知のファイル名(小文字 → include に書く名前) */
263
+ const KNOWN_FILE_NAMES = {
264
+ makefile: "Makefile",
265
+ gnumakefile: "GNUmakefile",
266
+ containerfile: "Containerfile",
267
+ jenkinsfile: "Jenkinsfile",
268
+ vagrantfile: "Vagrantfile",
269
+ procfile: "Procfile",
270
+ gemfile: "Gemfile",
271
+ rakefile: "Rakefile",
272
+ brewfile: "Brewfile",
273
+ podfile: "Podfile",
274
+ cartfile: "Cartfile",
275
+ pipfile: "Pipfile",
276
+ justfile: "Justfile",
277
+ gradlew: "gradlew",
278
+ mvnw: "mvnw",
279
+ license: "LICENSE",
280
+ licence: "LICENCE",
281
+ notice: "NOTICE",
282
+ authors: "AUTHORS",
283
+ codeowners: "CODEOWNERS",
284
+ contributing: "CONTRIBUTING",
285
+ changelog: "CHANGELOG",
286
+ readme: "README",
287
+ copying: "COPYING",
288
+ build: "BUILD",
289
+ workspace: "WORKSPACE",
290
+ "go.mod": "go.mod",
291
+ "go.work": "go.work",
292
+ ".gitignore": ".gitignore",
293
+ ".gitattributes": ".gitattributes",
294
+ ".gitmodules": ".gitmodules",
295
+ ".dockerignore": ".dockerignore",
296
+ ".editorconfig": ".editorconfig",
297
+ ".prettierrc": ".prettierrc",
298
+ ".prettierignore": ".prettierignore",
299
+ ".eslintrc": ".eslintrc",
300
+ ".eslintignore": ".eslintignore",
301
+ ".babelrc": ".babelrc",
302
+ ".browserslistrc": ".browserslistrc",
303
+ ".stylelintrc": ".stylelintrc",
304
+ ".npmignore": ".npmignore",
305
+ ".nvmrc": ".nvmrc",
306
+ ".node-version": ".node-version",
307
+ ".python-version": ".python-version",
308
+ ".ruby-version": ".ruby-version",
309
+ ".tool-versions": ".tool-versions",
310
+ ".flake8": ".flake8",
311
+ ".pylintrc": ".pylintrc",
312
+ ".rubocop": ".rubocop",
313
+ ".clang-format": ".clang-format",
314
+ ".clang-tidy": ".clang-tidy",
315
+ ".repomixignore": ".repomixignore",
316
+ };
317
+ /** 他から取り込んだコードのディレクトリ名(大文字小文字はこのとおり) */
318
+ const VENDOR_DIRECTORY_NAMES = new Set([
319
+ "vendor",
320
+ "third_party",
321
+ "third-party",
322
+ "bower_components",
323
+ "jspm_packages",
324
+ "Pods",
325
+ "Carthage",
326
+ ]);
327
+ /** 生成コードによくある名前の終わり(一致すれば exclude をこのパターンで書く) */
328
+ const GENERATED_SUFFIXES = [
329
+ ".pb.go",
330
+ "_pb2.py",
331
+ "_pb2.pyi",
332
+ "_pb2_grpc.py",
333
+ ".pb.cc",
334
+ ".pb.h",
335
+ ".pb.swift",
336
+ "_pb.js",
337
+ "_pb.d.ts",
338
+ "_grpc_pb.js",
339
+ ".g.dart",
340
+ ".freezed.dart",
341
+ ".gen.go",
342
+ ".gen.ts",
343
+ ".generated.ts",
344
+ ".generated.cs",
345
+ ".g.cs",
346
+ ".Designer.cs",
347
+ ".Designer.vb",
348
+ ];
349
+ const MINIFIED = /\.min\.(js|mjs|cjs|css)$/i;
350
+ /** 先頭の何行を生成コードの印として見るか */
351
+ const HEADER_LINES = 15;
352
+ /**
353
+ * 生成コードの印。誤って手書きのコードを外さないよう、強い印だけを見る:
354
+ * `@generated` / `<auto-generated` か、「generated」と「do not edit / modify / change」の両方
355
+ * (Go の `Code generated ... DO NOT EDIT.`、protoc の `Generated by the protocol buffer compiler. DO NOT EDIT!` など)。
356
+ * Django のマイグレーション(`# Generated by Django ...`)のように手で直すこともあるものは印にしない
357
+ */
358
+ export function looksGenerated(head) {
359
+ const header = head.split(/\r?\n/).slice(0, HEADER_LINES).join("\n");
360
+ if (/@generated\b|<auto-generated\b/i.test(header)) {
361
+ return true;
362
+ }
363
+ return /\bgenerated\b/i.test(header) && /\b(?:do not|don't|must not) (?:edit|modify|change)\b/i.test(header);
364
+ }
365
+ const SHEBANG_LANGUAGES = [
366
+ [/python/, "Python"],
367
+ [/\b(?:node|deno|bun)\b/, "JavaScript"],
368
+ [/ruby/, "Ruby"],
369
+ [/perl/, "Perl"],
370
+ [/php/, "PHP"],
371
+ [/pwsh|powershell/, "PowerShell"],
372
+ ];
373
+ function shebangLanguage(head) {
374
+ const first = head.split(/\r?\n/, 1)[0] ?? "";
375
+ if (!first.startsWith("#!")) {
376
+ return undefined;
377
+ }
378
+ return SHEBANG_LANGUAGES.find(([pattern]) => pattern.test(first))?.[1] ?? "シェル";
379
+ }
380
+ const UNKNOWN_REASON = "種類が分からない";
381
+ /** 拡張子(小文字・ドットなし)。ドットで始まる名前や拡張子の無い名前は "" */
382
+ function extensionOf(name) {
383
+ const ext = path.posix.extname(name);
384
+ return ext === "" || ext === name ? "" : ext.slice(1).toLowerCase();
385
+ }
386
+ function stemOf(name) {
387
+ const ext = path.posix.extname(name);
388
+ return (ext === "" || ext === name ? name : name.slice(0, -ext.length)).toLowerCase();
389
+ }
390
+ /** 中身を読まずに決まる種類(shebang の判定が要るものは undefined) */
391
+ function categorize(name) {
392
+ const lower = name.toLowerCase();
393
+ const ext = extensionOf(name);
394
+ if (SECRET_FILE_NAMES.has(lower) || SECRET_EXTENSIONS.has(ext) || lower.endsWith(".tfstate.backup")) {
395
+ return { kind: "omit", label: ext === "" ? name : `.${ext}`, reason: "鍵・証明書・認証情報のファイル" };
396
+ }
397
+ if (Object.hasOwn(DATA_EXTENSIONS, ext)) {
398
+ return { kind: "omit", label: `.${ext}`, reason: DATA_EXTENSIONS[ext] ?? "データ" };
399
+ }
400
+ if (lower.startsWith("dockerfile")) {
401
+ return { kind: "name", glob: "**/Dockerfile*" };
402
+ }
403
+ const known = KNOWN_FILE_NAMES[lower];
404
+ if (known !== undefined) {
405
+ return { kind: "name", glob: `**/${known}` };
406
+ }
407
+ const language = LANGUAGE_BY_EXTENSION[ext];
408
+ if (language !== undefined) {
409
+ return { kind: "language", language, glob: `**/*.${ext}` };
410
+ }
411
+ if (CONFIG_EXTENSIONS.has(ext) || DOC_EXTENSIONS.has(ext)) {
412
+ if (SECRET_FILE_STEMS.has(stemOf(name))) {
413
+ return { kind: "omit", label: name, reason: "鍵・証明書・認証情報のファイル" };
414
+ }
415
+ return CONFIG_EXTENSIONS.has(ext) ? { kind: "config", glob: `**/*.${ext}` } : { kind: "docs", glob: `**/*.${ext}` };
416
+ }
417
+ if (ext === "") {
418
+ return undefined;
419
+ }
420
+ return { kind: "omit", label: `.${ext}`, reason: UNKNOWN_REASON };
421
+ }
422
+ /** relativePath の中で最も浅い、他から取り込んだコードのディレクトリ(`a/vendor` など) */
423
+ function vendorDirectoryOf(relativePath) {
424
+ const segments = relativePath.split("/");
425
+ for (let i = 0; i < segments.length - 1; i += 1) {
426
+ const segment = segments[i] ?? "";
427
+ if (VENDOR_DIRECTORY_NAMES.has(segment)) {
428
+ return { dir: segments.slice(0, i + 1).join("/"), name: segment };
429
+ }
430
+ }
431
+ return undefined;
432
+ }
433
+ function generatedPatternOf(relativePath) {
434
+ const lower = relativePath.toLowerCase();
435
+ const suffix = GENERATED_SUFFIXES.find((s) => lower.endsWith(s.toLowerCase()));
436
+ return suffix === undefined ? relativePath : `**/*${suffix}`;
437
+ }
438
+ /** include の並び: 言語(件数の多い順)→ 設定・文書(拡張子の順)→ 既知の名前 → パス */
439
+ const GROUP_ORDER = { language: 0, config: 1, docs: 1, name: 2, path: 3 };
440
+ /**
441
+ * @param options.alwaysInclude ファイルが無くても include に書くもの(tazuna の docs/ 連携が書く
442
+ * Markdown など)
443
+ */
444
+ export function planInclude(input, options = {}) {
445
+ const heads = new Map();
446
+ const readHead = (relativePath) => {
447
+ if (!heads.has(relativePath)) {
448
+ heads.set(relativePath, input.readHead(relativePath));
449
+ }
450
+ return heads.get(relativePath);
451
+ };
452
+ const candidates = [];
453
+ const omittedFiles = [];
454
+ for (const file of input.files) {
455
+ const name = path.posix.basename(file.relativePath);
456
+ let category = categorize(name);
457
+ if (category?.kind === "omit" && category.reason !== UNKNOWN_REASON) {
458
+ // 鍵・データは中身を読まずに外す
459
+ omittedFiles.push({ file, label: category.label, reason: category.reason });
460
+ continue;
461
+ }
462
+ const head = readHead(file.relativePath);
463
+ if (head === undefined || head.includes("\u0000")) {
464
+ // 読めないもの・バイナリはパックにも入らない
465
+ continue;
466
+ }
467
+ if (category === undefined) {
468
+ const language = shebangLanguage(head);
469
+ if (language === undefined) {
470
+ if (head.trim() !== "") {
471
+ omittedFiles.push({ file, label: name, reason: UNKNOWN_REASON });
472
+ }
473
+ continue;
474
+ }
475
+ category = { kind: "language", language, glob: file.relativePath };
476
+ }
477
+ if (category.kind === "omit") {
478
+ if (head.trim() !== "") {
479
+ omittedFiles.push({ file, label: category.label, reason: category.reason });
480
+ }
481
+ continue;
482
+ }
483
+ candidates.push({
484
+ file,
485
+ glob: category.glob,
486
+ group: category.kind === "language" && category.glob === file.relativePath ? "path" : category.kind,
487
+ language: category.kind === "language" ? category.language : undefined,
488
+ });
489
+ }
490
+ // 2. include に入る種類のうち外すもの(取り込んだコード・圧縮済み・生成コード)
491
+ // 同じ名前の取り込んだコードのディレクトリが複数の場所にあれば `**/<名前>/**` にまとめる
492
+ const vendorDirs = new Map();
493
+ for (const { file } of candidates) {
494
+ const vendor = vendorDirectoryOf(file.relativePath);
495
+ if (vendor !== undefined) {
496
+ const dirs = vendorDirs.get(vendor.name) ?? new Set();
497
+ dirs.add(vendor.dir);
498
+ vendorDirs.set(vendor.name, dirs);
499
+ }
500
+ }
501
+ const excludedCandidates = [];
502
+ const survivors = [];
503
+ for (const candidate of candidates) {
504
+ const { file, language } = candidate;
505
+ const vendor = vendorDirectoryOf(file.relativePath);
506
+ if (vendor !== undefined) {
507
+ const many = (vendorDirs.get(vendor.name)?.size ?? 0) > 1;
508
+ excludedCandidates.push({ file, pattern: many ? `**/${vendor.name}/**` : `${vendor.dir}/**`, reason: "他から取り込んだコード" });
509
+ continue;
510
+ }
511
+ const minified = MINIFIED.exec(file.relativePath);
512
+ if (minified !== null) {
513
+ excludedCandidates.push({ file, pattern: `**/*.min.${(minified[1] ?? "js").toLowerCase()}`, reason: "圧縮済み" });
514
+ continue;
515
+ }
516
+ // 生成コードの印はソースだけで見る(文書の「編集しないで」の注意書きなどで外さない)
517
+ if (language !== undefined) {
518
+ const head = readHead(file.relativePath);
519
+ if (head !== undefined && looksGenerated(head)) {
520
+ excludedCandidates.push({ file, pattern: generatedPatternOf(file.relativePath), reason: "生成コード" });
521
+ continue;
522
+ }
523
+ }
524
+ survivors.push(candidate);
525
+ }
526
+ // 3. include は残ったものの種類から(外したものにしか無い拡張子は書かない)
527
+ const includeEntries = new Map();
528
+ for (const { glob, group } of survivors) {
529
+ const entry = includeEntries.get(glob) ?? { group, count: 0 };
530
+ entry.count += 1;
531
+ includeEntries.set(glob, entry);
532
+ }
533
+ for (const glob of options.alwaysInclude ?? []) {
534
+ if (!includeEntries.has(glob)) {
535
+ includeEntries.set(glob, { group: glob.startsWith("**/*.") ? "docs" : glob.startsWith("**/") ? "name" : "path", count: 0 });
536
+ }
537
+ }
538
+ const include = [...includeEntries.entries()]
539
+ .map(([glob, entry]) => ({ glob, ...entry }))
540
+ .sort((a, b) => {
541
+ const groupDiff = (GROUP_ORDER[a.group] ?? 9) - (GROUP_ORDER[b.group] ?? 9);
542
+ if (groupDiff !== 0) {
543
+ return groupDiff;
544
+ }
545
+ if (a.group === "language" && a.count !== b.count) {
546
+ return b.count - a.count;
547
+ }
548
+ return a.glob.toLowerCase().localeCompare(b.glob.toLowerCase());
549
+ })
550
+ .map((entry) => entry.glob);
551
+ const includeRegExps = include.map(globToRegExp);
552
+ const matchesInclude = (relativePath) => includeRegExps.some((re) => re.test(relativePath));
553
+ // 4. exclude は include に一致してしまうものだけ書く
554
+ const exclusions = new Map();
555
+ const addExclusion = (pattern, reason) => {
556
+ const current = exclusions.get(pattern);
557
+ if (current === undefined) {
558
+ exclusions.set(pattern, { pattern, reason, count: 1 });
559
+ }
560
+ else {
561
+ current.count += 1;
562
+ }
563
+ };
564
+ for (const { file, pattern, reason } of excludedCandidates) {
565
+ if (matchesInclude(file.relativePath)) {
566
+ addExclusion(pattern, reason);
567
+ }
568
+ }
569
+ // include の拡張子に一致してしまう、入れないファイル(credentials.json など)はパスで外す
570
+ const omissions = new Map();
571
+ for (const { file, label, reason } of omittedFiles) {
572
+ if (matchesInclude(file.relativePath)) {
573
+ addExclusion(file.relativePath, reason);
574
+ }
575
+ const key = `${label.toLowerCase()}\u0000${reason}`;
576
+ const current = omissions.get(key);
577
+ if (current === undefined) {
578
+ omissions.set(key, { label, reason, count: 1, example: file.relativePath });
579
+ }
580
+ else {
581
+ current.count += 1;
582
+ }
583
+ }
584
+ const languageCounts = new Map();
585
+ for (const { language } of survivors) {
586
+ if (language !== undefined) {
587
+ languageCounts.set(language, (languageCounts.get(language) ?? 0) + 1);
588
+ }
589
+ }
590
+ const included = survivors.map((candidate) => candidate.file);
591
+ // exclude の並び: 理由ごと(取り込んだコード → 圧縮済み → 生成コード → その他)、同じ理由の中はパターンの順
592
+ const reasonOrder = ["他から取り込んだコード", "圧縮済み", "生成コード"];
593
+ const rank = (reason) => {
594
+ const index = reasonOrder.indexOf(reason);
595
+ return index === -1 ? reasonOrder.length : index;
596
+ };
597
+ const excluded = [...exclusions.values()].sort((a, b) => rank(a.reason) - rank(b.reason) || a.pattern.toLowerCase().localeCompare(b.pattern.toLowerCase()));
598
+ return {
599
+ include,
600
+ exclude: excluded.map((entry) => entry.pattern),
601
+ languages: [...languageCounts.entries()]
602
+ .map(([name, count]) => ({ name, count }))
603
+ .sort((a, b) => b.count - a.count || a.name.localeCompare(b.name)),
604
+ omitted: [...omissions.values()].sort((a, b) => b.count - a.count || a.label.localeCompare(b.label)),
605
+ excluded,
606
+ includedCount: included.length,
607
+ includedBytes: included.reduce((sum, file) => sum + file.size, 0),
608
+ largest: [...included].sort((a, b) => b.size - a.size).slice(0, 3),
609
+ };
610
+ }