-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesm-example.js
More file actions
133 lines (112 loc) · 4.2 KB
/
esm-example.js
File metadata and controls
133 lines (112 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// ES Modules Example
import {
FileContextBuilder,
WhitelistBlacklist,
FileContentSearch,
RegexPatternMatcher,
type FileCollectorConfig,
type FileSearchOptions
} from 'contextr';
// Create a configuration with whitelist and blacklist
const config: FileCollectorConfig = {
name: "My Project Context",
showContents: true,
showMeta: true,
includeDirs: [
{
path: "./src",
include: ["**/*.js", "**/*.ts"],
exclude: ["**/*.test.js", "**/*.spec.ts"],
recursive: true,
useRegex: false
}
],
includeFiles: ["./package.json", "./README.md"],
excludeFiles: ["**/node_modules/**", "**/dist/**"],
useRegex: false
};
// Using the WhitelistBlacklist helper
const whitelist = WhitelistBlacklist.createWhitelist(["**/*.js", "**/*.ts"]);
const blacklist = WhitelistBlacklist.createBlacklist(["**/*.test.js", "**/node_modules/**"]);
// Combine whitelist and blacklist with regex support
const combinedConfig = WhitelistBlacklist.createConfig({
whitelist,
blacklist,
baseDir: "./src",
useRegex: true
});
// Add in-file search to only include files containing specific content
config.searchInFiles = {
pattern: "import\\s+{\\s*React",
isRegex: true
};
// Filter by file extension
const filterByExtension = (files, extensions) => {
return files.filter(file => {
const ext = file.filePath.split('.').pop();
return extensions.includes(ext);
});
};
// Build the context
async function buildContext() {
try {
// Using the basic configuration
const builder = new FileContextBuilder(config);
const context = await builder.build();
console.log(`Built context with ${context.files.length} files`);
// Filter files by extension
const tsFiles = filterByExtension(context.files, ['ts', 'tsx']);
console.log(`Found ${tsFiles.length} TypeScript files`);
// Search for content within the context files
const searchOptions: FileSearchOptions = {
pattern: "function\\s+\\w+\\s*\\(",
isRegex: true,
caseSensitive: false,
wholeWord: false,
contextLines: 2,
maxResults: 100
};
// Different search output formats
const searchResults = FileContentSearch.searchInFiles(context.files, searchOptions);
console.log(`Found ${searchResults.length} files with matches`);
// Get total match count
const totalMatches = FileContentSearch.countMatches(context.files, searchOptions);
console.log(`Total matches: ${totalMatches}`);
// Get just the list of matching files
const matchingFiles = FileContentSearch.searchForMatchingFiles(context.files, searchOptions);
console.log(`Matching files: ${matchingFiles.length}`);
// Get results in JSON format
const jsonResults = FileContentSearch.searchAsJson(context.files, searchOptions);
// Format the results with highlighting
const formattedResults = FileContentSearch.formatResults(
searchResults.map(result => FileContentSearch.addContextLines(result, 2)),
true, // show file path
true // highlight matches
);
// Using regex pattern matching directly with flags
const fileContent = "function hello() { return 'world'; }\nFUNCTION goodbye() {}";
// Case-insensitive search with 'i' flag
const isMatch = RegexPatternMatcher.test(fileContent, "function:i");
console.log(`Pattern match: ${isMatch}`); // true, matches both function and FUNCTION
// Get matches with line numbers
const matches = RegexPatternMatcher.getMatchesWithLineNumbers(fileContent, "function\\s+\\w+:gi");
console.log(`Found ${matches.length} function declarations with line numbers`);
// Extract context around matches
const matchesWithContext = RegexPatternMatcher.getMatchesWithContext(fileContent, "function\\s+\\w+:g", 1);
console.log(`Matches with context: ${JSON.stringify(matchesWithContext, null, 2)}`);
return context;
} catch (error) {
console.error("Error building context:", error);
throw error;
}
}
// Execute the function
buildContext()
.then(context => {
console.log("Context built successfully");
})
.catch(error => {
console.error("Failed to build context:", error);
});
// Export for use in other modules
export { buildContext };