@polka-codes/cli 0.1.6 → 0.1.8

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.
@@ -1,100 +0,0 @@
1
- import { promises as fs } from 'node:fs';
2
- import { join, relative, resolve } from 'node:path';
3
- import ignore from 'ignore';
4
- /** Default patterns commonly ignored in projects of various languages. */
5
- const DEFAULT_IGNORES = [
6
- '__pycache__',
7
- '.DS_Store',
8
- '.env',
9
- '.git',
10
- '.idea',
11
- '.svn',
12
- '.temp',
13
- '.vscode',
14
- 'coverage',
15
- 'dist',
16
- 'node_modules',
17
- 'out',
18
- 'Thumbs.db',
19
- ];
20
- /**
21
- * Reads a `.gitignore` file in `dirPath` (if it exists) and appends its lines
22
- * to the `basePatterns`. Returns a new array without mutating the original.
23
- */
24
- async function extendPatterns(basePatterns, dirPath) {
25
- try {
26
- const gitignorePath = join(dirPath, '.gitignore');
27
- const content = await fs.readFile(gitignorePath, 'utf8');
28
- const lines = content.split(/\r?\n/).filter(Boolean);
29
- return [...basePatterns, ...lines];
30
- }
31
- catch {
32
- // No .gitignore or unreadable
33
- return basePatterns;
34
- }
35
- }
36
- /** Creates an `ignore` instance from the given patterns. */
37
- function createIgnore(patterns) {
38
- return ignore().add(patterns);
39
- }
40
- /**
41
- * Lists files under `dirPath` in BFS order, respecting:
42
- * - A default set of ignores
43
- * - A root .gitignore under `cwd`
44
- * - Any .gitignore files in child directories (merged as we go)
45
- *
46
- * Returns `[files, limitReached]`:
47
- * - `files` is the array of file paths (relative to `cwd`)
48
- * - `limitReached` is `true` if `maxCount` was hit, otherwise `false`
49
- */
50
- export async function listFiles(dirPath, recursive, maxCount, cwd) {
51
- // Merge default ignores with root .gitignore (if found)
52
- let rootPatterns = [...DEFAULT_IGNORES];
53
- try {
54
- const rootGitignore = await fs.readFile(join(cwd, '.gitignore'), 'utf8');
55
- const lines = rootGitignore.split(/\r?\n/).filter(Boolean);
56
- rootPatterns = [...rootPatterns, ...lines];
57
- }
58
- catch {
59
- // No .gitignore at root or unreadable; ignore silently
60
- }
61
- // Final results (relative to `cwd`) and indicator if we reached the limit
62
- const results = [];
63
- // BFS queue
64
- // Each entry holds the directory path and the patterns inherited from its parent
65
- const queue = [{ path: resolve(dirPath), patterns: rootPatterns }];
66
- // Perform BFS until queue is empty or maxCount is reached
67
- while (queue.length > 0) {
68
- // biome-ignore lint/style/noNonNullAssertion: checked above
69
- const { path: currentPath, patterns: parentPatterns } = queue.shift();
70
- // Merge parent's patterns with local .gitignore
71
- const mergedPatterns = await extendPatterns(parentPatterns, currentPath);
72
- const folderIg = createIgnore(mergedPatterns);
73
- const entries = await fs.readdir(currentPath, { withFileTypes: true });
74
- for (const entry of entries) {
75
- const fullPath = join(currentPath, entry.name);
76
- // Convert full path to something relative to `cwd`
77
- const relPath = relative(cwd, fullPath).replace(/\\/g, '/');
78
- if (folderIg.ignores(relPath)) {
79
- continue; // Skip ignored entries
80
- }
81
- if (entry.isDirectory()) {
82
- if (recursive) {
83
- queue.push({ path: fullPath, patterns: mergedPatterns });
84
- }
85
- }
86
- else {
87
- results.push(relPath);
88
- if (results.length >= maxCount) {
89
- results.sort();
90
- // Stop searching as soon as we reach maxCount
91
- return [results, true];
92
- }
93
- }
94
- }
95
- }
96
- results.sort();
97
- // If we exhaust the BFS queue, we did not reach maxCount
98
- return [results, false];
99
- }
100
- //# sourceMappingURL=listFiles.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"listFiles.js","sourceRoot":"","sources":["../../src/utils/listFiles.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,EAAE,EAAE,MAAM,SAAS,CAAA;AACxC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnD,OAAO,MAAuB,MAAM,QAAQ,CAAA;AAE5C,0EAA0E;AAC1E,MAAM,eAAe,GAAG;IACtB,aAAa;IACb,WAAW;IACX,MAAM;IACN,MAAM;IACN,OAAO;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C,OAAO,CAAC,CAAA;gBACrB,IAAI,OAAO,CAAC,MAAM,IAAI,QAAQ,EAAE,CAAC;oBAC/B,OAAO,CAAC,IAAI,EAAE,CAAA;oBACd,8CAA8C;oBAC9C,OAAO,CAAC,OAAO,EAAE,IAAI,CAAC,CAAA;gBACxB,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,CAAC,IAAI,EAAE,CAAA;IACd,yDAAyD;IACzD,OAAO,CAAC,OAAO,EAAE,KAAK,CAAC,CAAA;AACzB,CAAC"}
@@ -1,2 +0,0 @@
1
- export declare const replaceInFile: (fileContent: string, diff: string) => Promise<string>;
2
- //# sourceMappingURL=replaceInFile.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"replaceInFile.d.ts","sourceRoot":"","sources":["../../src/utils/replaceInFile.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,aAAa,gBAAuB,MAAM,QAAQ,MAAM,KAAG,OAAO,CAAC,MAAM,CAoFrF,CAAA"}
@@ -1,85 +0,0 @@
1
- /* Example diff block format:
2
- <<<<<<< SEARCH
3
- // original text
4
- =======
5
- // replacement text
6
- >>>>>>> REPLACE
7
- */
8
- export const replaceInFile = async (fileContent, diff) => {
9
- // Regex to match blocks of the form:
10
- // <<<<<<< SEARCH
11
- // (some lines)
12
- // =======
13
- // (some lines)
14
- // >>>>>>> REPLACE
15
- const blockPattern = /<<<<<+ SEARCH\s*\r?\n([\s\S]*?)\r?\n=======[ \t]*\r?\n([\s\S]*?)\r?\n?>>>>>+ REPLACE/g;
16
- // Parse diff blocks
17
- const blocks = [];
18
- for (let match = blockPattern.exec(diff); match !== null; match = blockPattern.exec(diff)) {
19
- blocks.push({ search: match[1], replace: match[2] });
20
- }
21
- if (blocks.length === 0) {
22
- throw new Error('No valid diff blocks found.');
23
- }
24
- // Helper: try to find the search text in fileContent with progressive relaxation
25
- const findAndReplace = (content, search, replace) => {
26
- // 1) Direct exact match
27
- let index = content.indexOf(search);
28
- if (index !== -1) {
29
- return content.slice(0, index) + replace + content.slice(index + search.length);
30
- }
31
- // 2) Trim leading/ending whitespace in search and content
32
- const trimmedSearch = search.trim();
33
- const trimmedContent = content.trim();
34
- const offset = content.indexOf(trimmedContent); // to restore original indexing if found
35
- index = trimmedContent.indexOf(trimmedSearch);
36
- if (index !== -1) {
37
- // compute correct absolute index in the original content
38
- const absoluteIndex = offset + index;
39
- return content.slice(0, absoluteIndex) + replace + content.slice(absoluteIndex + trimmedSearch.length);
40
- }
41
- // 3) Whitespace-agnostic match:
42
- // Replace all consecutive whitespace in search and content with a single marker
43
- // to see if there's a match ignoring whitespace diffs
44
- const normalizedSearch = trimmedSearch.replace(/\s+/g, ' ');
45
- const normalizedContent = trimmedContent.replace(/\s+/g, ' ');
46
- index = normalizedContent.indexOf(normalizedSearch);
47
- if (index !== -1) {
48
- // Find actual location in the original content. We do a rough approach here:
49
- // We know the substring's "normalized" start is at 'index' in normalizedContent.
50
- // A simple way is to walk through the original trimmedContent to find where that occurs.
51
- // For brevity, we'll do a naive re-scan and hope it's correct in typical cases.
52
- let runningIndex = 0;
53
- let actualPos = offset;
54
- for (const segment of trimmedSearch.replace(/\s+/g, ' ').split(' ')) {
55
- const segIndex = content.indexOf(segment, actualPos);
56
- if (segIndex === -1) {
57
- break; // mismatch, won't happen if we truly found it
58
- }
59
- if (runningIndex === 0) {
60
- // First segment helps define the start
61
- actualPos = segIndex;
62
- }
63
- else {
64
- // Move just after the segment to keep scanning
65
- actualPos = segIndex + segment.length;
66
- }
67
- runningIndex++;
68
- }
69
- // By the time we’re done, actualPos should be the end of the matched substring.
70
- // We do a length calc for the final replacement index:
71
- // but we need the total length of trimmedSearch minus whitespace. We'll reconstruct:
72
- const strippedSearch = trimmedSearch.replace(/\s+/g, '');
73
- const endPos = actualPos; // The end of the final segment
74
- const startPos = endPos - strippedSearch.length;
75
- return content.slice(0, startPos) + replace + content.slice(endPos);
76
- }
77
- throw new Error(`Could not find the following text in file:\n${search}`);
78
- };
79
- let updatedFile = fileContent;
80
- for (const { search, replace } of blocks) {
81
- updatedFile = findAndReplace(updatedFile, search, replace);
82
- }
83
- return updatedFile;
84
- };
85
- //# sourceMappingURL=replaceInFile.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"replaceInFile.js","sourceRoot":"","sources":["../../src/utils/replaceInFile.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,EAAE,WAAmB,EAAE,IAAY,EAAmB,EAAE;IACxF,qCAAqC;IACrC,iBAAiB;IACjB,eAAe;IACf,UAAU;IACV,eAAe;IACf,kBAAkB;IAClB,MAAM,YAAY,GAAG,uFAAuF,CAAA;IAE5G,oBAAoB;IACpB,MAAM,MAAM,GAA0C,EAAE,CAAA;IACxD,KAAK,IAAI,KAAK,GAAG,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,KAAK,KAAK,IAAI,EAAE,KAAK,GAAG,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QAC1F,MAAM,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;IACtD,CAAC;IACD,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAA;IAChD,CAAC;IAED,iFAAiF;IACjF,MAAM,cAAc,GAAG,CAAC,OAAe,EAAE,MAAc,EAAE,OAAe,EAAU,EAAE;QAClF,wBAAwB;QACxB,IAAI,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;QACnC,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;YACjB,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,GAAG,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,GAAG,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