Skip to content

Commit 9d30d63

Browse files
committed
refactor(fmt): streamline worker-only structure
1 parent 71ce5ba commit 9d30d63

13 files changed

Lines changed: 62 additions & 280 deletions

‎packages/rstack/src/fmt/cli.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ const runFmtCLI = async (args: string[]): Promise<void> => {
140140
const result = await runFmtFiles({
141141
files,
142142
mode,
143-
cache: false,
144143
maxWorkers,
145144
});
146145

‎packages/rstack/src/fmt/format.ts‎

Lines changed: 0 additions & 49 deletions
This file was deleted.

‎packages/rstack/src/fmt/parser.ts‎

Lines changed: 0 additions & 25 deletions
This file was deleted.

‎packages/rstack/src/fmt/runner.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import type {
33
FmtFileRequest,
44
FmtFileResult,
55
FmtRunResult,
6-
FmtWorkerFileResult,
76
RunFmtFilesOptions,
87
} from './types.ts';
8+
import type { FmtWorkerPool } from './workerPool.ts';
99

1010
/** Formats one file and reports whether its contents differ. */
11-
type FormatFile = (file: FmtFileRequest, shouldWrite: boolean) => Promise<FmtWorkerFileResult>;
11+
type FormatFile = FmtWorkerPool['formatFile'];
1212

1313
/** Converts a formatter outcome into the shared per-file result. */
1414
const runFmtFile = async (
@@ -39,22 +39,22 @@ const runFmtFile = async (
3939
}
4040
};
4141

42-
/** Processes files in workers while preserving input order. */
43-
const runFmtFilesWithWorkers = async (
42+
/** Processes files in a worker pool while preserving input order. */
43+
const runFmtFilesInWorkerPool = async (
4444
files: FmtFileRequest[],
4545
shouldWrite: boolean,
4646
maxWorkers?: number,
4747
): Promise<FmtFileResult[]> => {
48-
const { createFmtWorker } = await import('./parallel.ts');
49-
const worker = await createFmtWorker(files.length, maxWorkers);
48+
const { createFmtWorkerPool } = await import('./workerPool.ts');
49+
const workerPool = await createFmtWorkerPool(files.length, maxWorkers);
5050

5151
try {
5252
const results = await Promise.all(
53-
files.map((file) => runFmtFile(file, shouldWrite, worker.formatFile)),
53+
files.map((file) => runFmtFile(file, shouldWrite, workerPool.formatFile)),
5454
);
5555
return results.filter((result): result is FmtFileResult => result !== undefined);
5656
} finally {
57-
worker.terminate();
57+
workerPool.terminate();
5858
}
5959
};
6060

@@ -83,7 +83,7 @@ const runFmtFiles = async ({
8383
const startTime = performance.now();
8484
const shouldWrite = mode === 'write';
8585
const results =
86-
files.length === 0 ? [] : await runFmtFilesWithWorkers(files, shouldWrite, maxWorkers);
86+
files.length === 0 ? [] : await runFmtFilesInWorkerPool(files, shouldWrite, maxWorkers);
8787

8888
return {
8989
files: results,

‎packages/rstack/src/fmt/types.ts‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,6 @@ interface ResolvedFmtConfig {
4545
ignorePatterns: string[];
4646
}
4747

48-
interface FormatTextOptions {
49-
/** File path used to resolve per-file options and infer the parser. */
50-
filePath: string;
51-
/** Cursor offset in the source to preserve across formatting. */
52-
cursorOffset?: number;
53-
/** Resolved project config used to derive per-file options. */
54-
config: ResolvedFmtConfig;
55-
}
56-
5748
interface DiscoverFmtFilesOptions {
5849
/** Absolute directory used to resolve input paths. */
5950
cwd: string;
@@ -70,7 +61,6 @@ interface FmtFileRequest {
7061
options: ResolvedFmtOptions & Required<Pick<PrettierOptions, 'filepath'>>;
7162
}
7263

73-
type FmtWorkerFileResult = 'changed' | 'unchanged' | 'unsupported';
7464
type FmtMode = 'write' | 'check' | 'list-different';
7565
type FmtExitCode = 0 | 1 | 2;
7666

@@ -79,8 +69,6 @@ interface RunFmtFilesOptions {
7969
files: FmtFileRequest[];
8070
/** Whether to write changes or only report them. */
8171
mode: FmtMode;
82-
/** Persistent cache support is added in a later implementation step. */
83-
cache: false;
8472
/** Maximum number of formatting workers. */
8573
maxWorkers?: number;
8674
}
@@ -107,19 +95,6 @@ interface FmtRunResult {
10795
durationMs: number;
10896
}
10997

110-
interface FormattedTextResult {
111-
status: 'formatted';
112-
formatted: string;
113-
cursorOffset?: number;
114-
}
115-
116-
interface SkippedTextResult {
117-
status: 'skipped';
118-
reason: 'unsupported';
119-
}
120-
121-
type FormatTextResult = FormattedTextResult | SkippedTextResult;
122-
12398
export type {
12499
DiscoverFmtFilesOptions,
125100
FmtConfig,
@@ -130,9 +105,6 @@ export type {
130105
FmtMode,
131106
FmtPluginSpecifier,
132107
FmtRunResult,
133-
FmtWorkerFileResult,
134-
FormatTextOptions,
135-
FormatTextResult,
136108
ResolvedFmtConfig,
137109
ResolvedFmtOptions,
138110
RunFmtFilesOptions,

‎packages/rstack/src/fmt/worker.ts‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
11
// Derived from @prettier/cli, see THIRD_PARTY_NOTICES.md
22

33
import { readFileSync, writeFileSync } from 'node:fs';
4-
import { format } from 'prettier';
5-
import { resolveFmtParser } from './parser.ts';
4+
import {
5+
format,
6+
getFileInfo,
7+
type FileInfoOptions,
8+
type Options as PrettierOptions,
9+
} from 'prettier';
610
import { getPrettierPlugins } from './prettierPlugins.ts';
7-
import type { FmtFileRequest, FmtWorkerFileResult } from './types.ts';
11+
import type { FmtFileRequest } from './types.ts';
12+
13+
type PrettierPlugins = NonNullable<PrettierOptions['plugins']>;
14+
type FormatFileResult = 'changed' | 'unchanged' | 'unsupported';
15+
16+
const fileInfoOptions = {
17+
ignorePath: [],
18+
resolveConfig: false,
19+
withNodeModules: true,
20+
} satisfies FileInfoOptions;
21+
22+
/** Uses the configured parser or infers one without loading Prettier config. */
23+
const resolveFmtParser = async (
24+
filePath: string,
25+
options: PrettierOptions,
26+
plugins: PrettierPlugins,
27+
): Promise<PrettierOptions['parser'] | null> =>
28+
options.parser ??
29+
(
30+
await getFileInfo(filePath, {
31+
...fileInfoOptions,
32+
plugins,
33+
})
34+
).inferredParser;
835

936
/**
1037
* Use synchronous direct I/O inside the dedicated worker to avoid libuv
@@ -13,7 +40,7 @@ import type { FmtFileRequest, FmtWorkerFileResult } from './types.ts';
1340
const formatFile = async (
1441
{ path, options }: FmtFileRequest,
1542
shouldWrite: boolean,
16-
): Promise<FmtWorkerFileResult> => {
43+
): Promise<FormatFileResult> => {
1744
const plugins = await getPrettierPlugins(options);
1845
const parser = await resolveFmtParser(path, options, plugins);
1946
if (!parser) {
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import WorkTank from 'worktank';
55

66
type FmtWorkerMethods = typeof import('./worker.ts');
77

8-
interface FmtWorker {
8+
interface FmtWorkerPool {
99
formatFile: FmtWorkerMethods['formatFile'];
1010
terminate: () => void;
1111
}
@@ -22,7 +22,10 @@ const getFmtWorkerUrl = (): URL => {
2222
};
2323

2424
/** Creates and starts every worker before formatting can begin. */
25-
const createFmtWorker = async (fileCount: number, maxWorkers?: number): Promise<FmtWorker> => {
25+
const createFmtWorkerPool = async (
26+
fileCount: number,
27+
maxWorkers?: number,
28+
): Promise<FmtWorkerPool> => {
2629
const workerCount = getFmtWorkerCount(fileCount, maxWorkers);
2730
const pool = new WorkTank<FmtWorkerMethods>({
2831
pool: {
@@ -51,4 +54,5 @@ const createFmtWorker = async (fileCount: number, maxWorkers?: number): Promise<
5154
};
5255
};
5356

54-
export { createFmtWorker, getFmtWorkerCount };
57+
export { createFmtWorkerPool, getFmtWorkerCount };
58+
export type { FmtWorkerPool };

0 commit comments

Comments
 (0)