Skip to content

Commit 9948b2c

Browse files
isabellahochstainless-app[bot]
authored andcommitted
Implement minimal file collection + diff apply helpers (#7)
* create helpers * export helpers * update deps
1 parent 8f0f959 commit 9948b2c

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
"lint": "./scripts/lint",
2727
"fix": "./scripts/format"
2828
},
29-
"dependencies": {},
3029
"devDependencies": {
3130
"@arethetypeswrong/cli": "^0.17.0",
3231
"@swc/core": "^1.3.102",
3332
"@swc/jest": "^0.2.29",
33+
"@types/glob": "^8.1.0",
3434
"@types/jest": "^29.4.0",
3535
"@types/node": "^20.17.6",
3636
"@typescript-eslint/eslint-plugin": "8.31.1",

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ export {
2020
PermissionDeniedError,
2121
UnprocessableEntityError,
2222
} from './core/error';
23+
24+
export { collectFiles, applyChanges, type FileData } from './lib/helpers';

src/lib/helpers.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { glob } from 'glob';
4+
5+
export interface FileData {
6+
path: string;
7+
contents: string;
8+
}
9+
10+
const DEFAULT_PATTERNS = [
11+
'**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx',
12+
'**/package.json', '**/tsconfig*.json', '**/*.config.*'
13+
];
14+
15+
const DEFAULT_IGNORE = [
16+
'node_modules/**',
17+
'.claude/**',
18+
'.git/**',
19+
'dist/**',
20+
'build/**',
21+
'.next/**',
22+
'coverage/**',
23+
'**/.env*'
24+
];
25+
26+
export async function collectFiles(
27+
basePath: string = process.cwd(),
28+
patterns: string[] = DEFAULT_PATTERNS,
29+
ignore: string[] = DEFAULT_IGNORE
30+
): Promise<FileData[]> {
31+
const originalCwd = process.cwd();
32+
33+
try {
34+
process.chdir(basePath);
35+
36+
const allFilePaths: string[] = [];
37+
for (const pattern of patterns) {
38+
const filePaths = await glob(pattern, {
39+
ignore,
40+
nodir: true,
41+
absolute: false
42+
});
43+
allFilePaths.push(...filePaths);
44+
}
45+
46+
const filePaths = [...new Set(allFilePaths)];
47+
48+
const files: FileData[] = [];
49+
50+
for (const filePath of filePaths) {
51+
try {
52+
const content = fs.readFileSync(path.resolve(basePath, filePath), 'utf8');
53+
if (content.trim()) {
54+
files.push({
55+
path: filePath,
56+
content
57+
});
58+
}
59+
} catch {
60+
continue;
61+
}
62+
}
63+
64+
return files;
65+
} finally {
66+
process.chdir(originalCwd);
67+
}
68+
}
69+
70+
export function applyChanges(
71+
changedFiles: FileData[],
72+
basePath: string = process.cwd()
73+
): void {
74+
for (const file of changedFiles) {
75+
try {
76+
const fullPath = path.resolve(basePath, file.path);
77+
const dirPath = path.dirname(fullPath);
78+
79+
fs.mkdirSync(dirPath, { recursive: true });
80+
fs.writeFileSync(fullPath, file.contents, 'utf8');
81+
} catch {
82+
continue;
83+
}
84+
}
85+
}

yarn.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,14 @@
891891
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.6.tgz#628effeeae2064a1b4e79f78e81d87b7e5fc7b50"
892892
integrity sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw==
893893

894+
"@types/glob@^8.1.0":
895+
version "8.1.0"
896+
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-8.1.0.tgz#b63e70155391b0584dce44e7ea25190bbc38f2fc"
897+
integrity sha512-IO+MJPVhoqz+28h1qLAcBEH2+xHMK6MTyHJc7MTnnYb6wsoLR29POVGJ7LycmVXIqyy/4/2ShP5sUwTXuOwb/w==
898+
dependencies:
899+
"@types/minimatch" "^5.1.2"
900+
"@types/node" "*"
901+
894902
"@types/graceful-fs@^4.1.3":
895903
version "4.1.9"
896904
resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.9.tgz#2a06bc0f68a20ab37b3e36aa238be6abdf49e8b4"
@@ -930,6 +938,11 @@
930938
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.15.tgz#596a1747233694d50f6ad8a7869fcb6f56cf5841"
931939
integrity sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==
932940

941+
"@types/minimatch@^5.1.2":
942+
version "5.1.2"
943+
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-5.1.2.tgz#07508b45797cb81ec3f273011b054cd0755eddca"
944+
integrity sha512-K0VQKziLUWkVKiRVrx4a40iPaxTUefQmjtkQofBkYRcoaaL/8rhwDWww9qWbrgicNOgnpIsMxyNIUM4+n6dUIA==
945+
933946
"@types/node@*":
934947
version "20.10.5"
935948
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.10.5.tgz#47ad460b514096b7ed63a1dae26fad0914ed3ab2"

0 commit comments

Comments
 (0)