-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommonjs-example.js
More file actions
102 lines (87 loc) · 2.9 KB
/
commonjs-example.js
File metadata and controls
102 lines (87 loc) · 2.9 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
// CommonJS Example
const {
FileContextBuilder,
WhitelistBlacklist,
FileContentSearch,
RegexPatternMatcher
} = require('contextr');
// Create a configuration with whitelist and blacklist
const config = {
name: "My Project Context",
showContents: true,
showMeta: true,
includeDirs: [
{
path: "./src",
include: ["**/*.js", "**/*.ts"],
exclude: ["**/*.test.js", "**/*.spec.ts"],
recursive: true
}
],
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
const combinedConfig = WhitelistBlacklist.createConfig({
whitelist,
blacklist,
baseDir: "./src"
});
// Add in-file search to only include files containing specific content
config.searchInFiles = {
pattern: "import React",
isRegex: false
};
// 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`);
// Search for content within the context files
const searchOptions = {
pattern: "function",
isRegex: false,
caseSensitive: false,
wholeWord: true,
contextLines: 2
};
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}`);
// Format the results with highlighting
const formattedResults = FileContentSearch.formatResults(searchResults, true, true);
console.log(formattedResults);
// Using regex pattern matching directly
const fileContent = "function hello() { return 'world'; }";
const isMatch = RegexPatternMatcher.test(fileContent, "function\\s+hello", "i");
console.log(`Pattern match: ${isMatch}`);
// Get matches with context
const matches = RegexPatternMatcher.getMatches(fileContent, "function\\s+\\w+", "g");
console.log(`Found ${matches.length} function declarations`);
return context;
} catch (error) {
console.error("Error building context:", error);
throw error;
}
}
// Export the function for use in other modules
module.exports = { buildContext };
// Execute if run directly
if (require.main === module) {
buildContext()
.then(context => {
console.log("Context built successfully");
})
.catch(error => {
console.error("Failed to build context:", error);
process.exit(1);
});
}