diff --git a/packages/rstack/src/fmt/cli.ts b/packages/rstack/src/fmt/cli.ts new file mode 100644 index 0000000..3738feb --- /dev/null +++ b/packages/rstack/src/fmt/cli.ts @@ -0,0 +1,54 @@ +import { parseArgs } from 'node:util'; +import { color } from 'rslog'; +import type { FmtMode } from './types.ts'; + +interface ParsedFmtCLIArgs { + mode: FmtMode; + patterns: string[]; + help: boolean; +} + +const fmtHelpMessage: string = `Rstack v${RSTACK_VERSION} + +${color.cyan('Usage')}: +${color.yellow(' $ rs fmt [options] [files/globs...]')} + +Format files with Prettier. + +${color.cyan('Options')}: + --write Write formatted files in place (default) + --check Check whether files are formatted + --list-different Print paths of unformatted files + -h, --help Display this help message`; + +const parseFmtCLIArgs = (args: string[]): ParsedFmtCLIArgs => { + const { values, positionals } = parseArgs({ + args, + options: { + write: { type: 'boolean' }, + check: { type: 'boolean' }, + 'list-different': { type: 'boolean' }, + listDifferent: { type: 'boolean' }, + help: { type: 'boolean', short: 'h' }, + }, + allowPositionals: true, + strict: true, + }); + + const listDifferent = values['list-different'] || values.listDifferent; + const modes = [values.write, values.check, listDifferent].filter(Boolean); + if (modes.length > 1) { + throw new Error('The --write, --check, and --list-different options cannot be used together.'); + } + + const mode = values.check ? 'check' : listDifferent ? 'list-different' : 'write'; + + return { + mode, + patterns: positionals, + help: values.help ?? false, + }; +}; + +export { fmtHelpMessage, parseFmtCLIArgs }; +export type { ParsedFmtCLIArgs }; diff --git a/packages/rstack/src/fmt/runner.ts b/packages/rstack/src/fmt/runner.ts index d64b98d..dbef5ba 100644 --- a/packages/rstack/src/fmt/runner.ts +++ b/packages/rstack/src/fmt/runner.ts @@ -1,5 +1,5 @@ import type { FmtExitCode, FmtFileResult, FmtRunResult, RunFmtFilesOptions } from './types.ts'; -import { formatFileSerial } from './vendor/prettier-cli/serial.ts'; +import { formatFileSerial } from './serial.ts'; const runFmtFiles = async ({ files, mode }: RunFmtFilesOptions): Promise => { const startTime = performance.now(); diff --git a/packages/rstack/src/fmt/vendor/prettier-cli/serial.ts b/packages/rstack/src/fmt/serial.ts similarity index 91% rename from packages/rstack/src/fmt/vendor/prettier-cli/serial.ts rename to packages/rstack/src/fmt/serial.ts index 2254861..57abcfc 100644 --- a/packages/rstack/src/fmt/vendor/prettier-cli/serial.ts +++ b/packages/rstack/src/fmt/serial.ts @@ -6,7 +6,7 @@ import { readFile, writeFile } from 'atomically'; import { format } from 'prettier'; -import type { FmtFileRequest } from '../../types.ts'; +import type { FmtFileRequest } from './types.ts'; const formatFileSerial = async ( { path, options }: FmtFileRequest, diff --git a/packages/rstack/src/fmt/vendor/prettier-cli/LICENSE b/packages/rstack/src/fmt/vendor/prettier-cli/LICENSE deleted file mode 100644 index 5767e34..0000000 --- a/packages/rstack/src/fmt/vendor/prettier-cli/LICENSE +++ /dev/null @@ -1,7 +0,0 @@ -Copyright © James Long and contributors - -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. diff --git a/packages/rstack/tests/fmt/cli.test.ts b/packages/rstack/tests/fmt/cli.test.ts new file mode 100644 index 0000000..a38a24a --- /dev/null +++ b/packages/rstack/tests/fmt/cli.test.ts @@ -0,0 +1,72 @@ +import { expect, test } from 'rstack/test'; +import { fmtHelpMessage, parseFmtCLIArgs } from '../../src/fmt/cli.ts'; + +test('uses write mode by default', () => { + expect(parseFmtCLIArgs([])).toEqual({ + mode: 'write', + patterns: [], + help: false, + }); +}); + +test.each([ + ['--write', 'write'], + ['--check', 'check'], + ['--list-different', 'list-different'], + ['--listDifferent', 'list-different'], +] as const)('parses %s mode', (option, mode) => { + expect(parseFmtCLIArgs([option])).toEqual({ + mode, + patterns: [], + help: false, + }); +}); + +test('preserves file paths and globs', () => { + const patterns = ['src/file with spaces.ts', 'src/**/*.{js,ts}', '!src/generated/**']; + + expect(parseFmtCLIArgs([patterns[0], '--check', ...patterns.slice(1)])).toEqual({ + mode: 'check', + patterns, + help: false, + }); +}); + +test('treats arguments after the terminator as paths', () => { + expect(parseFmtCLIArgs(['--check', '--', '--write', '--help'])).toEqual({ + mode: 'check', + patterns: ['--write', '--help'], + help: false, + }); +}); + +test.each(['--help', '-h'])('parses %s', (option) => { + expect(parseFmtCLIArgs([option]).help).toBe(true); +}); + +test('provides command help', () => { + expect(fmtHelpMessage).toContain('Usage:\n $ rs fmt [options] [files/globs...]'); + expect(fmtHelpMessage).toContain('--write'); + expect(fmtHelpMessage).toContain('--check'); + expect(fmtHelpMessage).toContain('--list-different'); + expect(fmtHelpMessage).toContain('-h, --help'); +}); + +test.each([ + ['--write', '--check'], + ['--write', '--list-different'], + ['--write', '--listDifferent'], + ['--check', '--list-different'], + ['--write', '--check', '--list-different'], +])('rejects conflicting modes: %s', (...args) => { + expect(() => parseFmtCLIArgs(args)).toThrow( + 'The --write, --check, and --list-different options cannot be used together.', + ); +}); + +test.each(['--unknown', '--no-cache', '--no-parallel', '--parallel-workers'])( + 'rejects unsupported option %s', + (option) => { + expect(() => parseFmtCLIArgs([option])).toThrow(); + }, +);