11import fs from "node:fs/promises" ;
22import isBinaryPath from "is-binary-path" ;
33import stringify from "json-sorted-stringify" ;
4+ import os from "node:os" ;
45import path from "node:path" ;
56import process from "node:process" ;
67import Cache from "./cache.js" ;
@@ -21,6 +22,7 @@ import {
2122 getProjectPath ,
2223 getStdin ,
2324 getTargetsPaths ,
25+ mapSettledWithConcurrency ,
2426} from "./utils.js" ;
2527import {
2628 fastRelativePath ,
@@ -179,34 +181,32 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions,
179181 const cache = shouldCache ? new Cache ( cacheVersion , projectPath , getCacheRootPath ( rootPath ) , options , stdout ) : undefined ;
180182 const prettier = await makePrettier ( options , cache ) ;
181183
182- //TODO: Maybe do work in chunks here, as keeping too many formatted files in memory can be a problem
183- const filesResults = await Promise . allSettled (
184- filesPathsTargets . map ( async ( filePath ) => {
185- const isIgnored = ( ) => ( ignoreManual ? ignoreManual ( filePath ) : getIgnoreResolved ( filePath , ignoreNames ) ) ;
186- const isCacheable = ( ) => cache ?. has ( filePath , isIgnored ) ;
187- const isExplicitlyIncluded = ( ) => filesExplicitPathsSet . has ( filePath ) ;
188- const isForceIncluded = options . dump && isExplicitlyIncluded ( ) ;
189- const isExcluded = cache ? ! ( await isCacheable ( ) ) : await isIgnored ( ) ;
190- if ( ! isForceIncluded && isExcluded ) return ;
191- const getFormatOptions = async ( ) : Promise < FormatOptions > => {
192- const editorConfig = options . editorConfig ? getEditorConfigFormatOptions ( await getEditorConfigResolved ( filePath , editorConfigNames ) ) : { } ;
193- const prettierConfig = prettierManualConfig || ( options . config ? await getPrettierConfigResolved ( filePath , prettierConfigNames ) : { } ) ;
194- const formatOptions = { ...editorConfig , ...prettierConfig , ...options . formatOptions } ;
195- return formatOptions ;
196- } ;
197- try {
198- if ( options . check || options . list ) {
199- return await prettier . checkWithPath ( filePath , getFormatOptions , cliContextConfig , pluginsDefaultOptions , pluginsCustomOptions ) ;
200- } else if ( options . write ) {
201- return await prettier . writeWithPath ( filePath , getFormatOptions , cliContextConfig , pluginsDefaultOptions , pluginsCustomOptions ) ;
202- } else {
203- return await prettier . formatWithPath ( filePath , getFormatOptions , cliContextConfig , pluginsDefaultOptions , pluginsCustomOptions ) ;
204- }
205- } finally {
206- spinner ?. update ( fastRelativePath ( rootPath , filePath ) ) ;
184+ const concurrency = options . parallel ? ( options . parallelWorkers ?? Math . max ( 1 , ( os . availableParallelism ?.( ) ?? os . cpus ( ) . length ) - 1 ) ) : 1 ;
185+ const filesResults = await mapSettledWithConcurrency ( filesPathsTargets , concurrency , async ( filePath ) => {
186+ const isIgnored = ( ) => ( ignoreManual ? ignoreManual ( filePath ) : getIgnoreResolved ( filePath , ignoreNames ) ) ;
187+ const isCacheable = ( ) => cache ?. has ( filePath , isIgnored ) ;
188+ const isExplicitlyIncluded = ( ) => filesExplicitPathsSet . has ( filePath ) ;
189+ const isForceIncluded = options . dump && isExplicitlyIncluded ( ) ;
190+ const isExcluded = cache ? ! ( await isCacheable ( ) ) : await isIgnored ( ) ;
191+ if ( ! isForceIncluded && isExcluded ) return ;
192+ const getFormatOptions = async ( ) : Promise < FormatOptions > => {
193+ const editorConfig = options . editorConfig ? getEditorConfigFormatOptions ( await getEditorConfigResolved ( filePath , editorConfigNames ) ) : { } ;
194+ const prettierConfig = prettierManualConfig || ( options . config ? await getPrettierConfigResolved ( filePath , prettierConfigNames ) : { } ) ;
195+ const formatOptions = { ...editorConfig , ...prettierConfig , ...options . formatOptions } ;
196+ return formatOptions ;
197+ } ;
198+ try {
199+ if ( options . check || options . list ) {
200+ return await prettier . checkWithPath ( filePath , getFormatOptions , cliContextConfig , pluginsDefaultOptions , pluginsCustomOptions ) ;
201+ } else if ( options . write ) {
202+ return await prettier . writeWithPath ( filePath , getFormatOptions , cliContextConfig , pluginsDefaultOptions , pluginsCustomOptions ) ;
203+ } else {
204+ return await prettier . formatWithPath ( filePath , getFormatOptions , cliContextConfig , pluginsDefaultOptions , pluginsCustomOptions ) ;
207205 }
208- } ) ,
209- ) ;
206+ } finally {
207+ spinner ?. update ( fastRelativePath ( rootPath , filePath ) ) ;
208+ }
209+ } ) ;
210210
211211 spinner ?. stop ( "Checking formatting..." ) ;
212212
0 commit comments