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
7 changes: 2 additions & 5 deletions packages/rstack/src/fmt/cli.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import path from 'node:path';
import { performance } from 'node:perf_hooks';
import { color, logger } from 'rslog';
import { parseArgs } from '../cli/args.ts';
import { loadRstackConfig } from '../config.ts';
import { resolveFmtConfig } from './config.ts';
import { discoverFmtFiles } from './discovery.ts';
import { createRelativePathResolver } from './relativePath.ts';
import { createRelativePathResolver, toPosixPath } from './pathHelpers.ts';
import { runFmtFiles } from './runner.ts';
import type { FmtMode, FmtRunResult, ResolvedFmtConfig } from './types.ts';

Expand Down Expand Up @@ -119,9 +118,7 @@ const parseFmtCLIArgs = (args: string[]): ParsedFmtCLIArgs => {
const createDisplayPathResolver = (cwd: string): ((filePath: string) => string) => {
const resolveRelativePath = createRelativePathResolver(cwd);

return path.sep === '\\'
? (filePath) => resolveRelativePath(filePath).replaceAll('\\', '/')
: resolveRelativePath;
return (filePath) => toPosixPath(resolveRelativePath(filePath));
};

const prettyTime = (seconds: number): string => {
Expand Down
2 changes: 1 addition & 1 deletion packages/rstack/src/fmt/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dirname } from 'node:path';
import micromatch from 'micromatch';
import { createRelativePathResolver } from './relativePath.ts';
import { createRelativePathResolver } from './pathHelpers.ts';
import type {
FmtConfig,
FmtConfigDefinition,
Expand Down
9 changes: 5 additions & 4 deletions packages/rstack/src/fmt/discoverPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ 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';
import {
createRelativePathResolver,
toPosixPath,
type RelativePathResolver,
} from './pathHelpers.ts';

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

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

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

/** Supports both the legacy tiny-readdir type and Node.js 24 Dirent. */
const getDirentParentPath = (dirent: Dirent): string =>
(dirent as Dirent & { parentPath?: string }).parentPath ?? dirent.path;
Expand Down
2 changes: 1 addition & 1 deletion packages/rstack/src/fmt/ignore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import createIgnore from 'ignore';
import { createRelativePathResolver } from './relativePath.ts';
import { createRelativePathResolver } from './pathHelpers.ts';
import type { ResolvedFmtConfig } from './types.ts';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import path from 'node:path';

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

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

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

Expand All @@ -13,5 +16,5 @@ const createRelativePathResolver = (rootPath: string): RelativePathResolver => {
: path.relative(rootPath, filePath);
};

export { createRelativePathResolver };
export { createRelativePathResolver, toPosixPath };
export type { RelativePathResolver };
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import path from 'node:path';
import { expect, test } from 'rstack/test';
import { createRelativePathResolver } from '../../src/fmt/relativePath.ts';
import { createRelativePathResolver, toPosixPath } from '../../src/fmt/pathHelpers.ts';

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

test('converts platform paths to POSIX paths', () => {
expect(toPosixPath(path.join('src', 'index.ts'))).toBe('src/index.ts');
});

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

Expand Down