view-ignored 0.1.0 → 0.2.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.
Files changed (165) hide show
  1. package/README.md +37 -54
  2. package/bin/viewig +2 -2
  3. package/out/src/bin.d.ts +2 -0
  4. package/out/src/bin.js +3 -0
  5. package/out/src/browser/binds/index.d.ts +45 -0
  6. package/out/src/browser/binds/index.js +78 -0
  7. package/out/src/browser/binds/plugins/git.d.ts +3 -0
  8. package/out/src/browser/binds/plugins/git.js +24 -0
  9. package/out/src/browser/binds/plugins/npm.d.ts +11 -0
  10. package/out/src/browser/binds/plugins/npm.js +71 -0
  11. package/out/src/browser/binds/plugins/vsce.d.ts +9 -0
  12. package/out/src/browser/binds/plugins/vsce.js +21 -0
  13. package/out/src/browser/binds/plugins/yarn.d.ts +16 -0
  14. package/out/src/browser/binds/plugins/yarn.js +78 -0
  15. package/out/src/browser/binds/targets.d.ts +64 -0
  16. package/out/src/browser/binds/targets.js +56 -0
  17. package/out/src/browser/errors.d.ts +10 -0
  18. package/out/src/browser/errors.js +20 -0
  19. package/out/src/browser/fileinfo.d.ts +82 -0
  20. package/out/src/browser/fileinfo.js +78 -0
  21. package/{lib → out/src/browser}/index.d.ts +0 -1
  22. package/{lib → out/src}/browser/index.js +0 -1
  23. package/out/src/browser/lib.d.ts +141 -0
  24. package/out/src/browser/lib.js +126 -0
  25. package/out/src/browser/scanner.d.ts +103 -0
  26. package/out/src/browser/scanner.js +161 -0
  27. package/out/src/browser/sorting.d.ts +41 -0
  28. package/out/src/browser/sorting.js +133 -0
  29. package/out/src/browser/sourceinfo.d.ts +62 -0
  30. package/out/src/browser/sourceinfo.js +105 -0
  31. package/out/src/browser/styling.d.ts +90 -0
  32. package/out/src/browser/styling.js +153 -0
  33. package/out/src/cli.d.ts +92 -0
  34. package/out/src/cli.js +249 -0
  35. package/out/src/config.d.ts +141 -0
  36. package/out/src/config.js +257 -0
  37. package/{lib/browser → out/src}/index.d.ts +0 -1
  38. package/{lib → out/src}/index.js +0 -1
  39. package/{lib → out/src}/lib.d.ts +1 -2
  40. package/{lib → out/src}/lib.js +1 -2
  41. package/out/src/styling.d.ts +4 -0
  42. package/out/src/styling.js +40 -0
  43. package/package.json +20 -14
  44. package/lib/bin.d.ts +0 -3
  45. package/lib/bin.d.ts.map +0 -1
  46. package/lib/bin.js +0 -4
  47. package/lib/bin.js.map +0 -1
  48. package/lib/browser/binds/index.d.ts +0 -2
  49. package/lib/browser/binds/index.d.ts.map +0 -1
  50. package/lib/browser/binds/index.js +0 -2
  51. package/lib/browser/binds/index.js.map +0 -1
  52. package/lib/browser/binds/targets.d.ts +0 -38
  53. package/lib/browser/binds/targets.d.ts.map +0 -1
  54. package/lib/browser/binds/targets.js +0 -38
  55. package/lib/browser/binds/targets.js.map +0 -1
  56. package/lib/browser/binds.d.ts +0 -22
  57. package/lib/browser/binds.d.ts.map +0 -1
  58. package/lib/browser/binds.js +0 -14
  59. package/lib/browser/binds.js.map +0 -1
  60. package/lib/browser/fileinfo.d.ts +0 -35
  61. package/lib/browser/fileinfo.d.ts.map +0 -1
  62. package/lib/browser/fileinfo.js +0 -42
  63. package/lib/browser/fileinfo.js.map +0 -1
  64. package/lib/browser/index.d.ts.map +0 -1
  65. package/lib/browser/index.js.map +0 -1
  66. package/lib/browser/lib.d.ts +0 -122
  67. package/lib/browser/lib.d.ts.map +0 -1
  68. package/lib/browser/lib.js +0 -115
  69. package/lib/browser/lib.js.map +0 -1
  70. package/lib/browser/looker.d.ts +0 -60
  71. package/lib/browser/looker.d.ts.map +0 -1
  72. package/lib/browser/looker.js +0 -80
  73. package/lib/browser/looker.js.map +0 -1
  74. package/lib/browser/plugins/git.d.ts +0 -10
  75. package/lib/browser/plugins/git.d.ts.map +0 -1
  76. package/lib/browser/plugins/git.js +0 -23
  77. package/lib/browser/plugins/git.js.map +0 -1
  78. package/lib/browser/plugins/npm.d.ts +0 -12
  79. package/lib/browser/plugins/npm.d.ts.map +0 -1
  80. package/lib/browser/plugins/npm.js +0 -70
  81. package/lib/browser/plugins/npm.js.map +0 -1
  82. package/lib/browser/plugins/vsce.d.ts +0 -10
  83. package/lib/browser/plugins/vsce.d.ts.map +0 -1
  84. package/lib/browser/plugins/vsce.js +0 -23
  85. package/lib/browser/plugins/vsce.js.map +0 -1
  86. package/lib/browser/plugins/yarn.d.ts +0 -15
  87. package/lib/browser/plugins/yarn.d.ts.map +0 -1
  88. package/lib/browser/plugins/yarn.js +0 -46
  89. package/lib/browser/plugins/yarn.js.map +0 -1
  90. package/lib/browser/sorting.d.ts +0 -31
  91. package/lib/browser/sorting.d.ts.map +0 -1
  92. package/lib/browser/sorting.js +0 -121
  93. package/lib/browser/sorting.js.map +0 -1
  94. package/lib/browser/sourcepattern.d.ts +0 -25
  95. package/lib/browser/sourcepattern.d.ts.map +0 -1
  96. package/lib/browser/sourcepattern.js +0 -45
  97. package/lib/browser/sourcepattern.js.map +0 -1
  98. package/lib/browser/styling.d.ts +0 -16
  99. package/lib/browser/styling.d.ts.map +0 -1
  100. package/lib/browser/styling.js +0 -116
  101. package/lib/browser/styling.js.map +0 -1
  102. package/lib/browser/tools/index.d.ts +0 -3
  103. package/lib/browser/tools/index.d.ts.map +0 -1
  104. package/lib/browser/tools/index.js +0 -3
  105. package/lib/browser/tools/index.js.map +0 -1
  106. package/lib/browser/tools/sorting.d.ts +0 -31
  107. package/lib/browser/tools/sorting.d.ts.map +0 -1
  108. package/lib/browser/tools/sorting.js +0 -121
  109. package/lib/browser/tools/sorting.js.map +0 -1
  110. package/lib/browser/tools/styles.d.ts +0 -16
  111. package/lib/browser/tools/styles.d.ts.map +0 -1
  112. package/lib/browser/tools/styles.js +0 -116
  113. package/lib/browser/tools/styles.js.map +0 -1
  114. package/lib/cli.d.ts +0 -26
  115. package/lib/cli.d.ts.map +0 -1
  116. package/lib/cli.js +0 -134
  117. package/lib/cli.js.map +0 -1
  118. package/lib/config.d.ts +0 -80
  119. package/lib/config.d.ts.map +0 -1
  120. package/lib/config.js +0 -105
  121. package/lib/config.js.map +0 -1
  122. package/lib/git.d.ts +0 -13
  123. package/lib/git.d.ts.map +0 -1
  124. package/lib/git.js +0 -37
  125. package/lib/git.js.map +0 -1
  126. package/lib/index.d.ts.map +0 -1
  127. package/lib/index.js.map +0 -1
  128. package/lib/lib.d.ts.map +0 -1
  129. package/lib/lib.js.map +0 -1
  130. package/lib/plugins/git.d.ts +0 -9
  131. package/lib/plugins/git.js +0 -22
  132. package/lib/plugins/git.js.map +0 -1
  133. package/lib/plugins/npm.d.ts +0 -12
  134. package/lib/plugins/npm.js +0 -74
  135. package/lib/plugins/npm.js.map +0 -1
  136. package/lib/plugins/vsce.d.ts +0 -9
  137. package/lib/plugins/vsce.js +0 -22
  138. package/lib/plugins/vsce.js.map +0 -1
  139. package/lib/plugins/yarn.d.ts +0 -15
  140. package/lib/plugins/yarn.js +0 -50
  141. package/lib/plugins/yarn.js.map +0 -1
  142. package/lib/tools/index.d.ts +0 -3
  143. package/lib/tools/index.js +0 -4
  144. package/lib/tools/index.js.map +0 -1
  145. package/lib/tools/methods.d.ts +0 -16
  146. package/lib/tools/methods.js +0 -81
  147. package/lib/tools/methods.js.map +0 -1
  148. package/lib/tools/sorting.d.ts +0 -30
  149. package/lib/tools/sorting.js +0 -121
  150. package/lib/tools/sorting.js.map +0 -1
  151. package/lib/tools/styles.d.ts +0 -15
  152. package/lib/tools/styles.js +0 -123
  153. package/lib/tools/styles.js.map +0 -1
  154. package/lib/util/index.d.ts +0 -3
  155. package/lib/util/index.js +0 -4
  156. package/lib/util/index.js.map +0 -1
  157. package/lib/util/presets.d.ts +0 -24
  158. package/lib/util/presets.js +0 -129
  159. package/lib/util/presets.js.map +0 -1
  160. package/lib/util/sorting.d.ts +0 -30
  161. package/lib/util/sorting.js +0 -121
  162. package/lib/util/sorting.js.map +0 -1
  163. package/lib/util/styles.d.ts +0 -8
  164. package/lib/util/styles.js +0 -52
  165. package/lib/util/styles.js.map +0 -1
@@ -0,0 +1,161 @@
1
+ import ignore from "ignore";
2
+ import { minimatch } from "minimatch";
3
+ export const patternTypeList = ["gitignore", "minimatch"];
4
+ export function isPatternType(value) {
5
+ return typeof value === "string" && patternTypeList.includes(value);
6
+ }
7
+ /**
8
+ * The pattern parser. Can check if the file path is ignored.
9
+ */
10
+ export class Scanner {
11
+ /**
12
+ * If `true`, when calling {@link Scanner.matches}, method will return `true` for ignored path.
13
+ * @default false
14
+ */
15
+ isNegated = false;
16
+ /**
17
+ * Defines way to check paths.
18
+ * @default "gitignore"
19
+ */
20
+ patternType;
21
+ /**
22
+ * Git configuration property.
23
+ * @see {@link https://git-scm.com/docs/git-config#Documentation/git-config.txt-coreignoreCase|git-config ignorecase}.
24
+ * @default false
25
+ */
26
+ ignoreCase;
27
+ patternList = new Set();
28
+ patternListExclude = new Set();
29
+ patternListInclude = new Set();
30
+ ignoreInstance;
31
+ ignoreInstanceExclude;
32
+ ignoreInstanceInclude;
33
+ constructor(options) {
34
+ this.patternType = options?.patternType ?? "gitignore";
35
+ this.ignoreCase = options?.ignoreCase ?? false;
36
+ this.isNegated = options?.negated ?? false;
37
+ this.ignoreInstance = ignore.default(options);
38
+ this.ignoreInstanceExclude = ignore.default(options);
39
+ this.ignoreInstanceInclude = ignore.default(options);
40
+ }
41
+ /**
42
+ * Adds new ignore rule.
43
+ * @param pattern .gitignore file specification pattern.
44
+ */
45
+ add(pattern) {
46
+ if (typeof pattern === "string") {
47
+ pattern = pattern.split('\n');
48
+ }
49
+ if (Array.isArray(pattern)) {
50
+ for (const pat of pattern) {
51
+ this.patternList.add(pat);
52
+ this.ignoreInstance.add(pat);
53
+ }
54
+ }
55
+ return this;
56
+ }
57
+ /**
58
+ * Force ignore pattern.
59
+ * @param pattern .gitignore file specification pattern.
60
+ */
61
+ addExclude(pattern) {
62
+ if (typeof pattern === "string") {
63
+ pattern = pattern.split('\n');
64
+ }
65
+ if (Array.isArray(pattern)) {
66
+ for (const pat of pattern) {
67
+ this.patternListExclude.add(pat);
68
+ this.ignoreInstanceExclude.add(pat);
69
+ }
70
+ }
71
+ return this;
72
+ }
73
+ /**
74
+ * Force ignore pattern.
75
+ * @param pattern .gitignore file specification pattern.
76
+ */
77
+ addInclude(pattern) {
78
+ if (typeof pattern === "string") {
79
+ pattern = pattern.split('\n');
80
+ }
81
+ if (Array.isArray(pattern)) {
82
+ for (const pat of pattern) {
83
+ this.patternListInclude.add(pat);
84
+ this.ignoreInstanceInclude.add(pat);
85
+ }
86
+ }
87
+ return this;
88
+ }
89
+ /**
90
+ * Checks if the scanner should ignore path.
91
+ * @see {@link Scanner.isNegated} can change the return value.
92
+ * @param path Dir entry, path.
93
+ */
94
+ matches(path) {
95
+ if (this.patternType === "gitignore") {
96
+ return this.matchesReal(() => Scanner.matchesGitignore(path, this.ignoreInstanceInclude), () => Scanner.matchesGitignore(path, this.ignoreInstanceExclude), () => Scanner.matchesGitignore(path, this.ignoreInstance));
97
+ }
98
+ // minimatch
99
+ return this.matchesReal(() => Scanner.matchesMinimatch(path, Array.from(this.patternListInclude)), () => Scanner.matchesMinimatch(path, Array.from(this.patternListExclude)), () => Scanner.matchesMinimatch(path, Array.from(this.patternList)));
100
+ }
101
+ matchesReal(include, exclude, ignores) {
102
+ if (include()) {
103
+ return false;
104
+ }
105
+ if (exclude()) {
106
+ return true;
107
+ }
108
+ const ign = ignores();
109
+ return this.isNegated ? !ign : ign;
110
+ }
111
+ static matchesGitignore(path, ignoreInstance) {
112
+ return ignoreInstance.ignores(path);
113
+ }
114
+ static matchesMinimatch(path, patternList) {
115
+ return Array.from(patternList).some((pattern) => {
116
+ return minimatch(path, pattern, { dot: true });
117
+ });
118
+ }
119
+ /**
120
+ * Checks if given pattern is valid.
121
+ * @param pattern Parser pattern.
122
+ */
123
+ patternIsValid(pattern) {
124
+ return Scanner.patternIsValid(pattern, { patternType: this.patternType });
125
+ }
126
+ /**
127
+ * Checks if given pattern is valid.
128
+ * @param pattern Parser pattern.
129
+ */
130
+ static patternIsValid(pattern, options) {
131
+ const { patternType = "gitignore" } = options ?? {};
132
+ if (Array.isArray(pattern)) {
133
+ return pattern.every(p => this.patternIsValid(p, options));
134
+ }
135
+ if (typeof pattern !== "string") {
136
+ return false;
137
+ }
138
+ if (patternType === "gitignore") {
139
+ return ignore.default.isPathValid(pattern);
140
+ }
141
+ if (patternType === "minimatch") {
142
+ try {
143
+ minimatch.makeRe(pattern);
144
+ return true;
145
+ }
146
+ catch (error) {
147
+ return false;
148
+ }
149
+ }
150
+ throw new TypeError(`Unknown pattern type '${patternType}'.`);
151
+ }
152
+ static patternToNegated(pattern) {
153
+ if (Array.isArray(pattern)) {
154
+ return pattern.map(Scanner.patternToNegated);
155
+ }
156
+ return Scanner.patternIsNegated(pattern) ? pattern.replace(/^!/, '') : `!${pattern}`;
157
+ }
158
+ static patternIsNegated(pattern) {
159
+ return pattern.startsWith('!');
160
+ }
161
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Contains all file sort names.
3
+ */
4
+ export declare const sortNameList: readonly ["firstFolders", "firstFiles", "type", "mixed", "modified"];
5
+ /**
6
+ * Contains all file sort names as a type.
7
+ */
8
+ export type SortName = typeof sortNameList[number];
9
+ /**
10
+ * {@link Array.prototype.sort}'s file path comparator.
11
+ */
12
+ export type SortFunc = (a: string, b: string) => number;
13
+ /**
14
+ * Checks if the value is the {@link SortName}.
15
+ */
16
+ export declare function isSortName(value: unknown): value is SortName;
17
+ /**
18
+ * Files and folders are sorted by their names.
19
+ * Folders are displayed before files.
20
+ */
21
+ export declare function firstFolders(a: string, b: string): number;
22
+ /**
23
+ * Files and folders are sorted by their names.
24
+ * Files are displayed before folders.
25
+ */
26
+ export declare function firstFiles(a: string, b: string): number;
27
+ /**
28
+ * Files and folders are sorted by last modified date in descending order.
29
+ * Folders are displayed before files.
30
+ */
31
+ export declare function modified(a: string, b: string, map: Map<string, number>): number;
32
+ /**
33
+ * Files and folders are grouped by extension type then sorted by thir names.
34
+ * Folders are displayed before files.
35
+ */
36
+ export declare function type(a: string, b: string): number;
37
+ /**
38
+ * Files and folders are sorted by their names.
39
+ * Files are interwoven with folders.
40
+ */
41
+ export declare function mixed(a: string, b: string): number;
@@ -0,0 +1,133 @@
1
+ import path from "path";
2
+ /**
3
+ * Contains all file sort names.
4
+ */
5
+ export const sortNameList = ["firstFolders", "firstFiles", "type", "mixed", "modified"];
6
+ /**
7
+ * Checks if the value is the {@link SortName}.
8
+ */
9
+ export function isSortName(value) {
10
+ return typeof value === "string" && sortNameList.includes(value);
11
+ }
12
+ /**
13
+ * @example
14
+ * "path/to/the/file" -> ["path", "to/the/file", false]
15
+ * "file" -> ["file", "file", true]
16
+ * "file/" -> ["file", "", false]
17
+ */
18
+ function slicePath(p) {
19
+ const slashIndex = p.indexOf('/');
20
+ const next = p.substring(0, slashIndex);
21
+ const other = p.substring(slashIndex + 1);
22
+ const isLast = next === '' && (p.lastIndexOf('/') === slashIndex);
23
+ return [slashIndex < 0 ? other : next, other, isLast];
24
+ }
25
+ /**
26
+ * Files and folders are sorted by their names.
27
+ * Folders are displayed before files.
28
+ */
29
+ export function firstFolders(a, b) {
30
+ let comp = 0;
31
+ let next1, others1, next2, others2, last1, last2;
32
+ for (; comp === 0;) {
33
+ [next1, others1, last1] = slicePath(a);
34
+ a = others1;
35
+ [next2, others2, last2] = slicePath(b);
36
+ b = others2;
37
+ comp = mixed(next1, next2);
38
+ if (last1 || last2) {
39
+ if (last1 === last2) {
40
+ break;
41
+ }
42
+ if (last1 === false) {
43
+ return -1;
44
+ }
45
+ return +1;
46
+ }
47
+ }
48
+ return comp;
49
+ }
50
+ /**
51
+ * Files and folders are sorted by their names.
52
+ * Files are displayed before folders.
53
+ */
54
+ export function firstFiles(a, b) {
55
+ let comp = 0;
56
+ let next1, others1, next2, others2, last1, last2;
57
+ for (; comp === 0;) {
58
+ [next1, others1, last1] = slicePath(a);
59
+ a = others1;
60
+ [next2, others2, last2] = slicePath(b);
61
+ b = others2;
62
+ comp = mixed(next1, next2);
63
+ if (last1 || last2) {
64
+ if (last1 === last2) {
65
+ break;
66
+ }
67
+ if (last1 === true) {
68
+ return -1;
69
+ }
70
+ return +1;
71
+ }
72
+ }
73
+ return comp;
74
+ }
75
+ /**
76
+ * Files and folders are sorted by last modified date in descending order.
77
+ * Folders are displayed before files.
78
+ */
79
+ export function modified(a, b, map) {
80
+ let comp = 0;
81
+ let others1, others2, last1, last2;
82
+ for (; comp === 0;) {
83
+ [, others1, last1] = slicePath(a);
84
+ a = others1;
85
+ [, others2, last2] = slicePath(b);
86
+ b = others2;
87
+ comp = (map.get(a) ?? 0) - (map.get(b) ?? 0);
88
+ if (last1 || last2) {
89
+ if (last1 === last2) {
90
+ break;
91
+ }
92
+ if (last1 === false) {
93
+ return -1;
94
+ }
95
+ return +1;
96
+ }
97
+ }
98
+ return comp;
99
+ }
100
+ /**
101
+ * Files and folders are grouped by extension type then sorted by thir names.
102
+ * Folders are displayed before files.
103
+ */
104
+ export function type(a, b) {
105
+ let comp = 0;
106
+ let next1, others1, next2, others2, last1, last2;
107
+ for (; comp === 0;) {
108
+ [next1, others1, last1] = slicePath(a);
109
+ a = others1;
110
+ [next2, others2, last2] = slicePath(b);
111
+ b = others2;
112
+ const ppa = path.parse(next1);
113
+ const ppb = path.parse(next2);
114
+ comp = mixed(ppa.ext, ppb.ext) || mixed(ppa.name, ppb.name);
115
+ if (last1 || last2) {
116
+ if (last1 === last2) {
117
+ break;
118
+ }
119
+ if (last1 === false) {
120
+ return -1;
121
+ }
122
+ return +1;
123
+ }
124
+ }
125
+ return comp;
126
+ }
127
+ /**
128
+ * Files and folders are sorted by their names.
129
+ * Files are interwoven with folders.
130
+ */
131
+ export function mixed(a, b) {
132
+ return a.localeCompare(b, undefined, { ignorePunctuation: false });
133
+ }
@@ -0,0 +1,62 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import { FileSystemAdapter, Methodology, ScanFileOptions, Scanner } from "./lib.js";
3
+ export interface SourceInfoHierarcyOptions<T extends {
4
+ toString(): string;
5
+ }> {
6
+ /**
7
+ * @default true
8
+ */
9
+ closest?: boolean;
10
+ /**
11
+ * @default undefined
12
+ */
13
+ filter?: (path: T) => boolean;
14
+ }
15
+ /**
16
+ * The source of patterns.
17
+ */
18
+ export declare class SourceInfo {
19
+ /**
20
+ * The relative path to the file.
21
+ */
22
+ readonly sourcePath: string;
23
+ /**
24
+ * The pattern parser.
25
+ */
26
+ readonly scanner: Scanner;
27
+ /**
28
+ * The last source file content.
29
+ */
30
+ content?: Buffer;
31
+ constructor(
32
+ /**
33
+ * The relative path to the file.
34
+ */
35
+ sourcePath: string,
36
+ /**
37
+ * The pattern parser.
38
+ */
39
+ scanner: Scanner);
40
+ /**
41
+ * Creates new {@link SourceInfo} instance.
42
+ */
43
+ static from(path: string, methodology: Methodology): SourceInfo;
44
+ /**
45
+ * Gets sources from the methodology.
46
+ */
47
+ static fromMethodology(methodology: Methodology, options: ScanFileOptions): Promise<SourceInfo[]>;
48
+ /**
49
+ * Selects the closest or farthest siblings relative to the path.
50
+ */
51
+ static hierarcy<T extends {
52
+ toString(): string;
53
+ }>(target: string, pathList: T[], options?: SourceInfoHierarcyOptions<T>): T | undefined;
54
+ /**
55
+ * @returns File path of the source.
56
+ */
57
+ toString(): string;
58
+ /**
59
+ * @returns The contents of the source file.
60
+ */
61
+ readSync(cwd: string | undefined, readFileSync: Exclude<FileSystemAdapter["readFileSync"], undefined>): Buffer;
62
+ }
@@ -0,0 +1,105 @@
1
+ import { dirname, join } from "path";
2
+ import { Scanner } from "./lib.js";
3
+ import { glob } from "glob";
4
+ import arrify from "arrify";
5
+ /**
6
+ * The source of patterns.
7
+ */
8
+ export class SourceInfo {
9
+ sourcePath;
10
+ scanner;
11
+ /**
12
+ * The last source file content.
13
+ */
14
+ content;
15
+ constructor(
16
+ /**
17
+ * The relative path to the file.
18
+ */
19
+ sourcePath,
20
+ /**
21
+ * The pattern parser.
22
+ */
23
+ scanner) {
24
+ this.sourcePath = sourcePath;
25
+ this.scanner = scanner;
26
+ }
27
+ /**
28
+ * Creates new {@link SourceInfo} instance.
29
+ */
30
+ static from(path, methodology) {
31
+ const scanner = new Scanner({
32
+ negated: methodology.matcherNegated,
33
+ ignoreCase: methodology.ignoreCase,
34
+ patternType: methodology.matcher
35
+ });
36
+ scanner.add(methodology.matcherAdd);
37
+ scanner.addExclude(methodology.matcherExclude);
38
+ scanner.addInclude(methodology.matcherInclude);
39
+ return new SourceInfo(path, scanner);
40
+ }
41
+ /**
42
+ * Gets sources from the methodology.
43
+ */
44
+ static async fromMethodology(methodology, options) {
45
+ const patterns = arrify(methodology.pattern);
46
+ if (patterns.some(p => typeof p !== "string")) {
47
+ return patterns;
48
+ }
49
+ const paths = await glob(patterns, {
50
+ ...options,
51
+ nodir: true,
52
+ dot: true,
53
+ posix: true
54
+ });
55
+ const sourceInfoList = paths.map(p => SourceInfo.from(p, methodology));
56
+ return sourceInfoList;
57
+ }
58
+ /**
59
+ * Selects the closest or farthest siblings relative to the path.
60
+ */
61
+ static hierarcy(target, pathList, options) {
62
+ const { closest = true, filter: scan } = options ?? {};
63
+ pathList = pathList.sort((a, b) => a.toString().localeCompare(b.toString()));
64
+ const checkStack = [];
65
+ // fill checkStack
66
+ let dir = target.toString();
67
+ for (;;) {
68
+ const parent = dirname(dir);
69
+ if (dir === parent) {
70
+ break;
71
+ }
72
+ checkStack.push(dir = parent);
73
+ }
74
+ if (!closest) {
75
+ checkStack.reverse();
76
+ }
77
+ const cacheDirNames = new Map();
78
+ for (const dir of checkStack) {
79
+ const closestPath = pathList.find(path => {
80
+ let pathDir = cacheDirNames.get(path);
81
+ if (!pathDir) {
82
+ cacheDirNames.set(path, pathDir = dirname(path.toString()));
83
+ }
84
+ if (dir !== pathDir) {
85
+ return false;
86
+ }
87
+ return scan ? scan?.(path) : true;
88
+ });
89
+ return closestPath;
90
+ }
91
+ }
92
+ /**
93
+ * @returns File path of the source.
94
+ */
95
+ toString() {
96
+ return this.sourcePath;
97
+ }
98
+ /**
99
+ * @returns The contents of the source file.
100
+ */
101
+ readSync(cwd, readFileSync) {
102
+ cwd ??= process.cwd();
103
+ return this.content = readFileSync(join(cwd, this.sourcePath));
104
+ }
105
+ }
@@ -0,0 +1,90 @@
1
+ import { FileInfo } from "../index.js";
2
+ import { ChalkInstance } from "chalk";
3
+ import { Options } from "boxen";
4
+ export interface FormatFilesOptions {
5
+ /**
6
+ * @default false
7
+ */
8
+ showSources?: boolean;
9
+ /**
10
+ * @default "paths"
11
+ */
12
+ style: StyleName;
13
+ /**
14
+ * @default "paths"
15
+ */
16
+ decor?: DecorName;
17
+ chalk: ChalkInstance;
18
+ }
19
+ /**
20
+ * @returns Prints a readable file list. Here is '\n' ending.
21
+ */
22
+ export declare function formatFiles(files: FileInfo[], options: FormatFilesOptions): string;
23
+ /**
24
+ * Contains all style names.
25
+ */
26
+ export declare const styleNameList: readonly ["tree", "paths"];
27
+ /**
28
+ * Contains all style names as a type.
29
+ */
30
+ export type StyleName = typeof styleNameList[number];
31
+ /**
32
+ * Checks if the value is the {@link StyleName}.
33
+ */
34
+ export declare function isStyleName(value: unknown): value is StyleName;
35
+ /**
36
+ * Contains all decor names.
37
+ */
38
+ export declare const decorNameList: readonly ["normal", "emoji", "nerdfonts"];
39
+ /**
40
+ * Contains all decor names as a type.
41
+ */
42
+ export type DecorName = typeof decorNameList[number];
43
+ /**
44
+ * Checks if the value is the {@link DecorName}.
45
+ */
46
+ export declare function isDecorName(value: unknown): value is DecorName;
47
+ /**
48
+ * Formatting options for the {@link decorCondition}.
49
+ */
50
+ export interface DecorConditionOptions {
51
+ /**
52
+ * @default ""
53
+ */
54
+ prefix?: string;
55
+ /**
56
+ * @default ""
57
+ */
58
+ postfix?: string;
59
+ /**
60
+ * If the decor is not an emoji or nerd use this string.
61
+ * @default ""
62
+ */
63
+ ifNormal?: string;
64
+ /**
65
+ * If style name (lowercase) contains `emoji` use this string.
66
+ * @default ""
67
+ */
68
+ ifEmoji?: string;
69
+ /**
70
+ * If style name (lowercase) contains `nerd` use this string.
71
+ * @default ""
72
+ */
73
+ ifNerd?: string;
74
+ }
75
+ /**
76
+ * Formats the string for specific style types.
77
+ * @param decor The decor name.
78
+ * @param condition Formatting options.
79
+ */
80
+ export declare function decorCondition(decor: DecorName, condition: DecorConditionOptions): string;
81
+ export interface BoxOptions extends Options {
82
+ noColor?: boolean;
83
+ }
84
+ export declare function boxError(message: string, options?: BoxOptions): string;
85
+ /**
86
+ * Returns file icon for the file name & extension.
87
+ * @param decor The decor name.
88
+ * @param filePath The full file path.
89
+ */
90
+ export declare function decorFile(decor: DecorName | undefined, filePath: string): string;