Skip to content

Commit 133c057

Browse files
authored
refactor(fmt): centralize path helpers (#212)
1 parent a116666 commit 133c057

6 files changed

Lines changed: 18 additions & 13 deletions

File tree

packages/rstack/src/fmt/cli.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import path from 'node:path';
21
import { performance } from 'node:perf_hooks';
32
import { color, logger } from 'rslog';
43
import { parseArgs } from '../cli/args.ts';
54
import { loadRstackConfig } from '../config.ts';
65
import { resolveFmtConfig } from './config.ts';
76
import { discoverFmtFiles } from './discovery.ts';
8-
import { createRelativePathResolver } from './relativePath.ts';
7+
import { createRelativePathResolver, toPosixPath } from './pathHelpers.ts';
98
import { runFmtFiles } from './runner.ts';
109
import type { FmtMode, FmtRunResult, ResolvedFmtConfig } from './types.ts';
1110

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

122-
return path.sep === '\\'
123-
? (filePath) => resolveRelativePath(filePath).replaceAll('\\', '/')
124-
: resolveRelativePath;
121+
return (filePath) => toPosixPath(resolveRelativePath(filePath));
125122
};
126123

127124
const prettyTime = (seconds: number): string => {

packages/rstack/src/fmt/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { dirname } from 'node:path';
22
import micromatch from 'micromatch';
3-
import { createRelativePathResolver } from './relativePath.ts';
3+
import { createRelativePathResolver } from './pathHelpers.ts';
44
import type {
55
FmtConfig,
66
FmtConfigDefinition,

packages/rstack/src/fmt/discoverPaths.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import ignore from 'ignore';
44
import isBinaryPath from 'is-binary-path';
55
import micromatch from 'micromatch';
66
import readdir, { type Dirent, type DirentLike } from 'tiny-readdir';
7-
import { createRelativePathResolver, type RelativePathResolver } from './relativePath.ts';
7+
import {
8+
createRelativePathResolver,
9+
toPosixPath,
10+
type RelativePathResolver,
11+
} from './pathHelpers.ts';
812

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

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

42-
const toPosixPath = (filePath: string): string =>
43-
path.sep === '\\' ? filePath.replaceAll('\\', '/') : filePath;
44-
4546
/** Supports both the legacy tiny-readdir type and Node.js 24 Dirent. */
4647
const getDirentParentPath = (dirent: Dirent): string =>
4748
(dirent as Dirent & { parentPath?: string }).parentPath ?? dirent.path;

packages/rstack/src/fmt/ignore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { readFile } from 'node:fs/promises';
22
import path from 'node:path';
33
import createIgnore from 'ignore';
4-
import { createRelativePathResolver } from './relativePath.ts';
4+
import { createRelativePathResolver } from './pathHelpers.ts';
55
import type { ResolvedFmtConfig } from './types.ts';
66

77
/**
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import path from 'node:path';
22

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

5+
const toPosixPath: (filePath: string) => string =
6+
path.sep === '\\' ? (filePath) => filePath.replaceAll('\\', '/') : (filePath) => filePath;
7+
58
const createRelativePathResolver = (rootPath: string): RelativePathResolver => {
69
const rootPrefix = rootPath.endsWith(path.sep) ? rootPath : `${rootPath}${path.sep}`;
710

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

16-
export { createRelativePathResolver };
19+
export { createRelativePathResolver, toPosixPath };
1720
export type { RelativePathResolver };

packages/rstack/tests/fmt/relativePath.test.ts renamed to packages/rstack/tests/fmt/pathHelpers.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import path from 'node:path';
22
import { expect, test } from 'rstack/test';
3-
import { createRelativePathResolver } from '../../src/fmt/relativePath.ts';
3+
import { createRelativePathResolver, toPosixPath } from '../../src/fmt/pathHelpers.ts';
44

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

7+
test('converts platform paths to POSIX paths', () => {
8+
expect(toPosixPath(path.join('src', 'index.ts'))).toBe('src/index.ts');
9+
});
10+
711
test('resolves paths relative to a fixed root', () => {
812
const resolveRelativePath = createRelativePathResolver(rootPath);
913

0 commit comments

Comments
 (0)