11import getStdin from "get-stdin" ;
22import * as fs from "node:fs/promises" ;
3+ import * as path from "node:path" ;
34import * as process from "node:process" ;
45import type { Readable } from "node:stream" ;
5- import type { CLI , Options } from "../types.js" ;
6+ import type { CLI } from "../types.js" ;
67import { EXIT_ERROR , EXIT_FAIL , EXIT_OK } from "../util/constants.js" ;
78import { getErrorMessage } from "../util/getErrorMessage.js" ;
9+ import { getOptionsFromConfig } from "../util/getOptionsFromConfig.js" ;
810import { isMissingFileError } from "../util/isMissingFileError.js" ;
911import { parseArgs } from "../util/parseArgs.js" ;
1012import { parseFilePatterns } from "../util/parseFilePatterns.js" ;
@@ -22,7 +24,7 @@ export async function main(cli: CLI): Promise<void> {
2224}
2325
2426async function mainUnsafe ( cli : CLI ) : Promise < number > {
25- const args = parseArgs ( cli , process . argv . slice ( 2 ) ) ;
27+ const args = parseArgs ( process . argv . slice ( 2 ) ) ;
2628
2729 if ( args . help ) {
2830 printHelp ( cli ) ;
@@ -37,13 +39,13 @@ async function mainUnsafe(cli: CLI): Promise<number> {
3739 const hasFilePatterns = args . filePatterns . length > 0 ;
3840
3941 if ( hasFilePatterns ) {
40- return mainFormatFiles ( cli , args . check , args , args . filePatterns ) ;
42+ return mainFormatFiles ( cli , args . check , args . filePatterns ) ;
4143 }
4244
4345 // If no file patterns are provided, check if there's input from stdin.
4446 // If stdin TTY it's an interactive terminal, so we shouldn't read from it.
4547 if ( ! process . stdin . isTTY ) {
46- return mainFormatStdin ( cli , process . stdin , args . check , args ) ;
48+ return mainFormatStdin ( cli , process . stdin , args . check ) ;
4749 }
4850
4951 throw new Error (
@@ -54,15 +56,14 @@ async function mainUnsafe(cli: CLI): Promise<number> {
5456async function mainFormatFiles (
5557 cli : CLI ,
5658 check : boolean ,
57- options : Options ,
5859 filePatterns : string [ ] ,
5960) : Promise < number > {
6061 if ( check ) {
6162 console . log ( "Checking formatting..." ) ;
6263 }
6364
6465 const filePaths = await parseFilePatterns ( cli , filePatterns ) ;
65- const changedFileCount = await formatFiles ( cli , check , options , filePaths ) ;
66+ const changedFileCount = await formatFiles ( cli , check , filePaths ) ;
6667
6768 if ( check ) {
6869 if ( changedFileCount > 0 ) {
@@ -88,13 +89,12 @@ async function mainFormatFiles(
8889export async function formatFiles (
8990 cli : CLI ,
9091 check : boolean ,
91- options : Options ,
9292 filePaths : string [ ] ,
9393) : Promise < number > {
9494 let changedFileCount = 0 ;
9595
9696 for ( const fileName of filePaths ) {
97- if ( await formatFile ( cli , check , options , fileName ) ) {
97+ if ( await formatFile ( cli , check , fileName ) ) {
9898 changedFileCount ++ ;
9999 }
100100 }
@@ -105,10 +105,10 @@ export async function formatFiles(
105105export async function formatFile (
106106 cli : CLI ,
107107 check : boolean ,
108- options : Options ,
109108 filePath : string ,
110109) : Promise < boolean > {
111110 try {
111+ const options = await getOptionsFromConfig ( filePath ) ;
112112 const content = await fs . readFile ( filePath , "utf8" ) ;
113113 const formatted = await cli . format ( content , options , filePath ) ;
114114
@@ -142,10 +142,13 @@ export async function mainFormatStdin(
142142 cli : CLI ,
143143 stdin : Readable ,
144144 check : boolean ,
145- options : Options ,
146145) : Promise < number > {
147146 const input = await getStdin ( { stdin } ) ;
148- const formatted = await cli . format ( input , options , "stdin" ) ;
147+ const fileEnding = cli . getStdinFileEnding ( input ) ;
148+ const fileName = `stdin.${ fileEnding } ` ;
149+ const filePath = path . resolve ( fileName ) ;
150+ const options = await getOptionsFromConfig ( filePath ) ;
151+ const formatted = await cli . format ( input , options , filePath ) ;
149152
150153 if ( check ) {
151154 if ( input !== formatted ) {
0 commit comments