Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/dev/file-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,13 @@ export async function getFileInfo(path: string): Promise<FileInfo> {
return { content, file_size: stats.size, last_modified: stats.mtime.toISOString() };
}

export async function getGitignorePatterns(
cwd: string,
outputPath: string
): Promise<Ignore | undefined> {
const outputDir = relative(cwd, outputPath);
export async function getGitignorePatterns(cwd: string): Promise<Ignore | undefined> {
try {
const content = await readFile(join(cwd, '.gitignore'), 'utf-8');
const patterns = content
.split('\n')
.map((line) => line.trim())
.filter((line) => line && !line.startsWith('#'));
patterns.push(`!${outputDir}`);
return ignore().add(patterns);
} catch {
return;
Expand All @@ -135,19 +130,27 @@ export async function getGitignorePatterns(

export async function listFiles(cwd: string, outputPath: string): Promise<string[]> {
const files: string[] = [];
const gitIgnore = await getGitignorePatterns(cwd, outputPath);
const gitIgnore = await getGitignorePatterns(cwd);

async function walk(currentPath: string): Promise<void> {
const entries = await readdir(currentPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = join(currentPath, entry.name);
const relPath = relative(cwd, fullPath);
if (relPath && gitIgnore?.ignores(relPath)) {
if (!relPath || relPath === '.git') {
continue;
}

const isIgnored = gitIgnore?.ignores(relPath) && !fullPath.startsWith(outputPath);
if (entry.isFile()) {
if (isIgnored) {
continue;
}
files.push(fullPath);
} else if (entry.isDirectory()) {
if (isIgnored && !outputPath.startsWith(fullPath)) {
continue;
}
await walk(fullPath);
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/dev/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ async function handleDeletePathRequest(cwd: string, req: IncomingMessage): Promi
}

async function startFileWatcher(cwd: string, outputPath: string): Promise<FSWatcher> {
const gitIgnore = await getGitignorePatterns(cwd, outputPath);
const gitIgnore = await getGitignorePatterns(cwd);

const watcher = chokidar.watch(cwd, {
ignoreInitial: true,
Expand All @@ -289,6 +289,10 @@ async function startFileWatcher(cwd: string, outputPath: string): Promise<FSWatc
return false;
}

if (path.startsWith(outputPath) || outputPath.startsWith(path)) {
return false;
}

return gitIgnore.ignores(relativePath);
},
});
Expand Down