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
143 changes: 143 additions & 0 deletions packages/rstack/THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,63 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

## isoconcurrency

This package includes bundled code from
[isoconcurrency](https://github.com/fabiospampinato/isoconcurrency).

License: MIT

The MIT License (MIT)

Copyright (c) 2025-present Fabio Spampinato

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

## isotimer

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Add the missing immediato license notice

The published formatter bundle now includes isotimer, whose newly added dependency chain includes immediato@1.1.0 (pnpm-lock.yaml:3419-3421), but this notice lists the other WorkTank transitive packages without including immediato. Because its MIT license requires its copyright and permission notice to accompany substantial distributed copies, add immediato’s license entry before publishing the bundled package.

Useful? React with 👍 / 👎.


This package includes bundled code from [isotimer](https://github.com/fabiospampinato/isotimer).

License: MIT

The MIT License (MIT)

Copyright (c) 2025-present Fabio Spampinato

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

## micromatch

This package includes bundled code from [micromatch](https://github.com/micromatch/micromatch).
Expand Down Expand Up @@ -283,6 +340,35 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

## promise-make-naked

This package includes bundled code from
[promise-make-naked](https://github.com/fabiospampinato/promise-make-naked).

License: MIT

The MIT License (MIT)

Copyright (c) 2021-present Fabio Spampinato

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

## tinyexec

This package includes bundled code from [tinyexec](https://github.com/tinylibs/tinyexec).
Expand Down Expand Up @@ -338,3 +424,60 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

## webworker-shim

This package includes bundled code from
[webworker-shim](https://github.com/fabiospampinato/webworker-shim).

License: MIT

The MIT License (MIT)

Copyright (c) 2022-present Fabio Spampinato

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

## worktank

This package includes bundled code from [worktank](https://github.com/fabiospampinato/worktank).

License: MIT

The MIT License (MIT)

Copyright (c) 2021-present Fabio Spampinato

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
3 changes: 2 additions & 1 deletion packages/rstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
"micromatch": "catalog:",
"rslog": "catalog:",
"tiny-readdir": "catalog:",
"typescript": "catalog:"
"typescript": "catalog:",
"worktank": "catalog:"
},
"peerDependencies": {
"@rspress/core": "^2.0.17"
Expand Down
9 changes: 7 additions & 2 deletions packages/rstack/src/fmt/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { FmtMode, FmtRunResult } from './types.ts';
interface ParsedFmtCLIArgs {
mode: FmtMode;
patterns: string[];
parallel: boolean;
help: boolean;
}

Expand All @@ -24,6 +25,7 @@ ${color.cyan('Options')}:
--write Write formatted files in place (default)
--check Check whether files are formatted
--list-different Print paths of unformatted files
--no-parallel Disable worker parallelism
-h, --help Display this help message`;

const parseFmtCLIArgs = (args: string[]): ParsedFmtCLIArgs => {
Expand All @@ -34,6 +36,8 @@ const parseFmtCLIArgs = (args: string[]): ParsedFmtCLIArgs => {
check: { type: 'boolean' },
'list-different': { type: 'boolean' },
listDifferent: { type: 'boolean' },
'no-parallel': { type: 'boolean' },
noParallel: { type: 'boolean' },
help: { type: 'boolean', short: 'h' },
},
allowPositionals: true,
Expand All @@ -51,6 +55,7 @@ const parseFmtCLIArgs = (args: string[]): ParsedFmtCLIArgs => {
return {
mode,
patterns: positionals,
parallel: !(values['no-parallel'] || values.noParallel),
help: values.help ?? false,
};
};
Expand Down Expand Up @@ -93,7 +98,7 @@ const logFmtResult = (result: FmtRunResult, mode: FmtMode, cwd: string): void =>
};

const runFmtCLI = async (args: string[]): Promise<void> => {
const { help, mode, patterns } = parseFmtCLIArgs(args);
const { help, mode, parallel, patterns } = parseFmtCLIArgs(args);
if (help) {
console.log(fmtHelpMessage);
return;
Expand All @@ -118,7 +123,7 @@ const runFmtCLI = async (args: string[]): Promise<void> => {
files,
mode,
cache: false,
parallel: false,
parallel,
});

logFmtResult(result, mode, cwd);
Expand Down
54 changes: 54 additions & 0 deletions packages/rstack/src/fmt/parallel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Derived from @prettier/cli, see THIRD_PARTY_NOTICES.md

import { availableParallelism } from 'node:os';
import WorkTank from 'worktank';

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

interface FmtWorker {
formatFile: FmtWorkerMethods['formatFileSerial'];
terminate: () => void;
}

const getFmtWorkerCount = (fileCount: number): number =>
Math.min(fileCount, Math.max(1, availableParallelism() - 1));

const getFmtWorkerUrl = (): URL => {
// Source tests run after build and exercise the same worker artifact as the CLI.
const workerPath = new URL(import.meta.url).pathname.endsWith('.ts')
? '../../dist/fmtWorker.js'
: './fmtWorker.js';
return new URL(workerPath, import.meta.url);
};

/** Creates and starts every worker before formatting can begin. */
const createFmtWorker = async (fileCount: number): Promise<FmtWorker> => {
const workerCount = getFmtWorkerCount(fileCount);
const pool = new WorkTank<FmtWorkerMethods>({
pool: {
name: 'rstack-fmt',
size: workerCount,
},
worker: {
autoInstantiate: true,
methods: getFmtWorkerUrl(),
},
});

try {
// Concurrent handshakes make WorkTank assign one task to every worker.
await Promise.all(
Array.from({ length: workerCount }, () => pool.exec('initializeFmtWorker', [])),
);
} catch (error) {
pool.terminate();
throw error;
}

return {
formatFile: (file, shouldWrite) => pool.exec('formatFileSerial', [file, shouldWrite]),
terminate: pool.terminate,
};
};

export { createFmtWorker };
37 changes: 35 additions & 2 deletions packages/rstack/src/fmt/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,31 @@ const runFmtFilesSerial = async (
return results;
};

/** Processes files concurrently while preserving input order. */
const runFmtFilesParallel = async (
files: FmtFileRequest[],
shouldWrite: boolean,
): Promise<FmtFileResult[]> => {
const { createFmtWorker } = await import('./parallel.ts');
const worker = await createFmtWorker(files.length);

try {
return await Promise.all(files.map((file) => runFmtFile(file, shouldWrite, worker.formatFile)));
} finally {
worker.terminate();
}
};

/** Checks every worker payload before any formatting can begin. */
const canRunFmtFilesParallel = (files: FmtFileRequest[]): boolean => {
try {
structuredClone(files);
return true;
} catch {
return false;
}
};

/** Maps file results to the Prettier-compatible CLI exit code. */
const getFmtExitCode = (files: FmtFileResult[]): FmtExitCode => {
let exitCode: FmtExitCode = 0;
Expand All @@ -67,9 +92,17 @@ const getFmtExitCode = (files: FmtFileResult[]): FmtExitCode => {
};

/** Runs resolved files and summarizes their outcomes for the CLI. */
const runFmtFiles = async ({ files, mode }: RunFmtFilesOptions): Promise<FmtRunResult> => {
const runFmtFiles = async ({
files,
mode,
parallel,
}: RunFmtFilesOptions): Promise<FmtRunResult> => {
const startTime = performance.now();
const results = await runFmtFilesSerial(files, mode === 'write');
const shouldWrite = mode === 'write';
const results =
parallel && files.length > 1 && canRunFmtFilesParallel(files)
? await runFmtFilesParallel(files, shouldWrite)
: await runFmtFilesSerial(files, shouldWrite);

return {
files: results,
Expand Down
6 changes: 1 addition & 5 deletions packages/rstack/src/fmt/serial.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/**
* Derived from @prettier/cli v0.12.0.
* SPDX-License-Identifier: MIT
* Modified by Rstack contributors.
*/
// Derived from @prettier/cli, see THIRD_PARTY_NOTICES.md

import { readFile, writeFile } from 'atomically';
import { format } from 'prettier';
Expand Down
4 changes: 2 additions & 2 deletions packages/rstack/src/fmt/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ interface RunFmtFilesOptions {
mode: FmtMode;
/** Persistent cache support is added in a later implementation step. */
cache: false;
/** Parallel execution support is added in a later implementation step. */
parallel: false;
/** Whether cloneable file requests should run in worker threads. */
parallel: boolean;
}

interface SuccessfulFmtFileResult {
Expand Down
13 changes: 6 additions & 7 deletions packages/rstack/src/fmt/worker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* Internal build entry reserved for the fmt worker.
*
* The worker implementation is intentionally added by a later PR. Keeping a
* dedicated entry now establishes a stable published asset boundary.
*/
export {};
import { formatFileSerial } from './serial.ts';

/** Confirms that the worker module and its runtime dependencies are ready. */
const initializeFmtWorker = (): true => true;

export { formatFileSerial, initializeFmtWorker };
13 changes: 13 additions & 0 deletions packages/rstack/tests/cli/fmt/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ test('formats the current directory with Prettier defaults', () => {
expect(readProjectFile('index.ts')).toBe('const message = "hello";\n');
});

test('supports disabling parallel execution', () => {
writeProjectFile('first.ts', 'const first="first"');
writeProjectFile('second.ts', 'const second="second"');

const result = runFmt(['--no-parallel', 'first.ts', 'second.ts']);

expect(result.status).toBe(0);
expect(result.stdout).toBe('first.ts\nsecond.ts\n');
expect(result.stderr).toBe('');
expect(readProjectFile('first.ts')).toBe('const first = "first";\n');
expect(readProjectFile('second.ts')).toBe('const second = "second";\n');
});

test('does not load Prettier config or ignore files', () => {
writeProjectFile('.prettierrc.json', '{ "singleQuote": true, "semi": false }\n');
writeProjectFile('.prettierignore', 'index.ts\n');
Expand Down
Loading
Loading