bolt.diy/app/utils/fileUtils.ts
2025-03-11 12:11:41 -07:00

51 lines
1.1 KiB
TypeScript

import ignore from 'ignore';
// Common patterns to ignore, similar to .gitignore
export const IGNORE_PATTERNS = [
'node_modules/**',
'.git/**',
'dist/**',
'build/**',
'.next/**',
'coverage/**',
'.cache/**',
'.vscode/**',
'.idea/**',
'**/*.log',
'**/.DS_Store',
'**/npm-debug.log*',
'**/yarn-debug.log*',
'**/yarn-error.log*',
'**/package-lock.json',
];
export const MAX_FILES = 1000;
export const ig = ignore().add(IGNORE_PATTERNS);
export const generateId = () => Math.random().toString(36).substring(2, 15);
export const isBinaryFile = async (file: File): Promise<boolean> => {
const chunkSize = 1024;
const buffer = new Uint8Array(await file.slice(0, chunkSize).arrayBuffer());
for (let i = 0; i < buffer.length; i++) {
const byte = buffer[i];
if (byte === 0 || (byte < 32 && byte !== 9 && byte !== 10 && byte !== 13)) {
return true;
}
}
return false;
};
export const shouldIncludeFile = (path: string): boolean => {
const projectDirectory = '/home/project/';
if (path.startsWith(projectDirectory)) {
path = path.substring(projectDirectory.length);
}
return !ig.ignores(path);
};