Skip to content
Merged
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
33 changes: 8 additions & 25 deletions packages/rstack/src/fmt/discoverPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ignore from 'ignore';
import isBinaryPath from 'is-binary-path';
import micromatch from 'micromatch';
import readdir, { type Dirent, type DirentLike } from 'tiny-readdir';
import { createRelativePathResolver, type RelativePathResolver } from './relativePath.ts';

const defaultIgnoredDirNames = new Set(['.git', '.sl', '.svn', '.hg', '.jj', 'node_modules']);

Expand Down Expand Up @@ -38,19 +39,6 @@ const isRelativePathInside = (relativePath: string): boolean =>
const isPathInside = (rootPath: string, filePath: string): boolean =>
isRelativePathInside(path.relative(rootPath, filePath));

type RelativePathResolver = (filePath: string) => string;

const createRelativePathResolver = (rootPath: string): RelativePathResolver => {
const rootPrefix = rootPath.endsWith(path.sep) ? rootPath : `${rootPath}${path.sep}`;

return (filePath) =>
filePath === rootPath
? ''
: filePath.startsWith(rootPrefix)
? filePath.slice(rootPrefix.length)
: path.relative(rootPath, filePath);
};

const toPosixPath = (filePath: string): string =>
path.sep === '\\' ? filePath.replaceAll('\\', '/') : filePath;

Expand Down Expand Up @@ -86,14 +74,14 @@ const findGitRoot = async (cwd: string): Promise<string> => {

class GitIgnoreMatcher {
readonly #rootPath: string;
readonly #rootPrefix: string;
readonly #resolveRelativePath: RelativePathResolver;
readonly #matchers = new Map<string, ReturnType<typeof ignore>>();
readonly #loads = new Map<string, Promise<void>>();
readonly #ignoredDirectories = new Map<string, boolean>();

private constructor(rootPath: string) {
this.#rootPath = rootPath;
this.#rootPrefix = rootPath.endsWith(path.sep) ? rootPath : `${rootPath}${path.sep}`;
this.#resolveRelativePath = createRelativePathResolver(rootPath);
}

static async create(cwd: string): Promise<GitIgnoreMatcher> {
Expand All @@ -103,11 +91,11 @@ class GitIgnoreMatcher {
}

async loadThrough(directoryPath: string): Promise<void> {
if (!isPathInside(this.#rootPath, directoryPath)) {
const relativePath = this.#resolveRelativePath(directoryPath);
if (!isRelativePathInside(relativePath)) {
return;
}

const relativePath = path.relative(this.#rootPath, directoryPath);
const segments = relativePath ? relativePath.split(path.sep) : [];
const loads = [this.#load(this.#rootPath)];
let currentPath = this.#rootPath;
Expand All @@ -121,7 +109,7 @@ class GitIgnoreMatcher {
}

async load(directoryPath: string): Promise<void> {
if (isPathInside(this.#rootPath, directoryPath)) {
if (isRelativePathInside(this.#resolveRelativePath(directoryPath))) {
await this.#load(directoryPath);
}
}
Expand All @@ -131,12 +119,7 @@ class GitIgnoreMatcher {
return false;
}

const relativePath =
filePath === this.#rootPath
? ''
: filePath.startsWith(this.#rootPrefix)
? filePath.slice(this.#rootPrefix.length)
: path.relative(this.#rootPath, filePath);
const relativePath = this.#resolveRelativePath(filePath);
if (relativePath === '' || !isRelativePathInside(relativePath)) {
return false;
}
Expand Down Expand Up @@ -175,7 +158,7 @@ class GitIgnoreMatcher {
return cached;
}

relativePath ??= path.relative(this.#rootPath, directoryPath);
relativePath ??= this.#resolveRelativePath(directoryPath);

// Git cannot re-include a path below an ignored directory.
const parentPath = path.dirname(directoryPath);
Expand Down
7 changes: 3 additions & 4 deletions packages/rstack/src/fmt/ignore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import createIgnore from 'ignore';
import { createRelativePathResolver } from './relativePath.ts';
import type { ResolvedFmtConfig } from './types.ts';

/**
Expand Down Expand Up @@ -28,12 +29,10 @@ const createDefaultIgnoreMatcher = (): IgnoreMatcher => {

const createPatternMatcher = (rootPath: string, patterns: string): IgnoreMatcher => {
const matcher = createIgnore({ allowRelativePaths: true }).add(patterns);
const rootPrefix = rootPath.endsWith(path.sep) ? rootPath : `${rootPath}${path.sep}`;
const resolveRelativePath = createRelativePathResolver(rootPath);

return (filePath, isDirectory = false) => {
const relativePath = filePath.startsWith(rootPrefix)
? filePath.slice(rootPrefix.length)
: path.relative(rootPath, filePath);
const relativePath = resolveRelativePath(filePath);
if (relativePath === '') {
return false;
}
Expand Down
17 changes: 17 additions & 0 deletions packages/rstack/src/fmt/relativePath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import path from 'node:path';

type RelativePathResolver = (filePath: string) => string;

const createRelativePathResolver = (rootPath: string): RelativePathResolver => {
const rootPrefix = rootPath.endsWith(path.sep) ? rootPath : `${rootPath}${path.sep}`;

return (filePath) =>
filePath === rootPath
? ''
: filePath.startsWith(rootPrefix)
? filePath.slice(rootPrefix.length)
: path.relative(rootPath, filePath);
};

export { createRelativePathResolver };
export type { RelativePathResolver };
21 changes: 21 additions & 0 deletions packages/rstack/tests/fmt/relativePath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import path from 'node:path';
import { expect, test } from 'rstack/test';
import { createRelativePathResolver } from '../../src/fmt/relativePath.ts';

const rootPath = path.join(import.meta.dirname, 'project');

test('resolves paths relative to a fixed root', () => {
const resolveRelativePath = createRelativePathResolver(rootPath);

expect(resolveRelativePath(rootPath)).toBe('');
expect(resolveRelativePath(path.join(rootPath, 'src/index.ts'))).toBe(
path.join('src', 'index.ts'),
);
});

test('falls back for paths outside the fixed root', () => {
const resolveRelativePath = createRelativePathResolver(rootPath);
const siblingPath = path.join(`${rootPath}-other`, 'index.ts');

expect(resolveRelativePath(siblingPath)).toBe(path.relative(rootPath, siblingPath));
});